fix: refactor parser into a single class
This commit is contained in:
parent
9a25fb97a8
commit
29f372daa8
|
@ -35,7 +35,12 @@ export default {
|
||||||
}),
|
}),
|
||||||
commonjs({
|
commonjs({
|
||||||
namedExports: {
|
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/noicejs/out/main-bundle.js': ['BaseError'],
|
||||||
'node_modules/js-yaml/index.js': [
|
'node_modules/js-yaml/index.js': [
|
||||||
'DEFAULT_SAFE_SCHEMA',
|
'DEFAULT_SAFE_SCHEMA',
|
||||||
|
|
|
@ -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 { isNil, isString } from 'lodash';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import { includeSchema, includeType } from 'src/config/type/Include';
|
||||||
import { regexpType } from 'src/config/type/Regexp';
|
import { regexpType } from 'src/config/type/Regexp';
|
||||||
import { streamType } from 'src/config/type/Stream';
|
import { streamType } from 'src/config/type/Stream';
|
||||||
import { NotFoundError } from 'src/error/NotFoundError';
|
import { NotFoundError } from 'src/error/NotFoundError';
|
||||||
|
import { YamlParser } from 'src/parser/YamlParser';
|
||||||
import { readFileSync } from 'src/source';
|
import { readFileSync } from 'src/source';
|
||||||
|
|
||||||
export const CONFIG_ENV = 'SALTY_HOME';
|
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> {
|
export async function loadConfig(name: string, ...extras: Array<string>): Promise<any> {
|
||||||
|
const parser = new YamlParser();
|
||||||
const paths = completePaths(name, extras);
|
const paths = completePaths(name, extras);
|
||||||
|
|
||||||
for (const p of paths) {
|
for (const p of paths) {
|
||||||
const data = await readConfig(p);
|
const data = await readConfig(p);
|
||||||
if (!isNil(data)) {
|
if (!isNil(data)) {
|
||||||
return safeLoad(data, {
|
return parser.parse(data);
|
||||||
schema: CONFIG_SCHEMA,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
13
src/index.ts
13
src/index.ts
|
@ -1,8 +1,9 @@
|
||||||
import { createLogger } from 'bunyan';
|
import { createLogger } from 'bunyan';
|
||||||
import { safeDump, safeLoad } from 'js-yaml';
|
import { safeDump } from 'js-yaml';
|
||||||
import { detailed, Options } from 'yargs-parser';
|
import { detailed, Options } from 'yargs-parser';
|
||||||
|
|
||||||
import { CONFIG_SCHEMA, loadConfig } from 'src/config';
|
import { CONFIG_SCHEMA, loadConfig } from 'src/config';
|
||||||
|
import { YamlParser } from 'src/parser/YamlParser';
|
||||||
import { loadRules, resolveRules } from 'src/rule';
|
import { loadRules, resolveRules } from 'src/rule';
|
||||||
import { loadSource, writeSource } from 'src/source';
|
import { loadSource, writeSource } from 'src/source';
|
||||||
import { VERSION_INFO } from 'src/version';
|
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 rules = await loadRules(args.argv.rules);
|
||||||
const source = await loadSource(args.argv.source);
|
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);
|
const ctx = new VisitorContext(logger);
|
||||||
|
|
||||||
switch (args.argv.mode) {
|
switch (args.argv.mode) {
|
||||||
case 'check':
|
case 'check':
|
||||||
for (const rule of activeRules) {
|
for (const rule of activeRules) {
|
||||||
|
|
|
@ -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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
export interface Parser {
|
||||||
|
parse(body: string): any;
|
||||||
|
}
|
10
src/rule.ts
10
src/rule.ts
|
@ -1,12 +1,10 @@
|
||||||
import * as Ajv from 'ajv';
|
import * as Ajv from 'ajv';
|
||||||
import { safeLoad } from 'js-yaml';
|
|
||||||
import { JSONPath } from 'jsonpath-plus';
|
import { JSONPath } from 'jsonpath-plus';
|
||||||
import { cloneDeep, intersection, isNil } from 'lodash';
|
import { cloneDeep, intersection, isNil } from 'lodash';
|
||||||
import { LogLevel } from 'noicejs';
|
import { LogLevel } from 'noicejs';
|
||||||
|
|
||||||
import { CONFIG_SCHEMA } from 'src/config';
|
import { YamlParser } from 'src/parser/YamlParser';
|
||||||
import { readFileSync } from 'src/source';
|
import { readFileSync } from 'src/source';
|
||||||
|
|
||||||
import { Visitor } from 'src/visitor';
|
import { Visitor } from 'src/visitor';
|
||||||
import { VisitorContext } from 'src/visitor/context';
|
import { VisitorContext } from 'src/visitor/context';
|
||||||
|
|
||||||
|
@ -32,6 +30,7 @@ export interface RuleSelector {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loadRules(paths: Array<string>): Promise<Array<Rule>> {
|
export async function loadRules(paths: Array<string>): Promise<Array<Rule>> {
|
||||||
|
const parser = new YamlParser();
|
||||||
const rules = [];
|
const rules = [];
|
||||||
|
|
||||||
for (const path of paths) {
|
for (const path of paths) {
|
||||||
|
@ -39,10 +38,7 @@ export async function loadRules(paths: Array<string>): Promise<Array<Rule>> {
|
||||||
encoding: 'utf-8',
|
encoding: 'utf-8',
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = safeLoad(contents, {
|
const data = parser.parse(contents);
|
||||||
schema: CONFIG_SCHEMA,
|
|
||||||
});
|
|
||||||
|
|
||||||
rules.push(...data.rules.map((data: any) => new Rule(data)));
|
rules.push(...data.rules.map((data: any) => new Rule(data)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue