1
0
Fork 0

fix: wrap yargs and exit after completion

This commit is contained in:
ssube 2019-07-04 10:52:17 -05:00
parent 8b9d84ef87
commit 8b8d669b46
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
2 changed files with 125 additions and 103 deletions

123
src/config/args.ts Normal file
View File

@ -0,0 +1,123 @@
import { Options, showCompletionScript } from 'yargs';
import { VERSION_INFO } from 'src/version';
const yargs = require('yargs/yargs');
export const CONFIG_ARGS_NAME = 'config-name';
export const CONFIG_ARGS_PATH = 'config-path';
const RULE_OPTION: Options = {
default: [],
group: 'Rules:',
type: 'array',
};
export interface Args {
args: any;
mode: string;
}
/**
* Wrap yargs to exit after completion.
*/
export function parseArgs(): Args {
let mode = 'check';
const args = yargs(process.argv.slice(2))
.usage(`Usage: salty-dog <mode> [options]`)
.command({
command: ['check', '*'],
describe: 'validate the source documents',
handler: (argv: any) => {
mode = 'check';
},
})
.command({
command: ['fix'],
describe: 'validate the source document and insert defaults',
builder: (yargs: any) => {
return yargs.option('coerce', {
default: false,
type: 'boolean',
});
},
handler: (argv: any) => {
mode = 'fix';
},
})
.command({
command: ['list'],
describe: 'list active rules',
handler: (argv: any) => {
mode = 'list';
},
})
.command({
command: ['complete'],
describe: 'generate tab completion script for bash or zsh',
handler: (argv: any) => {
mode = 'complete';
},
})
.option(CONFIG_ARGS_NAME, {
default: `.${VERSION_INFO.app.name}.yml`,
group: 'Config:',
type: 'string',
})
.option(CONFIG_ARGS_PATH, {
default: [],
group: 'Config:',
type: 'array',
})
.option('count', {
alias: ['c'],
default: false,
desc: 'Exit with error count',
type: 'boolean',
})
.option('dest', {
alias: ['d'],
default: '-',
type: 'string',
})
.option('format', {
alias: ['f'],
default: 'yaml',
type: 'string',
})
.option('rules', {
alias: ['r'],
default: [],
desc: 'Rules file',
type: 'array',
})
.option('source', {
alias: ['s'],
default: '-',
type: 'string',
})
.option('exclude-level', RULE_OPTION)
.option('exclude-name', RULE_OPTION)
.option('exclude-tag', RULE_OPTION)
.option('include-level', RULE_OPTION)
.option('include-name', RULE_OPTION)
.option('include-tag', {
...RULE_OPTION,
alias: ['t', 'tag'],
})
.help()
.version(VERSION_INFO.app.version)
.alias('version', 'v')
.argv;
if (mode === 'complete') {
showCompletionScript();
process.exit(0);
}
return {
args,
mode,
};
}

View File

@ -1,121 +1,20 @@
import { createLogger } from 'bunyan';
import { Options, showCompletionScript, usage } from 'yargs';
import { loadConfig } from 'src/config';
import { CONFIG_ARGS_NAME, CONFIG_ARGS_PATH, parseArgs } from 'src/config/args';
import { YamlParser } from 'src/parser/YamlParser';
import { loadRules, resolveRules, visitRules } from 'src/rule';
import { loadSource, writeSource } from 'src/source';
import { VERSION_INFO } from 'src/version';
import { VisitorContext } from 'src/visitor/context';
const CONFIG_ARGS_NAME = 'config-name';
const CONFIG_ARGS_PATH = 'config-path';
const MODES = ['check', 'fix', 'list'];
const RULE_OPTION: Options = {
default: [],
group: 'Rules:',
type: 'array',
};
const STATUS_SUCCESS = 0;
const STATUS_ERROR = 1;
export async function main(argv: Array<string>): Promise<number> {
let mode = 'check';
const args = usage(`Usage: salty-dog <mode> [options]`)
.command({
command: ['check', '*'],
describe: 'validate the source documents',
handler: (argv) => {
mode = 'check';
},
})
.command({
command: ['fix'],
describe: 'validate the source document and insert defaults',
builder: (yargs: any) => {
return yargs.option('coerce', {
default: false,
type: 'boolean',
});
},
handler: (argv) => {
mode = 'fix';
},
})
.command({
command: ['list'],
describe: 'list active rules',
handler: (argv) => {
mode = 'list';
},
})
.command({
command: ['complete'],
describe: 'generate tab completion script for bash or zsh',
handler: (argv) => {
mode = 'complete';
},
})
.option(CONFIG_ARGS_NAME, {
default: `.${VERSION_INFO.app.name}.yml`,
group: 'Config:',
type: 'string',
})
.option(CONFIG_ARGS_PATH, {
default: [],
group: 'Config:',
type: 'array',
})
.option('count', {
alias: ['c'],
default: false,
desc: 'Exit with error count',
type: 'boolean',
})
.option('dest', {
alias: ['d'],
default: '-',
type: 'string',
})
.option('format', {
alias: ['f'],
default: 'yaml',
type: 'string',
})
.option('rules', {
alias: ['r'],
default: [],
desc: 'Rules file',
type: 'array',
})
.option('source', {
alias: ['s'],
default: '-',
type: 'string',
})
.option('exclude-level', RULE_OPTION)
.option('exclude-name', RULE_OPTION)
.option('exclude-tag', RULE_OPTION)
.option('include-level', RULE_OPTION)
.option('include-name', RULE_OPTION)
.option('include-tag', {
...RULE_OPTION,
alias: ['t', 'tag'],
})
.help()
.version(VERSION_INFO.app.version)
.alias('version', 'v')
.argv;
if (mode === 'complete') {
showCompletionScript();
return STATUS_SUCCESS;
}
const { args, mode } = parseArgs();
const config = await loadConfig(args[CONFIG_ARGS_NAME], ...args[CONFIG_ARGS_PATH]);
const logger = createLogger(config.data.logger);
logger.info(VERSION_INFO, 'version info');