#!/opt/local/bin/node

// Compile a Vega-Lite spec to Vega

import {createWriteStream} from 'fs';
import {compile as compileVL} from '../build/index.js';
import read from './read.js';
import args from './args.js';

const arg = args('vega');

// load spec, compile vg spec
read(arg._[0]).then((text) => compile(JSON.parse(text)));

async function compile(vlSpec) {
  const vgSpec = compileVL(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');
  }
}
