1
0
Fork 0

feat(parser): serialize data via parser

This commit is contained in:
ssube 2019-06-16 16:35:18 -05:00
parent 29f372daa8
commit ab40330f61
5 changed files with 27 additions and 21 deletions

View File

@ -1,23 +1,12 @@
import { DEFAULT_SAFE_SCHEMA, Schema } from 'js-yaml';
import { isNil, isString } from 'lodash';
import { join } from 'path';
import { envType } from 'src/config/type/Env';
import { includeSchema, includeType } from 'src/config/type/Include';
import { regexpType } from 'src/config/type/Regexp';
import { streamType } from 'src/config/type/Stream';
import { CONFIG_ENV, CONFIG_SCHEMA } from 'src/config/schema';
import { includeSchema } from 'src/config/type/Include';
import { NotFoundError } from 'src/error/NotFoundError';
import { YamlParser } from 'src/parser/YamlParser';
import { readFileSync } from 'src/source';
export const CONFIG_ENV = 'SALTY_HOME';
export const CONFIG_SCHEMA = Schema.create([DEFAULT_SAFE_SCHEMA], [
envType,
includeType,
regexpType,
streamType,
]);
includeSchema.schema = CONFIG_SCHEMA;
/**

14
src/config/schema.ts Normal file
View File

@ -0,0 +1,14 @@
import { DEFAULT_SAFE_SCHEMA, Schema } from 'js-yaml';
import { envType } from 'src/config/type/Env';
import { includeType } from 'src/config/type/Include';
import { regexpType } from 'src/config/type/Regexp';
import { streamType } from 'src/config/type/Stream';
export const CONFIG_ENV = 'SALTY_HOME';
export const CONFIG_SCHEMA = Schema.create([DEFAULT_SAFE_SCHEMA], [
envType,
includeType,
regexpType,
streamType,
]);

View File

@ -1,8 +1,7 @@
import { createLogger } from 'bunyan';
import { safeDump } from 'js-yaml';
import { detailed, Options } from 'yargs-parser';
import { CONFIG_SCHEMA, loadConfig } from 'src/config';
import { loadConfig } from 'src/config';
import { YamlParser } from 'src/parser/YamlParser';
import { loadRules, resolveRules } from 'src/rule';
import { loadSource, writeSource } from 'src/source';
@ -108,9 +107,7 @@ export async function main(argv: Array<string>): Promise<number> {
}
} else {
logger.info('all rules passed');
const output = safeDump(data, {
schema: CONFIG_SCHEMA,
});
const output = parser.dump(data);
await writeSource(args.argv.dest, output);
return STATUS_SUCCESS;
}
@ -121,4 +118,3 @@ main(process.argv).then((status) => process.exit(status)).catch((err) => {
console.error('uncaught error during main:', err);
process.exit(STATUS_ERROR);
});

View File

@ -1,9 +1,15 @@
import { safeLoad } from 'js-yaml';
import { safeDump, safeLoad } from 'js-yaml';
import { CONFIG_SCHEMA } from 'src/config';
import { CONFIG_SCHEMA } from 'src/config/schema';
import { Parser } from 'src/parser';
export class YamlParser implements Parser {
dump(data: any): string {
return safeDump(data, {
schema: CONFIG_SCHEMA,
});
}
parse(body: string): any {
return safeLoad(body, {
schema: CONFIG_SCHEMA,

View File

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