Make CLI
parent
31877f825e
commit
4d440e3edf
|
@ -1,3 +1,3 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
require('../lib/cli');
|
||||
require('../lib/cli').default(process.argv.slice(2));
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
declare module 'optimist' {
|
||||
declare type Optimist = {
|
||||
usage(string): Optimist,
|
||||
argv: {
|
||||
[string]: any,
|
||||
_: string[],
|
||||
},
|
||||
};
|
||||
|
||||
declare module.exports: Optimist;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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",
|
||||
|
|
72
src/cli.js
72
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 <path> ...')
|
||||
.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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue