Skip to content

Commit

Permalink
feat: count errors, improve error messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jun 15, 2019
1 parent 5e05c72 commit f50f2f1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
13 changes: 2 additions & 11 deletions Makefile
Expand Up @@ -42,7 +42,7 @@ RELEASE_OPTS ?= --commit-all
export NODE_VERSION := $(shell node -v)
export RUNNER_VERSION := $(CI_RUNNER_VERSION)

all: build run-terminal
all: build

clean: ## clean up the target directory
rm -rf node_modules
Expand Down Expand Up @@ -74,16 +74,7 @@ todo:

# build targets
build: ## builds, bundles, and tests the application
build: build-fast

build-cover: ## builds, bundles, and tests the application with code coverage
build-cover: configure node_modules

build-fast: ## builds, bundles, and tests the application
build-fast: configure node_modules

build-strict: ## builds, bundles, and tests the application with type checks and extra warnings (slow)
build-strict: configure node_modules
build: bundle

bundle: node_modules
$(NODE_BIN)/rollup --config $(CONFIG_PATH)/rollup.js
Expand Down
24 changes: 19 additions & 5 deletions src/index.ts
Expand Up @@ -12,6 +12,7 @@ const CONFIG_ARGS_PATH = 'config-path';

const MAIN_ARGS: Options = {
alias: {
'count': ['c'],
'includeTag': ['t', 'tag'],
'mode': ['m'],
},
Expand All @@ -25,10 +26,14 @@ const MAIN_ARGS: Options = {
'includeTag',
'rules',
],
boolean: [
'count',
],
count: ['v'],
default: {
[CONFIG_ARGS_NAME]: `.${VERSION_INFO.app.name}.yml`,
[CONFIG_ARGS_PATH]: [],
'count': false,
'excludeLevel': [],
'excludeName': [],
'excludeTag': [],
Expand Down Expand Up @@ -69,24 +74,33 @@ export async function main(argv: Array<string>): Promise<number> {
const activeRules = await resolveRules(rules, args.argv as any);

// run rules
let status = STATUS_SUCCESS;
let errors = 0;
switch (args.argv.mode) {
case 'check':
for (const rule of activeRules) {
if (checkRule(rule, data)) {
if (checkRule(rule, data, logger)) {
logger.info({ rule }, 'passed rule');
} else {
logger.warn({ rule }, 'failed rule');
status = STATUS_ERROR;
++errors;
}
}
break;
default:
logger.error({ mode: args.argv.mode }, 'unsupported mode');
status = STATUS_ERROR;
++errors;
}

return status;
if (errors > 0) {
logger.error({ errors }, 'some rules failed');
if (args.argv.count) {
return errors;
} else {
return STATUS_ERROR;
}
} else {
return STATUS_SUCCESS;
}
}

main(process.argv).then((status) => process.exit(status)).catch((err) => {
Expand Down
15 changes: 12 additions & 3 deletions src/rule.ts
Expand Up @@ -2,7 +2,7 @@ import * as Ajv from 'ajv';
import { readFile } from 'fs';
import { JSONPath } from 'jsonpath-plus';
import { intersection } from 'lodash';
import { LogLevel } from 'noicejs';
import { LogLevel, Logger } from 'noicejs';
import { promisify } from 'util';
import { safeLoad } from 'js-yaml';
import { CONFIG_SCHEMA } from './config';
Expand Down Expand Up @@ -81,12 +81,21 @@ export async function resolveRules(rules: Array<Rule>, selector: RuleSelector):
return Array.from(activeRules);
}

export function checkRule(rule: Rule, data: any): boolean {
export function checkRule(rule: Rule, data: any, logger: Logger): boolean {
const ajv = new ((Ajv as any).default)()
const schema = ajv.compile(rule.schema);
const scopes = JSONPath({
json: data,
path: rule.nodes.select,
});
return scopes.every((s: any) => schema(s));

for (const scope of scopes) {
const valid = schema(scope);
if (!valid) {
logger.warn({ errors: schema.errors, item: scope }, 'rule failed on item');
return false;
}
}

return true;
}

0 comments on commit f50f2f1

Please sign in to comment.