From 4d440e3edf411d46b908f77bc90ea3252e1a210c Mon Sep 17 00:00:00 2001 From: Paul Loyd Date: Sun, 17 Dec 2017 13:13:27 +0300 Subject: [PATCH] Make CLI --- bin/flow2schema | 2 +- declarations/optimist.js | 11 ------ declarations/yargs.js | 18 ++++++++++ package.json | 4 +-- src/cli.js | 72 +++++++++++++++++++++++++++------------- 5 files changed, 70 insertions(+), 37 deletions(-) delete mode 100644 declarations/optimist.js create mode 100644 declarations/yargs.js diff --git a/bin/flow2schema b/bin/flow2schema index d2316ff..797a3f1 100755 --- a/bin/flow2schema +++ b/bin/flow2schema @@ -1,3 +1,3 @@ #!/usr/bin/env node -require('../lib/cli'); +require('../lib/cli').default(process.argv.slice(2)); diff --git a/declarations/optimist.js b/declarations/optimist.js deleted file mode 100644 index 24c9210..0000000 --- a/declarations/optimist.js +++ /dev/null @@ -1,11 +0,0 @@ -declare module 'optimist' { - declare type Optimist = { - usage(string): Optimist, - argv: { - [string]: any, - _: string[], - }, - }; - - declare module.exports: Optimist; -} diff --git a/declarations/yargs.js b/declarations/yargs.js new file mode 100644 index 0000000..b8db401 --- /dev/null +++ b/declarations/yargs.js @@ -0,0 +1,18 @@ +declare module 'yargs' { + declare type Option = { + alias?: string, + type?: string, + demand?: boolean, + default?: mixed, + choices?: mixed[], + coerce?: any => mixed, + }; + + declare class Yargs { + usage(string): this; + option(string, Option): this; + get argv(): any; + } + + declare export default function yargs(string|string[]): Yargs; +} diff --git a/package.json b/package.json index dc9550a..134b242 100644 --- a/package.json +++ b/package.json @@ -26,10 +26,10 @@ "@babel/types": "^7.0.0-beta.32", "babylon": "^7.0.0-beta.32", "json-stringify-pretty-compact": "^1.0.4", - "optimist": "^0.6.1", "resolve": "^1.5.0", "wu": "^2.1.0", - "yaml-js": "^0.2.0" + "yaml-js": "^0.2.0", + "yargs": "^10.0.3" }, "devDependencies": { "@babel/cli": "^7.0.0-beta.32", diff --git a/src/cli.js b/src/cli.js index 0360064..de49952 100644 --- a/src/cli.js +++ b/src/cli.js @@ -1,39 +1,65 @@ import * as yaml from 'yaml-js'; -import * as optimist from 'optimist'; +import yargs from 'yargs'; import stringifyJson from 'json-stringify-pretty-compact'; import collect from '.'; -const argv = optimist - .usage('Usage: $0 ...') - .argv; +type Args = { + _: string[], + type: 'json-schema' | 'intermediate', + indent: number, + maxWidth: number, +}; -argv._.forEach(run); +function run(file: string, args: Args): string { + const {types, schema} = collect(file); -function run(path: string) { - if (path === '-') { - path = '/dev/stdin'; + switch (args.type) { + case 'intermediate': + return yaml.dump(types, null, null, { + indent: args.indent, + width: args.maxWidth, + }).trimRight(); + case 'json-schema': + default: + return stringifyJson(schema, { + indent: args.indent, + maxLength: args.maxWidth, + }); } +} + +export default function (argv: string[]) { + const args: Args = yargs(argv) + .usage('flow2schema -t type [file]') + .option('type', { + alias: 't', + choices: ['json-schema', 'intermediate'], + demand: true, + }) + .option('indent', { + type: 'number', + default: 4, + coerce: val => val >= 2 ? Math.floor(val) : 4, + }) + .option('max-width', { + type: 'number', + default: 100, + coerce: val => val >= 20 ? Math.floor(val) : 100, + }) + .argv; + + // TODO: support Windows. + const file = args._.length === 0 ? '/dev/stdin' : args._[0]; try { - const [indent, width] = [4, 80]; - const {types, schema} = collect(path); + const output = run(file, args); - const typesOutput = yaml.dump(types, null, null, { - indent, - width, - }).trimRight(); - - const schemaOutput = stringifyJson(schema, { - indent, - maxLength: width, - }); - - console.log(typesOutput); - console.log('--------'); - console.log(schemaOutput); + console.log(output); } catch (ex) { console.error(ex.message); console.error(ex.stack); + + process.exit(1); } }