#!/usr/bin/env node
const fs = require('fs');
const path = require('path');

const rootDir = path.resolve(__dirname, '..');
const srcDir = path.join(rootDir, 'src');
const outDir = path.join(rootDir, 'www');

function fail(message) {
  console.error(`error: ${message}`);
  process.exit(1);
}

if (!fs.existsSync(srcDir)) {
  fail(`missing source directory: ${srcDir}`);
}

try {
  fs.rmSync(outDir, { recursive: true, force: true });
  fs.mkdirSync(outDir, { recursive: true });
  fs.cpSync(srcDir, outDir, { recursive: true });

  console.log(`Built web assets: ${path.relative(rootDir, outDir)}`);
} catch (error) {
  fail(error && error.message ? error.message : String(error));
}
