From 8b9d84ef87bb52c15b530338747b3e8d534f5b74 Mon Sep 17 00:00:00 2001 From: ssube Date: Thu, 4 Jul 2019 10:35:27 -0500 Subject: [PATCH] fix: print completion or rules before attempting to load source --- config/rollup.js | 1 + src/index.ts | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/config/rollup.js b/config/rollup.js index 3c2f3b6..5a1b298 100644 --- a/config/rollup.js +++ b/config/rollup.js @@ -91,6 +91,7 @@ const bundle = { 'Type', ], 'node_modules/yargs/index.js': [ + 'showCompletionScript', 'usage', ], }, diff --git a/src/index.ts b/src/index.ts index 88bbfb7..d426a27 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import { createLogger } from 'bunyan'; -import { Options, usage } from 'yargs'; +import { Options, showCompletionScript, usage } from 'yargs'; import { loadConfig } from 'src/config'; import { YamlParser } from 'src/parser/YamlParser'; @@ -53,6 +53,13 @@ export async function main(argv: Array): Promise { 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:', @@ -99,14 +106,17 @@ export async function main(argv: Array): Promise { ...RULE_OPTION, alias: ['t', 'tag'], }) - .completion('completion', 'generate command completion for bash or zsh') .help() .version(VERSION_INFO.app.version) .alias('version', 'v') .argv; - const config = await loadConfig(args[CONFIG_ARGS_NAME], ...args[CONFIG_ARGS_PATH]); + if (mode === 'complete') { + showCompletionScript(); + return STATUS_SUCCESS; + } + const config = await loadConfig(args[CONFIG_ARGS_NAME], ...args[CONFIG_ARGS_PATH]); const logger = createLogger(config.data.logger); logger.info(VERSION_INFO, 'version info'); logger.info({ args }, 'main arguments'); @@ -131,10 +141,6 @@ export async function main(argv: Array): Promise { logger, }); - const parser = new YamlParser(); - const source = await loadSource(args.source); - let docs = parser.parse(source); - const rules = await loadRules(args.rules, ctx.ajv); const activeRules = await resolveRules(rules, args as any); @@ -143,6 +149,10 @@ export async function main(argv: Array): Promise { return STATUS_SUCCESS; } + const parser = new YamlParser(); + const source = await loadSource(args.source); + let docs = parser.parse(source); + for (const data of docs) { await visitRules(ctx, activeRules, data); }