feat(parser): serialize data via parser
This commit is contained in:
parent
29f372daa8
commit
ab40330f61
|
@ -1,23 +1,12 @@
|
||||||
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';
|
||||||
|
|
||||||
import { envType } from 'src/config/type/Env';
|
import { CONFIG_ENV, CONFIG_SCHEMA } from 'src/config/schema';
|
||||||
import { includeSchema, includeType } from 'src/config/type/Include';
|
import { includeSchema } 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 { NotFoundError } from 'src/error/NotFoundError';
|
||||||
import { YamlParser } from 'src/parser/YamlParser';
|
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_SCHEMA = Schema.create([DEFAULT_SAFE_SCHEMA], [
|
|
||||||
envType,
|
|
||||||
includeType,
|
|
||||||
regexpType,
|
|
||||||
streamType,
|
|
||||||
]);
|
|
||||||
|
|
||||||
includeSchema.schema = CONFIG_SCHEMA;
|
includeSchema.schema = CONFIG_SCHEMA;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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,
|
||||||
|
]);
|
|
@ -1,8 +1,7 @@
|
||||||
import { createLogger } from 'bunyan';
|
import { createLogger } from 'bunyan';
|
||||||
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 { loadConfig } from 'src/config';
|
||||||
import { YamlParser } from 'src/parser/YamlParser';
|
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';
|
||||||
|
@ -108,9 +107,7 @@ export async function main(argv: Array<string>): Promise<number> {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.info('all rules passed');
|
logger.info('all rules passed');
|
||||||
const output = safeDump(data, {
|
const output = parser.dump(data);
|
||||||
schema: CONFIG_SCHEMA,
|
|
||||||
});
|
|
||||||
await writeSource(args.argv.dest, output);
|
await writeSource(args.argv.dest, output);
|
||||||
return STATUS_SUCCESS;
|
return STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
@ -121,4 +118,3 @@ main(process.argv).then((status) => process.exit(status)).catch((err) => {
|
||||||
console.error('uncaught error during main:', err);
|
console.error('uncaught error during main:', err);
|
||||||
process.exit(STATUS_ERROR);
|
process.exit(STATUS_ERROR);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -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';
|
import { Parser } from 'src/parser';
|
||||||
|
|
||||||
export class YamlParser implements Parser {
|
export class YamlParser implements Parser {
|
||||||
|
dump(data: any): string {
|
||||||
|
return safeDump(data, {
|
||||||
|
schema: CONFIG_SCHEMA,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
parse(body: string): any {
|
parse(body: string): any {
|
||||||
return safeLoad(body, {
|
return safeLoad(body, {
|
||||||
schema: CONFIG_SCHEMA,
|
schema: CONFIG_SCHEMA,
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
export interface Parser {
|
export interface Parser {
|
||||||
|
dump(data: any): string;
|
||||||
parse(body: string): any;
|
parse(body: string): any;
|
||||||
}
|
}
|
Loading…
Reference in New Issue