1
0
Fork 0

fix(config): group yargs to fix result TS interface

This commit is contained in:
ssube 2019-11-02 14:30:37 -05:00 committed by Sean Sube
parent 97256855ac
commit 5fefe0c79d
3 changed files with 75 additions and 73 deletions

View File

@ -36,7 +36,6 @@ export interface ParsedArgs extends RuleSelector, RuleSources {
defaults: boolean; defaults: boolean;
dest: string; dest: string;
mode: string; mode: string;
rules: Array<string>;
source: string; source: string;
} }
@ -47,10 +46,8 @@ export interface ParseResults {
/** /**
* Wrap yargs to exit after completion. * Wrap yargs to exit after completion.
*
* @TODO: fix it to use argv, not sure if yargs can do that
*/ */
export function parseArgs(argv: Array<string>): ParseResults { export async function parseArgs(argv: Array<string>): Promise<ParseResults> {
let mode: MODE = MODE.check; let mode: MODE = MODE.check;
const parser = usage(`Usage: salty-dog <mode> [options]`) const parser = usage(`Usage: salty-dog <mode> [options]`)
@ -64,13 +61,15 @@ export function parseArgs(argv: Array<string>): ParseResults {
.command({ .command({
builder: (yargs: any) => { builder: (yargs: any) => {
return yargs return yargs
.option('coerce', { .options({
default: false, coerce: {
type: 'boolean', default: false,
}) type: 'boolean',
.option('defaults', { },
default: true, defaults: {
type: 'boolean', default: true,
type: 'boolean',
},
}); });
}, },
command: ['fix'], command: ['fix'],
@ -93,70 +92,72 @@ export function parseArgs(argv: Array<string>): ParseResults {
mode = MODE.complete; mode = MODE.complete;
}, },
}) })
.option(CONFIG_ARGS_NAME, { .options({
default: `.${VERSION_INFO.package.name}.yml`, [CONFIG_ARGS_NAME]: {
group: 'Config:', default: `.${VERSION_INFO.package.name}.yml`,
type: 'string', group: 'Config:',
}) type: 'string',
.option(CONFIG_ARGS_PATH, { },
default: [], [CONFIG_ARGS_PATH]: {
group: 'Config:', default: [],
type: 'array', group: 'Config:',
}) type: 'array',
.option('count', { },
alias: ['c'], 'count': {
default: false, alias: ['c'],
desc: 'Exit with error count', default: false,
type: 'boolean', desc: 'Exit with error count',
}) type: 'boolean',
.option('dest', { },
alias: ['d'], 'dest': {
default: '-', alias: ['d'],
type: 'string', default: '-',
}) type: 'string',
.option('format', { },
alias: ['f'], 'exclude-level': RULE_OPTION,
default: 'yaml', 'exclude-name': RULE_OPTION,
type: 'string', 'exclude-tag': RULE_OPTION,
}) 'format': {
.option('rule-file', { alias: ['f'],
alias: ['r', 'rule', 'rules'], default: 'yaml',
default: [], type: 'string',
desc: 'Rules file', },
type: 'array', 'include-level': RULE_OPTION,
}) 'include-name': RULE_OPTION,
.option('rule-module', { 'include-tag': {
alias: ['m'], ...RULE_OPTION,
default: [], alias: ['t', 'tag'],
desc: 'Rules module', },
type: 'array', 'rule-file': {
}) alias: ['r', 'rule', 'rules'],
.option('rule-path', { default: [],
alias: ['p'], desc: 'Rules file',
default: [], type: 'array',
desc: 'Rules path', },
type: 'array', 'rule-module': {
}) alias: ['m'],
.option('source', { default: [],
alias: ['s'], desc: 'Rules module',
default: '-', type: 'array',
type: 'string', },
}) 'rule-path': {
.option('exclude-level', RULE_OPTION) alias: ['p'],
.option('exclude-name', RULE_OPTION) default: [],
.option('exclude-tag', RULE_OPTION) desc: 'Rules path',
.option('include-level', RULE_OPTION) type: 'array',
.option('include-name', RULE_OPTION) },
.option('include-tag', { 'source': {
...RULE_OPTION, alias: ['s'],
alias: ['t', 'tag'], default: '-',
type: 'string',
},
}) })
.help() .help()
.version(VERSION_INFO.package.version) .version(VERSION_INFO.package.version)
.alias('version', 'v'); .alias('version', 'v');
// @TODO: this should not need a cast but argv's type only has the last option (include-tag) // @TODO: this should not need a cast but the parser's type only has the last option (include-tag)
// @tslint:disable-next-line:no-any // tslint:disable-next-line:no-any
const args = parser.argv as any; const args = parser.argv as any;
return { return {
args, args,

View File

@ -14,7 +14,7 @@ const STATUS_ERROR = 1;
const STATUS_MAX = 255; const STATUS_MAX = 255;
export async function main(argv: Array<string>): Promise<number> { export async function main(argv: Array<string>): Promise<number> {
const { args, mode } = parseArgs(argv); const { args, mode } = await parseArgs(argv);
if (mode === MODE.complete) { if (mode === MODE.complete) {
showCompletionScript(); showCompletionScript();
return STATUS_SUCCESS; return STATUS_SUCCESS;
@ -24,7 +24,7 @@ export async function main(argv: Array<string>): Promise<number> {
const logger = createLogger(config.data.logger); const logger = createLogger(config.data.logger);
logger.info(VERSION_INFO, 'version info'); logger.info(VERSION_INFO, 'version info');
logger.info({ args }, 'main arguments'); logger.info({ args, mode }, 'main arguments');
// check mode // check mode
if (!VALID_MODES.has(mode)) { if (!VALID_MODES.has(mode)) {

View File

@ -9,7 +9,8 @@ export function doesExist<T>(val: T | null | undefined): val is T {
* *
* This is not a general replacement for `.length > 0`, since it is also a typeguard: * This is not a general replacement for `.length > 0`, since it is also a typeguard:
* `if (hasItems(val)) else { val }` will complain that `val` is `never` in the `else` * `if (hasItems(val)) else { val }` will complain that `val` is `never` in the `else`
* branch, since it was proven not to be an array by this function. * branch, since it was proven not to be an array by this function, even if `val` is
* simply empty.
*/ */
export function hasItems<T>(val: Array<T> | null | undefined): val is Array<T>; export function hasItems<T>(val: Array<T> | null | undefined): val is Array<T>;
export function hasItems<T>(val: ReadonlyArray<T> | null | undefined): val is ReadonlyArray<T>; export function hasItems<T>(val: ReadonlyArray<T> | null | undefined): val is ReadonlyArray<T>;