const fs = require('fs-extra')
const path = require('path')
const { execSync } = require('child_process')

const rootPath = path.resolve(__dirname, '../')

const distPath = path.resolve(rootPath, 'dist')

function clean() {
  fs.removeSync(distPath)
}

function execTSC() {
  const tsPath = require.resolve('typescript')
  const tscBinary = path.resolve(tsPath, '../../../.bin/tsc')
  try {
    execSync(`${tscBinary} -p ./tsconfig.json`, {
      cwd: path.resolve(__dirname, '../')
    })
  } catch (e) {
    console.error(e.stdout.toString())
  }
}

function copyStatic() {
  fs.copySync(
    path.resolve(rootPath, 'src/public'),
    path.resolve(distPath, 'public')
  )
}

clean()
execTSC()
copyStatic()