From 8b8d669b4607922653e1a835b3d79614d45d5f43 Mon Sep 17 00:00:00 2001 From: ssube Date: Thu, 4 Jul 2019 10:52:17 -0500 Subject: [PATCH] fix: wrap yargs and exit after completion --- src/config/args.ts | 123 +++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 105 +------------------------------------- 2 files changed, 125 insertions(+), 103 deletions(-) create mode 100644 src/config/args.ts diff --git a/src/config/args.ts b/src/config/args.ts new file mode 100644 index 0000000..ae4ee10 --- /dev/null +++ b/src/config/args.ts @@ -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 [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, + }; +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index d426a27..e75ba8a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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): Promise { - let mode = 'check'; - - const args = usage(`Usage: salty-dog [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');