From a68975c6241b2357db0e43c232a09a2b837cdc45 Mon Sep 17 00:00:00 2001 From: ssube Date: Sat, 15 Jun 2019 20:21:11 -0500 Subject: [PATCH] feat: write to stdout or file --- Makefile | 12 +++++++++++- config/rollup.js | 9 ++++++++- src/index.ts | 21 ++++++++++++++++----- src/source.ts | 13 ++++++++++++- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 55a4a1d..920dfc2 100755 --- a/Makefile +++ b/Makefile @@ -119,4 +119,14 @@ run-rules: ## validate the rules directory do \ echo "Validating $${file}..."; \ node out/bundle.js --rules $(ROOT_PATH)/rules/salty-dog.yml --source $${file} --tag important; \ - done \ No newline at end of file + done + +run-stream: ## validate stdin and write it to stdout, errors to stderr + @node out/bundle.js \ + --config-path $(ROOT_PATH)/docs --config-name config-stderr.yml \ + --dest - \ + --format yaml \ + --rules $(ROOT_PATH)/rules/kubernetes.yml \ + --source - \ + --tag important \ + --tag optional \ No newline at end of file diff --git a/config/rollup.js b/config/rollup.js index e770b7b..064b0d8 100644 --- a/config/rollup.js +++ b/config/rollup.js @@ -37,7 +37,14 @@ export default { namedExports: { 'node_modules/lodash/lodash.js': ['intersection', 'isNil', 'isString'], 'node_modules/noicejs/out/main-bundle.js': ['BaseError'], - 'node_modules/js-yaml/index.js': ['DEFAULT_SAFE_SCHEMA', 'SAFE_SCHEMA', 'safeLoad', 'Schema', 'Type'], + 'node_modules/js-yaml/index.js': [ + 'DEFAULT_SAFE_SCHEMA', + 'SAFE_SCHEMA', + 'safeDump', + 'safeLoad', + 'Schema', + 'Type', + ], 'node_modules/yargs-parser/index.js': ['detailed'], }, }), diff --git a/src/index.ts b/src/index.ts index bff1611..8f6fd53 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,11 @@ import { createLogger } from 'bunyan'; +import { safeDump, safeLoad } from 'js-yaml'; import { detailed, Options } from 'yargs-parser'; -import { loadConfig, CONFIG_SCHEMA } from 'src/config'; -import { loadRules, resolveRules, checkRule } from 'src/rule'; -import { loadSource } from 'src/source'; +import { CONFIG_SCHEMA, loadConfig } from 'src/config'; +import { checkRule, loadRules, resolveRules } from 'src/rule'; +import { loadSource, writeSource } from 'src/source'; import { VERSION_INFO } from 'src/version'; -import { safeLoad } from 'js-yaml'; const CONFIG_ARGS_NAME = 'config-name'; const CONFIG_ARGS_PATH = 'config-path'; @@ -13,8 +13,11 @@ const CONFIG_ARGS_PATH = 'config-path'; const MAIN_ARGS: Options = { alias: { 'count': ['c'], + 'dest': ['d'], + 'format': ['f'], 'includeTag': ['t', 'tag'], 'mode': ['m'], + 'source': ['s'], }, array: [ CONFIG_ARGS_PATH, @@ -34,9 +37,11 @@ const MAIN_ARGS: Options = { [CONFIG_ARGS_NAME]: `.${VERSION_INFO.app.name}.yml`, [CONFIG_ARGS_PATH]: [], 'count': false, + 'dest': '-', 'excludeLevel': [], 'excludeName': [], 'excludeTag': [], + 'format': 'yaml', 'includeLevel': [], 'includeName': [], 'includeTag': [], @@ -45,7 +50,9 @@ const MAIN_ARGS: Options = { 'source': '-', }, envPrefix: VERSION_INFO.app.name, - string: ['mode'], + string: [ + 'mode', + ], }; const STATUS_SUCCESS = 0; @@ -100,6 +107,10 @@ export async function main(argv: Array): Promise { } } else { logger.info('all rules passed'); + const output = safeDump(data, { + schema: CONFIG_SCHEMA, + }); + await writeSource(args.argv.dest, output); return STATUS_SUCCESS; } } diff --git a/src/source.ts b/src/source.ts index b44a3b8..f4ae3da 100644 --- a/src/source.ts +++ b/src/source.ts @@ -1,7 +1,8 @@ import { promisify } from 'util'; -import { readFile } from 'fs'; +import { readFile, writeFile } from 'fs'; const readFileSync = promisify(readFile); +const writeFileSync = promisify(writeFile); export async function loadSource(path: string): Promise { if (path === '-') { @@ -10,3 +11,13 @@ export async function loadSource(path: string): Promise { return readFileSync(path, 'utf-8'); } } + +export async function writeSource(path: string, data: string): Promise { + if (path === '-') { + process.stdout.write(data); + } else { + return writeFileSync(path, data, { + encoding: 'utf-8', + }); + } +}