From 30ab437da366465e2e64d6470ae534adc9e27440 Mon Sep 17 00:00:00 2001 From: ssube Date: Thu, 4 Jul 2019 13:58:25 -0500 Subject: [PATCH] feat: make fix mode mutations and defaults independent --- src/config/args.ts | 17 ++++++++++++----- src/index.ts | 15 ++++----------- src/rule.ts | 4 +++- src/visitor/context.ts | 3 +++ 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/config/args.ts b/src/config/args.ts index 44f1c66..3652b12 100644 --- a/src/config/args.ts +++ b/src/config/args.ts @@ -18,8 +18,10 @@ export interface Args { /** * Wrap yargs to exit after completion. + * + * @TODO: fix it to use argv, not sure if yargs can do that */ -export function parseArgs(): Args { +export function parseArgs(argv: Array) { let mode = 'check'; const args = usage(`Usage: salty-dog [options]`) @@ -34,10 +36,15 @@ export function parseArgs(): Args { command: ['fix'], describe: 'validate the source document and insert defaults', builder: (yargs: any) => { - return yargs.option('coerce', { - default: false, - type: 'boolean', - }); + return yargs + .option('coerce', { + default: false, + type: 'boolean', + }) + .option('defaults', { + default: true, + type: 'boolean', + }); }, handler: (argv: any) => { mode = 'fix'; diff --git a/src/index.ts b/src/index.ts index 7f85bba..4705633 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,7 +14,7 @@ const STATUS_SUCCESS = 0; const STATUS_ERROR = 1; export async function main(argv: Array): Promise { - const { args, mode } = parseArgs(); + const { args, mode } = parseArgs(argv); const config = await loadConfig(args[CONFIG_ARGS_NAME], ...args[CONFIG_ARGS_PATH]); const logger = createLogger(config.data.logger); @@ -27,18 +27,11 @@ export async function main(argv: Array): Promise { return STATUS_ERROR; } - // const schema = new Schema(); - const result = { errors: [], valid: true }; // schema.match(config); - if (!result.valid) { - logger.error({ errors: result.errors }, 'config failed to validate'); - return STATUS_ERROR; - } - - const coerce = Reflect.has(args, 'coerce') ? Reflect.get(args, 'coerce') : false; const ctx = new VisitorContext({ - coerce, - defaults: mode === 'fix', + coerce: args.coerce, + defaults: args.defaults, logger, + mutate: mode === 'fix', }); const rules = await loadRules(args.rules, ctx.ajv); diff --git a/src/rule.ts b/src/rule.ts index 60cc4d7..85c1377 100644 --- a/src/rule.ts +++ b/src/rule.ts @@ -134,7 +134,9 @@ export async function visitRules(ctx: VisitorContext, rules: Array, data: rule: rule.name, }, 'rule passed with modifications'); - applyDiff(item, itemCopy); + if (ctx.mutate) { + applyDiff(item, itemCopy); + } } else { ctx.logger.info({ rule: rule.name }, 'rule passed'); } diff --git a/src/visitor/context.ts b/src/visitor/context.ts index 2deee38..2242669 100644 --- a/src/visitor/context.ts +++ b/src/visitor/context.ts @@ -7,6 +7,7 @@ export interface VisitorContextOptions { coerce: boolean; defaults: boolean; logger: Logger; + mutate: boolean; } export class VisitorContext implements VisitorContextOptions, VisitorResult { @@ -16,6 +17,7 @@ export class VisitorContext implements VisitorContextOptions, VisitorResult { public readonly defaults: boolean; public readonly errors: Array; public readonly logger: Logger; + public readonly mutate: boolean; constructor(options: VisitorContextOptions) { this.ajv = new ((Ajv as any).default)({ @@ -27,6 +29,7 @@ export class VisitorContext implements VisitorContextOptions, VisitorResult { this.defaults = options.defaults; this.errors = []; this.logger = options.logger; + this.mutate = options.mutate; } public error(options: any, msg: string) {