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({
coerce: {
default: false, default: false,
type: 'boolean', type: 'boolean',
}) },
.option('defaults', { defaults: {
default: true, default: true,
type: 'boolean', 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({
[CONFIG_ARGS_NAME]: {
default: `.${VERSION_INFO.package.name}.yml`, default: `.${VERSION_INFO.package.name}.yml`,
group: 'Config:', group: 'Config:',
type: 'string', type: 'string',
}) },
.option(CONFIG_ARGS_PATH, { [CONFIG_ARGS_PATH]: {
default: [], default: [],
group: 'Config:', group: 'Config:',
type: 'array', type: 'array',
}) },
.option('count', { 'count': {
alias: ['c'], alias: ['c'],
default: false, default: false,
desc: 'Exit with error count', desc: 'Exit with error count',
type: 'boolean', type: 'boolean',
}) },
.option('dest', { 'dest': {
alias: ['d'], alias: ['d'],
default: '-', default: '-',
type: 'string', type: 'string',
}) },
.option('format', { 'exclude-level': RULE_OPTION,
'exclude-name': RULE_OPTION,
'exclude-tag': RULE_OPTION,
'format': {
alias: ['f'], alias: ['f'],
default: 'yaml', default: 'yaml',
type: 'string', type: 'string',
}) },
.option('rule-file', { 'include-level': RULE_OPTION,
'include-name': RULE_OPTION,
'include-tag': {
...RULE_OPTION,
alias: ['t', 'tag'],
},
'rule-file': {
alias: ['r', 'rule', 'rules'], alias: ['r', 'rule', 'rules'],
default: [], default: [],
desc: 'Rules file', desc: 'Rules file',
type: 'array', type: 'array',
}) },
.option('rule-module', { 'rule-module': {
alias: ['m'], alias: ['m'],
default: [], default: [],
desc: 'Rules module', desc: 'Rules module',
type: 'array', type: 'array',
}) },
.option('rule-path', { 'rule-path': {
alias: ['p'], alias: ['p'],
default: [], default: [],
desc: 'Rules path', desc: 'Rules path',
type: 'array', type: 'array',
}) },
.option('source', { 'source': {
alias: ['s'], alias: ['s'],
default: '-', default: '-',
type: 'string', 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() .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>;