Skip to content

Commit

Permalink
use dynamic import
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Nov 19, 2024
1 parent 9059240 commit 3222264
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions bin/vl2vg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

const {createWriteStream} = require('fs');
const vegaLite = require('..');
const compactStringify = require('json-stringify-pretty-compact');
const read = require('./read');
const args = require('./args');

Expand All @@ -13,13 +12,14 @@ const arg = args('vega');
// load spec, compile vg spec
read(arg._[0]).then(text => compile(JSON.parse(text)));

function compile(vlSpec) {
async function compile(vlSpec) {
// @ts-ignore
const vgSpec = vegaLite.compile(vlSpec).spec;

const file = arg._[1] || null;
const out = file ? createWriteStream(file) : process.stdout;
if (arg.p) {
const compactStringify = (await import('json-stringify-pretty-compact')).default;
out.write(compactStringify(vgSpec) + '\n');
} else {
out.write(JSON.stringify(vgSpec) + '\n');
Expand Down
21 changes: 11 additions & 10 deletions scripts/build-normalized-examples
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
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;
async function main() {
const {default: compactStringify} = await import('json-stringify-pretty-compact');

const vlExamples = examples.filter(example => example.includes('.vl.json'));
for (const example of vlExamples) {
const path = `${DIR + SPECS}/${example}`;
const spec = JSON.parse(fs.readFileSync(path));

const preNormalized = stableStringify(spec);
Expand All @@ -32,11 +34,10 @@ examples.forEach(example => {
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);
});
const newFilenameAndPath = `${DIR + SPECS}/normalized/${newFilename}`;
fs.writeFileSync(newFilenameAndPath, postNormalizedOutput);
}
}
});
}

main();

0 comments on commit 3222264

Please sign in to comment.