2019-11-09 23:58:38 +00:00
|
|
|
import { createLogger } from 'bunyan';
|
|
|
|
import { showCompletionScript } from 'yargs';
|
|
|
|
|
|
|
|
import { loadConfig } from './config';
|
2019-11-14 12:02:55 +00:00
|
|
|
import { CONFIG_ARGS_NAME, CONFIG_ARGS_PATH, MODE, parseArgs } from './config/args';
|
2019-11-09 23:58:38 +00:00
|
|
|
import { YamlParser } from './parser/YamlParser';
|
2019-11-17 18:07:02 +00:00
|
|
|
import { createRuleSelector, createRuleSources, loadRules, resolveRules, validateConfig } from './rule';
|
2019-11-14 13:56:49 +00:00
|
|
|
import { RuleVisitor } from './rule/RuleVisitor';
|
2019-11-14 01:32:20 +00:00
|
|
|
import { readSource, writeSource } from './source';
|
2019-11-09 23:58:38 +00:00
|
|
|
import { VERSION_INFO } from './version';
|
|
|
|
import { VisitorContext } from './visitor/VisitorContext';
|
|
|
|
|
2019-11-19 12:03:48 +00:00
|
|
|
const ARGS_START = 2;
|
2019-11-10 03:56:49 +00:00
|
|
|
export const STATUS_SUCCESS = 0;
|
|
|
|
export const STATUS_ERROR = 1;
|
|
|
|
export const STATUS_MAX = 255;
|
2019-11-09 23:58:38 +00:00
|
|
|
|
|
|
|
export async function main(argv: Array<string>): Promise<number> {
|
2019-11-19 12:03:48 +00:00
|
|
|
const { args, mode } = await parseArgs(argv.slice(ARGS_START));
|
2019-11-09 23:58:38 +00:00
|
|
|
if (mode === MODE.complete) {
|
|
|
|
showCompletionScript();
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
const config = await loadConfig(args[CONFIG_ARGS_NAME], ...args[CONFIG_ARGS_PATH]);
|
|
|
|
|
|
|
|
const logger = createLogger(config.data.logger);
|
|
|
|
logger.info(VERSION_INFO, 'version info');
|
|
|
|
logger.info({ args, mode }, 'main arguments');
|
|
|
|
|
2019-11-14 12:02:55 +00:00
|
|
|
// load rules
|
2019-11-09 23:58:38 +00:00
|
|
|
const ctx = new VisitorContext({
|
2019-11-12 14:25:09 +00:00
|
|
|
logger,
|
|
|
|
schemaOptions: {
|
2019-11-09 23:58:38 +00:00
|
|
|
coerce: args.coerce,
|
|
|
|
defaults: args.defaults,
|
2019-11-12 14:26:31 +00:00
|
|
|
mutate: args.mutate,
|
2019-11-09 23:58:38 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-11-17 18:07:02 +00:00
|
|
|
if (!validateConfig(ctx, config)) {
|
|
|
|
logger.error('config was not valid according to embedded schema');
|
|
|
|
return STATUS_ERROR;
|
|
|
|
}
|
|
|
|
|
2019-11-09 23:58:38 +00:00
|
|
|
const ruleSelector = createRuleSelector(args);
|
|
|
|
const ruleSources = createRuleSources(args);
|
|
|
|
|
|
|
|
const loadedRules = await loadRules(ruleSources, ctx);
|
|
|
|
const activeRules = await resolveRules(loadedRules, ruleSelector);
|
|
|
|
|
|
|
|
if (mode === MODE.list) {
|
|
|
|
logger.info({
|
|
|
|
activeCount: activeRules.length,
|
|
|
|
activeRules,
|
|
|
|
loadedCount: loadedRules.length,
|
|
|
|
loadedRules,
|
|
|
|
ruleSelector,
|
|
|
|
ruleSources,
|
|
|
|
}, 'listing active rules');
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-11-14 12:02:55 +00:00
|
|
|
// load source
|
2019-11-09 23:58:38 +00:00
|
|
|
const parser = new YamlParser();
|
2019-11-14 01:32:20 +00:00
|
|
|
const source = await readSource(args.source);
|
2019-11-09 23:58:38 +00:00
|
|
|
const docs = parser.parse(source);
|
|
|
|
|
2019-11-14 13:56:49 +00:00
|
|
|
const visitor = new RuleVisitor({
|
|
|
|
rules: activeRules,
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const root of docs) {
|
|
|
|
await visitor.visit(ctx, root);
|
2019-11-09 23:58:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx.errors.length === 0) {
|
|
|
|
logger.info('all rules passed');
|
|
|
|
const output = parser.dump(...docs);
|
|
|
|
await writeSource(args.dest, output);
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.error({ count: ctx.errors.length, errors: ctx.errors }, 'some rules failed');
|
|
|
|
if (args.count) {
|
|
|
|
return Math.min(ctx.errors.length, STATUS_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_ERROR;
|
|
|
|
}
|