Skip to content

Commit

Permalink
fix: wrap yargs and exit after completion
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jul 4, 2019
1 parent 8b9d84e commit 8b8d669
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 103 deletions.
123 changes: 123 additions & 0 deletions 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 <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,
};
}
105 changes: 2 additions & 103 deletions 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<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');
Expand Down

0 comments on commit 8b8d669

Please sign in to comment.