feat: make fix mode mutations and defaults independent
This commit is contained in:
parent
88adb66466
commit
30ab437da3
|
@ -18,8 +18,10 @@ export interface Args {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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(): Args {
|
export function parseArgs(argv: Array<string>) {
|
||||||
let mode = 'check';
|
let mode = 'check';
|
||||||
|
|
||||||
const args = usage(`Usage: salty-dog <mode> [options]`)
|
const args = usage(`Usage: salty-dog <mode> [options]`)
|
||||||
|
@ -34,10 +36,15 @@ export function parseArgs(): Args {
|
||||||
command: ['fix'],
|
command: ['fix'],
|
||||||
describe: 'validate the source document and insert defaults',
|
describe: 'validate the source document and insert defaults',
|
||||||
builder: (yargs: any) => {
|
builder: (yargs: any) => {
|
||||||
return yargs.option('coerce', {
|
return yargs
|
||||||
default: false,
|
.option('coerce', {
|
||||||
type: 'boolean',
|
default: false,
|
||||||
});
|
type: 'boolean',
|
||||||
|
})
|
||||||
|
.option('defaults', {
|
||||||
|
default: true,
|
||||||
|
type: 'boolean',
|
||||||
|
});
|
||||||
},
|
},
|
||||||
handler: (argv: any) => {
|
handler: (argv: any) => {
|
||||||
mode = 'fix';
|
mode = 'fix';
|
||||||
|
|
15
src/index.ts
15
src/index.ts
|
@ -14,7 +14,7 @@ const STATUS_SUCCESS = 0;
|
||||||
const STATUS_ERROR = 1;
|
const STATUS_ERROR = 1;
|
||||||
|
|
||||||
export async function main(argv: Array<string>): Promise<number> {
|
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 config = await loadConfig(args[CONFIG_ARGS_NAME], ...args[CONFIG_ARGS_PATH]);
|
||||||
|
|
||||||
const logger = createLogger(config.data.logger);
|
const logger = createLogger(config.data.logger);
|
||||||
|
@ -27,18 +27,11 @@ export async function main(argv: Array<string>): Promise<number> {
|
||||||
return STATUS_ERROR;
|
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({
|
const ctx = new VisitorContext({
|
||||||
coerce,
|
coerce: args.coerce,
|
||||||
defaults: mode === 'fix',
|
defaults: args.defaults,
|
||||||
logger,
|
logger,
|
||||||
|
mutate: mode === 'fix',
|
||||||
});
|
});
|
||||||
|
|
||||||
const rules = await loadRules(args.rules, ctx.ajv);
|
const rules = await loadRules(args.rules, ctx.ajv);
|
||||||
|
|
|
@ -134,7 +134,9 @@ export async function visitRules(ctx: VisitorContext, rules: Array<Rule>, data:
|
||||||
rule: rule.name,
|
rule: rule.name,
|
||||||
}, 'rule passed with modifications');
|
}, 'rule passed with modifications');
|
||||||
|
|
||||||
applyDiff(item, itemCopy);
|
if (ctx.mutate) {
|
||||||
|
applyDiff(item, itemCopy);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
ctx.logger.info({ rule: rule.name }, 'rule passed');
|
ctx.logger.info({ rule: rule.name }, 'rule passed');
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ export interface VisitorContextOptions {
|
||||||
coerce: boolean;
|
coerce: boolean;
|
||||||
defaults: boolean;
|
defaults: boolean;
|
||||||
logger: Logger;
|
logger: Logger;
|
||||||
|
mutate: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class VisitorContext implements VisitorContextOptions, VisitorResult {
|
export class VisitorContext implements VisitorContextOptions, VisitorResult {
|
||||||
|
@ -16,6 +17,7 @@ export class VisitorContext implements VisitorContextOptions, VisitorResult {
|
||||||
public readonly defaults: boolean;
|
public readonly defaults: boolean;
|
||||||
public readonly errors: Array<any>;
|
public readonly errors: Array<any>;
|
||||||
public readonly logger: Logger;
|
public readonly logger: Logger;
|
||||||
|
public readonly mutate: boolean;
|
||||||
|
|
||||||
constructor(options: VisitorContextOptions) {
|
constructor(options: VisitorContextOptions) {
|
||||||
this.ajv = new ((Ajv as any).default)({
|
this.ajv = new ((Ajv as any).default)({
|
||||||
|
@ -27,6 +29,7 @@ export class VisitorContext implements VisitorContextOptions, VisitorResult {
|
||||||
this.defaults = options.defaults;
|
this.defaults = options.defaults;
|
||||||
this.errors = [];
|
this.errors = [];
|
||||||
this.logger = options.logger;
|
this.logger = options.logger;
|
||||||
|
this.mutate = options.mutate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public error(options: any, msg: string) {
|
public error(options: any, msg: string) {
|
||||||
|
|
Loading…
Reference in New Issue