#!/opt/local/bin/node

// Render a Vega-Lite specification to SVG
import {writeFile} from 'fs';
import {render} from './render.js';

const svgHeader =
  '<?xml version="1.0" encoding="utf-8"?>\n' +
  '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" ' +
  '"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n';

render('svg', (body, arg) => {
  const svg = (arg.h ? svgHeader : '') + body;
  const file = arg._[1] || null;

  if (file) {
    // write to file
    writeFile(file, svg, (err) => {
      if (err) throw err;
    });
  } else {
    // write to stdout
    process.stdout.write(svg);
  }
});
