fix: wrap yargs and exit after completion
This commit is contained in:
parent
8b9d84ef87
commit
8b8d669b46
|
@ -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
src/index.ts
105
src/index.ts
|
@ -1,121 +1,20 @@
|
||||||
import { createLogger } from 'bunyan';
|
import { createLogger } from 'bunyan';
|
||||||
import { Options, showCompletionScript, usage } from 'yargs';
|
|
||||||
|
|
||||||
import { loadConfig } from 'src/config';
|
import { loadConfig } from 'src/config';
|
||||||
|
import { CONFIG_ARGS_NAME, CONFIG_ARGS_PATH, parseArgs } from 'src/config/args';
|
||||||
import { YamlParser } from 'src/parser/YamlParser';
|
import { YamlParser } from 'src/parser/YamlParser';
|
||||||
import { loadRules, resolveRules, visitRules } from 'src/rule';
|
import { loadRules, resolveRules, visitRules } from 'src/rule';
|
||||||
import { loadSource, writeSource } from 'src/source';
|
import { loadSource, writeSource } from 'src/source';
|
||||||
import { VERSION_INFO } from 'src/version';
|
import { VERSION_INFO } from 'src/version';
|
||||||
import { VisitorContext } from 'src/visitor/context';
|
import { VisitorContext } from 'src/visitor/context';
|
||||||
|
|
||||||
const CONFIG_ARGS_NAME = 'config-name';
|
|
||||||
const CONFIG_ARGS_PATH = 'config-path';
|
|
||||||
|
|
||||||
const MODES = ['check', 'fix', 'list'];
|
const MODES = ['check', 'fix', 'list'];
|
||||||
|
|
||||||
const RULE_OPTION: Options = {
|
|
||||||
default: [],
|
|
||||||
group: 'Rules:',
|
|
||||||
type: 'array',
|
|
||||||
};
|
|
||||||
|
|
||||||
const STATUS_SUCCESS = 0;
|
const STATUS_SUCCESS = 0;
|
||||||
const STATUS_ERROR = 1;
|
const STATUS_ERROR = 1;
|
||||||
|
|
||||||
export async function main(argv: Array<string>): Promise<number> {
|
export async function main(argv: Array<string>): Promise<number> {
|
||||||
let mode = 'check';
|
const { args, mode } = parseArgs();
|
||||||
|
|
||||||
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 config = await loadConfig(args[CONFIG_ARGS_NAME], ...args[CONFIG_ARGS_PATH]);
|
const config = await loadConfig(args[CONFIG_ARGS_NAME], ...args[CONFIG_ARGS_PATH]);
|
||||||
const logger = createLogger(config.data.logger);
|
const logger = createLogger(config.data.logger);
|
||||||
logger.info(VERSION_INFO, 'version info');
|
logger.info(VERSION_INFO, 'version info');
|
||||||
|
|
Loading…
Reference in New Issue