From 29f372daa8eae7c78f73781d3872f6d4826c96c7 Mon Sep 17 00:00:00 2001 From: ssube Date: Sun, 16 Jun 2019 16:15:01 -0500 Subject: [PATCH] fix: refactor parser into a single class --- config/rollup.js | 7 ++++++- src/config/index.ts | 8 ++++---- src/index.ts | 13 +++++++------ src/parser/YamlParser.ts | 12 ++++++++++++ src/parser/index.ts | 3 +++ src/rule.ts | 10 +++------- 6 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 src/parser/YamlParser.ts create mode 100644 src/parser/index.ts diff --git a/config/rollup.js b/config/rollup.js index c1114c5..8449c70 100644 --- a/config/rollup.js +++ b/config/rollup.js @@ -35,7 +35,12 @@ export default { }), commonjs({ namedExports: { - 'node_modules/lodash/lodash.js': ['cloneDeep', 'intersection', 'isNil', 'isString'], + 'node_modules/lodash/lodash.js': [ + 'cloneDeep', + 'intersection', + 'isNil', + 'isString', + ], 'node_modules/noicejs/out/main-bundle.js': ['BaseError'], 'node_modules/js-yaml/index.js': [ 'DEFAULT_SAFE_SCHEMA', diff --git a/src/config/index.ts b/src/config/index.ts index c6a319b..9be3fa9 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1,4 +1,4 @@ -import { DEFAULT_SAFE_SCHEMA, safeLoad, Schema } from 'js-yaml'; +import { DEFAULT_SAFE_SCHEMA, Schema } from 'js-yaml'; import { isNil, isString } from 'lodash'; import { join } from 'path'; @@ -7,6 +7,7 @@ import { includeSchema, includeType } from 'src/config/type/Include'; import { regexpType } from 'src/config/type/Regexp'; import { streamType } from 'src/config/type/Stream'; import { NotFoundError } from 'src/error/NotFoundError'; +import { YamlParser } from 'src/parser/YamlParser'; import { readFileSync } from 'src/source'; export const CONFIG_ENV = 'SALTY_HOME'; @@ -50,14 +51,13 @@ export function completePaths(name: string, extras: Array): Array): Promise { + const parser = new YamlParser(); const paths = completePaths(name, extras); for (const p of paths) { const data = await readConfig(p); if (!isNil(data)) { - return safeLoad(data, { - schema: CONFIG_SCHEMA, - }); + return parser.parse(data); } } diff --git a/src/index.ts b/src/index.ts index fd464ea..83c292b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,9 @@ import { createLogger } from 'bunyan'; -import { safeDump, safeLoad } from 'js-yaml'; +import { safeDump } from 'js-yaml'; import { detailed, Options } from 'yargs-parser'; import { CONFIG_SCHEMA, loadConfig } from 'src/config'; +import { YamlParser } from 'src/parser/YamlParser'; import { loadRules, resolveRules } from 'src/rule'; import { loadSource, writeSource } from 'src/source'; import { VERSION_INFO } from 'src/version'; @@ -76,13 +77,13 @@ export async function main(argv: Array): Promise { const rules = await loadRules(args.argv.rules); const source = await loadSource(args.argv.source); - const data = safeLoad(source, { - schema: CONFIG_SCHEMA, - }); - const activeRules = await resolveRules(rules, args.argv as any); - // run rules + const parser = new YamlParser(); + const data = parser.parse(source); + + const activeRules = await resolveRules(rules, args.argv as any); const ctx = new VisitorContext(logger); + switch (args.argv.mode) { case 'check': for (const rule of activeRules) { diff --git a/src/parser/YamlParser.ts b/src/parser/YamlParser.ts new file mode 100644 index 0000000..4834c90 --- /dev/null +++ b/src/parser/YamlParser.ts @@ -0,0 +1,12 @@ +import { safeLoad } from 'js-yaml'; + +import { CONFIG_SCHEMA } from 'src/config'; +import { Parser } from 'src/parser'; + +export class YamlParser implements Parser { + parse(body: string): any { + return safeLoad(body, { + schema: CONFIG_SCHEMA, + }); + } +} \ No newline at end of file diff --git a/src/parser/index.ts b/src/parser/index.ts new file mode 100644 index 0000000..85157a7 --- /dev/null +++ b/src/parser/index.ts @@ -0,0 +1,3 @@ +export interface Parser { + parse(body: string): any; +} \ No newline at end of file diff --git a/src/rule.ts b/src/rule.ts index 9f3d67e..e9290ae 100644 --- a/src/rule.ts +++ b/src/rule.ts @@ -1,12 +1,10 @@ import * as Ajv from 'ajv'; -import { safeLoad } from 'js-yaml'; import { JSONPath } from 'jsonpath-plus'; import { cloneDeep, intersection, isNil } from 'lodash'; import { LogLevel } from 'noicejs'; -import { CONFIG_SCHEMA } from 'src/config'; +import { YamlParser } from 'src/parser/YamlParser'; import { readFileSync } from 'src/source'; - import { Visitor } from 'src/visitor'; import { VisitorContext } from 'src/visitor/context'; @@ -32,6 +30,7 @@ export interface RuleSelector { } export async function loadRules(paths: Array): Promise> { + const parser = new YamlParser(); const rules = []; for (const path of paths) { @@ -39,10 +38,7 @@ export async function loadRules(paths: Array): Promise> { encoding: 'utf-8', }); - const data = safeLoad(contents, { - schema: CONFIG_SCHEMA, - }); - + const data = parser.parse(contents); rules.push(...data.rules.map((data: any) => new Rule(data))); }