Skip to content

Commit 36667d1

Browse files
author
Mark Matyas
committed
Add term colors and pretty print yaml
Signed-off-by: Mark Matyas <[email protected]>
1 parent 7ab1c6f commit 36667d1

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

scripts/build.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@ const glob = require('glob');
55
const yaml = require('js-yaml');
66
const fs = require('fs');
77
const {log} = require('console');
8+
89
const ERROR = 1;
910
const isCLI = require.main === module;
11+
const colors = {
12+
red: '\x1b[31m',
13+
cyan: '\x1b[36m',
14+
green: '\x1b[32m',
15+
yellow: '\x1b[33m'
16+
};
1017

1118
// @todo only load new/changed yaml - use git diff
1219
function getYamlFilePaths() {
1320
return glob.sync('{**/*.yaml,**/*.yml}');
1421
}
1522

1623
function failure(error = 'Error') {
17-
log(`${error}`);
24+
log(colors.red, `${error}`);
1825
process.exitCode = ERROR;
1926
return ERROR;
2027
}
@@ -24,23 +31,25 @@ function loadYamlFile(path) {
2431
let data = yaml.safeLoad(fs.readFileSync(path, 'utf8'));
2532
return {data, path};
2633
} catch (error) {
27-
return failure(`> Invalid yaml file: ${path}\n`);
34+
return failure(`Invalid yaml file: ${path}\n`);
2835
}
2936
}
3037

3138
function logYamlDoc({data, path}) {
32-
log(path);
33-
log(data);
39+
log(colors.cyan, path);
40+
yaml.safeDump(data).split('\n').forEach(line => log(colors.green, line));
3441
}
3542

3643
function run() {
3744
let paths = getYamlFilePaths();
3845
if (!paths.length) {
39-
return failure('> 0 yaml files');
46+
return failure('0 yaml files');
4047
}
4148

4249
let docs = paths.map(loadYamlFile).filter(x => x !== ERROR);
43-
log('> Valid yaml file(s):\n');
50+
if (docs.length) {
51+
log(colors.yellow, 'Valid yaml file(s):');
52+
}
4453
docs.forEach(logYamlDoc);
4554
}
4655

0 commit comments

Comments
 (0)