-
Notifications
You must be signed in to change notification settings - Fork 592
/
build-normalized-examples
executable file
·42 lines (36 loc) · 1.47 KB
/
build-normalized-examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env node
'use strict';
/*
* This script normalizes all the specs in examples/specs and
* writes the full normalized spec to `examples/specs/normalized`
* the normalized spec is different than the original.
* We output this to a different folder so that build-examples script does not generate
* redundant Vega output specs.
*/
const fs = require('fs');
const vl = require('../build/vega-lite');
const stableStringify = require('fast-json-stable-stringify');
const compactStringify = require('json-stringify-pretty-compact');
const DIR = __dirname + '/..';
const SPECS = '/examples/specs';
const examples = fs.readdirSync(DIR + SPECS);
examples.forEach(example => {
if (example && example.includes('.vl.json')) {
const path = DIR + SPECS + '/' + example;
const spec = JSON.parse(fs.readFileSync(path));
const preNormalized = stableStringify(spec);
const fullSpec = vl.normalize(spec);
const postNormalized = stableStringify(fullSpec);
// console.log(preNormalized, postNormalized);
if (preNormalized !== postNormalized) {
const postNormalizedOutput = compactStringify(fullSpec, null, 2);
// -8 is cutting .vl.json
const newFilename = example.slice(0, -8) + '_normalized.vl.json';
const newFilenameAndPath = DIR + SPECS + '/normalized/' + newFilename;
fs.writeFile(newFilenameAndPath, postNormalizedOutput, err => {
if (err) return console.log(err);
console.log('Built:', newFilename);
});
}
}
});