Skip to content

Commit

Permalink
Improve CLI to accept codegen options
Browse files Browse the repository at this point in the history
- Can now generate all types of js codes except custom

Signed-off-by: Pouyan Heyratpour <pouyan@cloudzero.ir>
  • Loading branch information
pouyanh committed Oct 11, 2017
1 parent 9092973 commit b010342
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
File renamed without changes.
46 changes: 37 additions & 9 deletions lib/cli.js
Expand Up @@ -3,24 +3,52 @@
const fs = require('fs');
const pkg = require('../package.json');
const cli = require('commander');
const yaml = require('js-yaml').safeLoad;
const CodeGen = require('./codegen').CodeGen;
const fnMap = {
'typescript': CodeGen.getTypescriptCode,
'angular': CodeGen.getAngularCode,
'node': CodeGen.getNodeCode,
'react': CodeGen.getReactCode,
};

cli
.command('generate <file>')
.version(pkg.version)
.command('generate <file> [imports...]')
.alias('gen')
.description('Generate from Swagger file')
.action(file => {
const result = CodeGen.getTypescriptCode({
moduleName: 'Test',
className: 'Test',
swagger: JSON.parse(fs.readFileSync(file, 'utf-8')),
lint: false
.option('-t, --type <type>', 'Code type [typescript]', /^(typescript|angular|node|react)$/i, 'typescript')
.option('-m, --module <module>', 'Your AngularJS module name [Test]', 'Test')
.option('-c, --class <class>', 'Class name [Test]', 'Test')
.option('-l, --lint', 'Whether or not to run jslint on the generated code [false]')
.option('-b, --beautify', 'Whether or not to beautify the generated code [false]')
.action((file, imports, options) => {
const fn = fnMap[options.type];
options.lint = options.lint || false;
options.beautify = options.beautify || false;

const content = fs.readFileSync(file, 'utf-8');

var swagger;
try {
swagger = JSON.parse(content);
} catch (e) {
swagger = yaml(content);
}

const result = fn({
moduleName: options.module,
className: options.class,
swagger: swagger,
lint: options.lint,
beautify: options.beautify
});

console.log(result);
});

cli.version(pkg.version);
cli.parse(process.argv);

if (!cli.args.length) {
cli.help();
}
}
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -8,7 +8,7 @@
"clean": "rm -rf tmp-*"
},
"bin": {
"swagger2ts": "bin/swagger2ts.js"
"swagger2js": "bin/swagger2js.js"
},
"bugs": {
"url": "https://github.com/wcandillon/swagger-js-codegen/issues"
Expand All @@ -31,6 +31,7 @@
"dependencies": {
"commander": "^2.9.0",
"js-beautify": "^1.5.1",
"js-yaml": "^3.10.0",
"jshint": "^2.5.1",
"lodash": "^4.15.0",
"mustache": "^2.0.0",
Expand Down

0 comments on commit b010342

Please sign in to comment.