const fs = require('fs-extra');
const ch = require('child_process');
const SCENE = process.env.SCENE;

fs.ensureDirSync('.temp');
fs.emptyDirSync('.temp');

const distDir = `build${!!SCENE ? '/' + SCENE : ''}`;

ch.execSync(`npm run build${!!SCENE ? ':' + SCENE : ''}:test`, {
  stdio: ['ignore', 'inherit', 'inherit'],
});

const distFiles = fs.readdirSync(distDir);

if (!distFiles.length) throw new Error(`请先执行 yarn ${distDir}`);

distFiles.forEach((fileName) => {
  fs.copySync(
    `${distDir}/${fileName}`,
    `.temp/${fileName === 'resources' ? fileName : 'data/' + fileName}`
  );
});
