Skip to content

Commit

Permalink
fix: refactor parser into a single class
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jun 16, 2019
1 parent 9a25fb9 commit 29f372d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
7 changes: 6 additions & 1 deletion config/rollup.js
Expand Up @@ -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',
Expand Down
8 changes: 4 additions & 4 deletions 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';

Expand All @@ -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';
Expand Down Expand Up @@ -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);
}
}

Expand Down
13 changes: 7 additions & 6 deletions 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';
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions 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,
});
}
}
3 changes: 3 additions & 0 deletions src/parser/index.ts
@@ -0,0 +1,3 @@
export interface Parser {
parse(body: string): any;
}
10 changes: 3 additions & 7 deletions 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';

Expand All @@ -32,17 +30,15 @@ export interface RuleSelector {
}

export async function loadRules(paths: Array<string>): Promise<Array<Rule>> {
const parser = new YamlParser();
const rules = [];

for (const path of paths) {
const contents = await readFileSync(path, {
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)));
}

Expand Down

0 comments on commit 29f372d

Please sign in to comment.