Make CLI
parent
31877f825e
commit
4d440e3edf
|
@ -1,3 +1,3 @@
|
||||||
#!/usr/bin/env node
|
#!/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",
|
"@babel/types": "^7.0.0-beta.32",
|
||||||
"babylon": "^7.0.0-beta.32",
|
"babylon": "^7.0.0-beta.32",
|
||||||
"json-stringify-pretty-compact": "^1.0.4",
|
"json-stringify-pretty-compact": "^1.0.4",
|
||||||
"optimist": "^0.6.1",
|
|
||||||
"resolve": "^1.5.0",
|
"resolve": "^1.5.0",
|
||||||
"wu": "^2.1.0",
|
"wu": "^2.1.0",
|
||||||
"yaml-js": "^0.2.0"
|
"yaml-js": "^0.2.0",
|
||||||
|
"yargs": "^10.0.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/cli": "^7.0.0-beta.32",
|
"@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 yaml from 'yaml-js';
|
||||||
import * as optimist from 'optimist';
|
import yargs from 'yargs';
|
||||||
import stringifyJson from 'json-stringify-pretty-compact';
|
import stringifyJson from 'json-stringify-pretty-compact';
|
||||||
|
|
||||||
import collect from '.';
|
import collect from '.';
|
||||||
|
|
||||||
const argv = optimist
|
type Args = {
|
||||||
.usage('Usage: $0 <path> ...')
|
_: string[],
|
||||||
.argv;
|
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) {
|
switch (args.type) {
|
||||||
if (path === '-') {
|
case 'intermediate':
|
||||||
path = '/dev/stdin';
|
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 {
|
try {
|
||||||
const [indent, width] = [4, 80];
|
const output = run(file, args);
|
||||||
const {types, schema} = collect(path);
|
|
||||||
|
|
||||||
const typesOutput = yaml.dump(types, null, null, {
|
console.log(output);
|
||||||
indent,
|
|
||||||
width,
|
|
||||||
}).trimRight();
|
|
||||||
|
|
||||||
const schemaOutput = stringifyJson(schema, {
|
|
||||||
indent,
|
|
||||||
maxLength: width,
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(typesOutput);
|
|
||||||
console.log('--------');
|
|
||||||
console.log(schemaOutput);
|
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
console.error(ex.message);
|
console.error(ex.message);
|
||||||
console.error(ex.stack);
|
console.error(ex.stack);
|
||||||
|
|
||||||
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue