fix(config): make args parser type safe
This commit is contained in:
parent
f0023bd2a8
commit
0427a092be
|
@ -1,4 +1,4 @@
|
||||||
import { mustGet } from '@apextoaster/js-utils';
|
import { mustDefault, mustGet } from '@apextoaster/js-utils';
|
||||||
import { createLogger } from 'bunyan';
|
import { createLogger } from 'bunyan';
|
||||||
import yargs from 'yargs';
|
import yargs from 'yargs';
|
||||||
|
|
||||||
|
@ -58,9 +58,9 @@ export async function main(argv: Array<string>): Promise<number> {
|
||||||
const ctx = new VisitorContext({
|
const ctx = new VisitorContext({
|
||||||
logger,
|
logger,
|
||||||
schemaOptions: {
|
schemaOptions: {
|
||||||
coerce: args.coerce,
|
coerce: mustDefault(args.coerce, false),
|
||||||
defaults: args.defaults,
|
defaults: mustDefault(args.defaults, true),
|
||||||
mutate: args.mutate,
|
mutate: mustDefault(args.mutate, true),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import yargs, { Options } from 'yargs';
|
import { LogLevel } from 'noicejs';
|
||||||
|
import yargs from 'yargs';
|
||||||
|
|
||||||
import { RuleSources } from '../rule/load.js';
|
import { RuleSources } from '../rule/load.js';
|
||||||
import { RuleSelector } from '../rule/resolve.js';
|
import { RuleSelector } from '../rule/resolve.js';
|
||||||
|
@ -14,27 +15,14 @@ export enum MODE {
|
||||||
export const CONFIG_ARGS_NAME = 'config-name';
|
export const CONFIG_ARGS_NAME = 'config-name';
|
||||||
export const CONFIG_ARGS_PATH = 'config-path';
|
export const CONFIG_ARGS_PATH = 'config-path';
|
||||||
|
|
||||||
const RULE_OPTION: Options = {
|
|
||||||
default: [],
|
|
||||||
group: 'Rules:',
|
|
||||||
type: 'array',
|
|
||||||
};
|
|
||||||
|
|
||||||
export interface Args {
|
|
||||||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
|
||||||
args: any;
|
|
||||||
mode: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ParsedArgs extends RuleSelector, RuleSources {
|
export interface ParsedArgs extends RuleSelector, RuleSources {
|
||||||
[CONFIG_ARGS_NAME]: string;
|
[CONFIG_ARGS_NAME]: string;
|
||||||
[CONFIG_ARGS_PATH]: string;
|
[CONFIG_ARGS_PATH]: Array<string>;
|
||||||
coerce: boolean;
|
coerce?: boolean;
|
||||||
count: boolean;
|
count: boolean;
|
||||||
defaults: boolean;
|
defaults?: boolean;
|
||||||
dest: string;
|
dest: string;
|
||||||
mode: string;
|
mutate?: boolean;
|
||||||
mutate: boolean;
|
|
||||||
reporter: string;
|
reporter: string;
|
||||||
source: string;
|
source: string;
|
||||||
}
|
}
|
||||||
|
@ -50,7 +38,8 @@ export interface ParseResults {
|
||||||
export async function parseArgs(argv: Array<string>): Promise<ParseResults> {
|
export async function parseArgs(argv: Array<string>): Promise<ParseResults> {
|
||||||
let mode: MODE = MODE.check;
|
let mode: MODE = MODE.check;
|
||||||
|
|
||||||
const parser = yargs(argv).usage('Usage: salty-dog <mode> [options]')
|
const parser = yargs(argv)
|
||||||
|
.usage('Usage: salty-dog <mode> [options]')
|
||||||
.command({
|
.command({
|
||||||
command: ['check', '*'],
|
command: ['check', '*'],
|
||||||
describe: 'validate the source documents',
|
describe: 'validate the source documents',
|
||||||
|
@ -101,7 +90,7 @@ export async function parseArgs(argv: Array<string>): Promise<ParseResults> {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
},
|
},
|
||||||
[CONFIG_ARGS_PATH]: {
|
[CONFIG_ARGS_PATH]: {
|
||||||
default: [],
|
default: [] as Array<string>,
|
||||||
group: 'Config:',
|
group: 'Config:',
|
||||||
type: 'array',
|
type: 'array',
|
||||||
},
|
},
|
||||||
|
@ -116,25 +105,43 @@ export async function parseArgs(argv: Array<string>): Promise<ParseResults> {
|
||||||
default: '-',
|
default: '-',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
},
|
},
|
||||||
'exclude-level': RULE_OPTION,
|
'exclude-level': {
|
||||||
'exclude-name': RULE_OPTION,
|
default: [] as Array<LogLevel>,
|
||||||
'exclude-tag': RULE_OPTION,
|
group: 'Rules:',
|
||||||
|
type: 'array',
|
||||||
|
},
|
||||||
|
'exclude-name': {
|
||||||
|
default: [] as Array<string>,
|
||||||
|
group: 'Rules:',
|
||||||
|
type: 'array',
|
||||||
|
},
|
||||||
|
'exclude-tag': {
|
||||||
|
default: [] as Array<string>,
|
||||||
|
group: 'Rules:',
|
||||||
|
type: 'array',
|
||||||
|
},
|
||||||
'format': {
|
'format': {
|
||||||
alias: ['f'],
|
alias: ['f'],
|
||||||
default: 'yaml',
|
default: 'yaml',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
},
|
},
|
||||||
'include-level': {
|
'include-level': {
|
||||||
...RULE_OPTION,
|
|
||||||
alias: ['l', 'level'],
|
alias: ['l', 'level'],
|
||||||
|
default: [] as Array<LogLevel>,
|
||||||
|
group: 'Rules:',
|
||||||
|
type: 'array',
|
||||||
},
|
},
|
||||||
'include-name': {
|
'include-name': {
|
||||||
...RULE_OPTION,
|
|
||||||
alias: ['n', 'name'],
|
alias: ['n', 'name'],
|
||||||
|
default: [] as Array<string>,
|
||||||
|
group: 'Rules:',
|
||||||
|
type: 'array',
|
||||||
},
|
},
|
||||||
'include-tag': {
|
'include-tag': {
|
||||||
...RULE_OPTION,
|
|
||||||
alias: ['t', 'tag'],
|
alias: ['t', 'tag'],
|
||||||
|
default: [] as Array<string>,
|
||||||
|
group: 'Rules:',
|
||||||
|
type: 'array',
|
||||||
},
|
},
|
||||||
'reporter': {
|
'reporter': {
|
||||||
alias: ['report'],
|
alias: ['report'],
|
||||||
|
@ -171,10 +178,7 @@ export async function parseArgs(argv: Array<string>): Promise<ParseResults> {
|
||||||
.version(VERSION_INFO.package.version)
|
.version(VERSION_INFO.package.version)
|
||||||
.alias('version', 'v');
|
.alias('version', 'v');
|
||||||
|
|
||||||
// @TODO: this should not need a cast but the parser's type omits command options and doesn't expose camelCase
|
const args = await parser.parse(argv);
|
||||||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
|
||||||
const args = parser.parse(argv) as any;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
args,
|
args,
|
||||||
mode,
|
mode,
|
||||||
|
|
Loading…
Reference in New Issue