From ab40330f610f07ac8760def50fa8023fc2d25951 Mon Sep 17 00:00:00 2001 From: ssube Date: Sun, 16 Jun 2019 16:35:18 -0500 Subject: [PATCH] feat(parser): serialize data via parser --- src/config/index.ts | 15 ++------------- src/config/schema.ts | 14 ++++++++++++++ src/index.ts | 8 ++------ src/parser/YamlParser.ts | 10 ++++++++-- src/parser/index.ts | 1 + 5 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 src/config/schema.ts diff --git a/src/config/index.ts b/src/config/index.ts index 9be3fa9..07ce883 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -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; /** diff --git a/src/config/schema.ts b/src/config/schema.ts new file mode 100644 index 0000000..c9adbd1 --- /dev/null +++ b/src/config/schema.ts @@ -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, +]); diff --git a/src/index.ts b/src/index.ts index 83c292b..e4851d9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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): Promise { } } 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); }); - diff --git a/src/parser/YamlParser.ts b/src/parser/YamlParser.ts index 4834c90..366e118 100644 --- a/src/parser/YamlParser.ts +++ b/src/parser/YamlParser.ts @@ -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, diff --git a/src/parser/index.ts b/src/parser/index.ts index 85157a7..4f8f58f 100644 --- a/src/parser/index.ts +++ b/src/parser/index.ts @@ -1,3 +1,4 @@ export interface Parser { + dump(data: any): string; parse(body: string): any; } \ No newline at end of file