feat: write to stdout or file
This commit is contained in:
parent
c58a94d703
commit
a68975c624
12
Makefile
12
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
|
||||
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
|
|
@ -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'],
|
||||
},
|
||||
}),
|
||||
|
|
21
src/index.ts
21
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<string>): Promise<number> {
|
|||
}
|
||||
} else {
|
||||
logger.info('all rules passed');
|
||||
const output = safeDump(data, {
|
||||
schema: CONFIG_SCHEMA,
|
||||
});
|
||||
await writeSource(args.argv.dest, output);
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<string> {
|
||||
if (path === '-') {
|
||||
|
@ -10,3 +11,13 @@ export async function loadSource(path: string): Promise<string> {
|
|||
return readFileSync(path, 'utf-8');
|
||||
}
|
||||
}
|
||||
|
||||
export async function writeSource(path: string, data: string): Promise<void> {
|
||||
if (path === '-') {
|
||||
process.stdout.write(data);
|
||||
} else {
|
||||
return writeFileSync(path, data, {
|
||||
encoding: 'utf-8',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue