1
0
Fork 0

feat: make fix mode mutations and defaults independent

This commit is contained in:
ssube 2019-07-04 13:58:25 -05:00
parent 88adb66466
commit 30ab437da3
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
4 changed files with 22 additions and 17 deletions

View File

@ -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<string>) {
let mode = 'check';
const args = usage(`Usage: salty-dog <mode> [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';

View File

@ -14,7 +14,7 @@ const STATUS_SUCCESS = 0;
const STATUS_ERROR = 1;
export async function main(argv: Array<string>): Promise<number> {
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<string>): Promise<number> {
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);

View File

@ -134,7 +134,9 @@ export async function visitRules(ctx: VisitorContext, rules: Array<Rule>, 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');
}

View File

@ -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<any>;
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) {