Skip to content

Commit

Permalink
feat: Add force deletion flag (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffflorian committed May 28, 2019
1 parent d42bb2d commit ffa408d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
},
"dependencies": {
"axios": "0.18.0",
"ci-info": "2.0.0",
"cli-interact": "0.1.9",
"commander": "2.20.0",
"fs-extra": "8.0.1",
"handlebars": "4.1.2",
Expand All @@ -14,6 +16,7 @@
},
"description": "Generate Swagger API clients with axios",
"devDependencies": {
"@types/cli-interact": "0.1.0",
"@types/fs-extra": "7.0.0",
"@types/handlebars-helpers": "0.5.2",
"@types/jasmine": "3.3.12",
Expand Down Expand Up @@ -56,11 +59,9 @@
"name": "swaxios",
"prettier": "@wireapp/prettier-config",
"scripts": {
"clean:temp": "rimraf src/temp",
"clean:dist": "rimraf dist",
"clean": "yarn clean:temp && yarn clean:dist",
"clean": "rimraf dist",
"copy": "cpx src/templates/**/* dist/templates/",
"dist": "yarn clean:dist && tsc && yarn copy",
"dist": "yarn clean && tsc && yarn copy",
"fix": "yarn fix:other && yarn fix:ts",
"fix:other": "yarn prettier --write",
"fix:ts": "yarn lint:ts --fix",
Expand All @@ -69,7 +70,7 @@
"lint:ts": "tslint --config tslint.json --project tsconfig.json \"**/*.ts?(x)\"",
"prettier": "prettier \"**/*.{json,md}\"",
"release:patch": "git pull && yarn && yarn dist && npm version patch && npm publish && git push --follow-tags",
"start": "yarn clean:temp && ts-node src/cli.ts -i ./src/test/fixtures/wire-sso.json -o ./src/temp",
"start": "ts-node src/cli.ts -i ./src/test/fixtures/wire-sso.json -o ./src/temp -f",
"test": "yarn lint && yarn test:node",
"test:node": "nyc jasmine --config=jasmine.json"
},
Expand Down
25 changes: 24 additions & 1 deletion src/Swaxios.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import axios from 'axios';
import {getYesNo} from 'cli-interact';
import fs from 'fs-extra';
import initializeHelpers from 'handlebars-helpers';
import path from 'path';
import {Path, Spec} from 'swagger-schema-official';
import url from 'url';
import yaml from 'yamljs';
const {isCI} = require('ci-info');

import {APIClientGenerator, IndexFileGenerator, ResourceGenerator} from './generators';
import {DirEntry, generateFileIndex} from './util/FileUtil';
Expand Down Expand Up @@ -100,7 +102,28 @@ async function readInputFile(inputFile: string): Promise<Spec> {
return swaggerJson;
}

export async function writeClient(inputFile: string, outputDirectory: string): Promise<void> {
async function checkOutputDirectory(outputDirectory: string, forceDeletion?: boolean): Promise<void> {
const directoryExists = await fs.pathExists(outputDirectory);
function shouldDelete(): boolean {
const question = `The output directory "${outputDirectory}" exists already. Would you like to delete it?\nNOTE: Without deletion, Swaxios can generate unexpected results.`;
return forceDeletion || (!isCI && getYesNo(question));
}

if (directoryExists) {
if (shouldDelete()) {
console.info(`Deleting "${outputDirectory}" ...`);
await fs.remove(outputDirectory);
return;
}

if (isCI) {
throw new Error(`Output directory "${outputDirectory}" exists already`);
}
}
}

export async function writeClient(inputFile: string, outputDirectory: string, forceDeletion?: boolean): Promise<void> {
await checkOutputDirectory(outputDirectory, forceDeletion);
const parsedInput = url.parse(inputFile);
const swaggerJson = parsedInput.protocol ? await readInputURL(inputFile) : await readInputFile(inputFile);
await validateConfig(swaggerJson);
Expand Down
9 changes: 4 additions & 5 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@ program
.version(version, '-v, --version')
.option('-i, --input <file>', 'File path (or URL) to OpenAPI Specification, i.e. swagger.json (required)')
.option('-o, --output <directory>', 'Path to output directory for generated TypeScript code (required)')
.option('-f, --force', 'Force deleting the output directory before generating')
.parse(process.argv);

if (!program.input || !program.output) {
program.outputHelp();
process.exit(1);
}

const outputDirectory = path.resolve(process.cwd(), program.output);
const outputDirectory = path.resolve(program.output || '.');

writeClient(program.input, outputDirectory)
.then(() => {
console.log(`Created API client in "${outputDirectory}".`);
})
writeClient(program.input, outputDirectory, program.force)
.then(() => console.log(`Created API client in "${outputDirectory}".`))
.catch(error => {
console.error(error);
process.exit(1);
Expand Down
26 changes: 25 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@
dependencies:
any-observable "^0.3.0"

"@types/cli-interact@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@types/cli-interact/-/cli-interact-0.1.0.tgz#63379a2009727dd3d893d098bc183700c28ee711"
integrity sha512-Xq/ucHBj2GLoW0bWEm4KnzUy+joweC1A/ce4W4qxie84c0OFyZ2Q2Uli+2q6nkDBNQfm+wccJp8NGvExg+xj1g==
dependencies:
"@types/readline-sync" "*"

"@types/fs-extra@7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-7.0.0.tgz#9c4ad9e1339e7448a76698829def1f159c1b636c"
Expand Down Expand Up @@ -143,6 +150,11 @@
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-1.16.4.tgz#5e5e97702cb68498aaba7349b941648daaf2385c"
integrity sha512-MG7ExKBo7AQ5UrL1awyYLNinNM/kyXgE4iP4Ul9fB+T7n768Z5Xem8IZeP6Bna0xze8gkDly49Rgge2HOEw4xA==

"@types/readline-sync@*":
version "1.4.3"
resolved "https://registry.yarnpkg.com/@types/readline-sync/-/readline-sync-1.4.3.tgz#eac55a39d5a349912062c9e5216cd550c07fd9c8"
integrity sha512-YP9NVli96E+qQLAF2db+VjnAUEeZcFVg4YnMgr8kpDUFwQBnj31rPLOVHmazbKQhaIkJ9cMHsZhpKdzUeL0KTg==

"@types/swagger-parser@4.0.3":
version "4.0.3"
resolved "https://registry.yarnpkg.com/@types/swagger-parser/-/swagger-parser-4.0.3.tgz#ff88b5999118143fce8ac38594bc9ad2ffacaf38"
Expand Down Expand Up @@ -755,7 +767,7 @@ chownr@^1.1.1:
resolved "https://registry.yarnpkg.com/chownr/-/chownr-1.1.1.tgz#54726b8b8fff4df053c42187e801fb4412df1494"
integrity sha512-j38EvO5+LHX84jlo6h4UzmOwi0UgW61WRyPtJz4qaadK5eY3BTS5TY/S1Stc3Uk2lIM6TPevAlULiEJwie860g==

ci-info@^2.0.0:
ci-info@2.0.0, ci-info@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-2.0.0.tgz#67a9e964be31a51e15e5010d58e6f12834002f46"
integrity sha512-5tK7EtrZ0N+OLFMthtqOj4fI2Jeb88C4CAZPu25LDVUgXJ0A3Js4PMGqrn0JU1W0Mh1/Z8wZzYPxqUrXeBboCQ==
Expand All @@ -777,6 +789,13 @@ cli-cursor@^2.0.0, cli-cursor@^2.1.0:
dependencies:
restore-cursor "^2.0.0"

cli-interact@0.1.9:
version "0.1.9"
resolved "https://registry.yarnpkg.com/cli-interact/-/cli-interact-0.1.9.tgz#e58ae8505bbf7ed2e12671057cd559b12fdce28f"
integrity sha1-5YroUFu/ftLhJnEFfNVZsS/c4o8=
dependencies:
readline-sync "~0.4.9"

cli-truncate@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/cli-truncate/-/cli-truncate-0.2.1.tgz#9f15cfbb0705005369216c626ac7d05ab90dd574"
Expand Down Expand Up @@ -3058,6 +3077,11 @@ readdirp@^2.0.0:
micromatch "^3.1.10"
readable-stream "^2.0.2"

readline-sync@~0.4.9:
version "0.4.10"
resolved "https://registry.yarnpkg.com/readline-sync/-/readline-sync-0.4.10.tgz#bef03d993eca3585fd064f5d56d7276765dfc121"
integrity sha1-vvA9mT7KNYX9Bk9dVtcnZ2XfwSE=

regenerator-runtime@^0.11.0:
version "0.11.1"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9"
Expand Down

0 comments on commit ffa408d

Please sign in to comment.