1
0
Fork 0

fix: refactor parser into a single class

This commit is contained in:
ssube 2019-06-16 16:15:01 -05:00
parent 9a25fb97a8
commit 29f372daa8
6 changed files with 35 additions and 18 deletions

View File

@ -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',

View File

@ -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<string>): Array<string
}
export async function loadConfig(name: string, ...extras: Array<string>): Promise<any> {
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);
}
}

View File

@ -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<string>): Promise<number> {
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) {

12
src/parser/YamlParser.ts Normal file
View File

@ -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,
});
}
}

3
src/parser/index.ts Normal file
View File

@ -0,0 +1,3 @@
export interface Parser {
parse(body: string): any;
}

View File

@ -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<string>): Promise<Array<Rule>> {
const parser = new YamlParser();
const rules = [];
for (const path of paths) {
@ -39,10 +38,7 @@ export async function loadRules(paths: Array<string>): Promise<Array<Rule>> {
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)));
}