text: #!/usr/bin/env node
  // Copyright 2017-2020 @polkadot/dev authors & contributors
  // This software may be modified and distributed under the terms
  // of the Apache-2.0 license. See the LICENSE file for details.
  
  const babel = require('@babel/cli/lib/babel/dir').default;
  const execSync = require('./execSync');
  const cpx = require('cpx');
  const fs = require('fs');
  const mkdirp = require('mkdirp');
  const path = require('path');
  
  console.log(__dirname);
  
  const CPX = ['css', 'gif', 'hbs', 'jpg', 'js', 'png', 'svg', 'd.ts']
    .map((ext) => `src/**/*.${ext}`)
    .concat('package.json');
  
  console.log('$ polkadot-dev-build-ts', process.argv.slice(2).join(' '));
  
  function buildWebpack () {
    execSync('yarn polkadot-exec-webpack --config webpack.config.js --mode production');
  }
  
  async function buildBabel (dir) {
    await babel({
      babelOptions: {
        configFile: path.join(process.cwd(), '../../babel.config.js')
      },
      cliOptions: {
        extensions: ['.ts', '.tsx'],
        filenames: ['src'],
        ignore: '**/*.d.ts',
        outDir: path.join(process.cwd(), 'build')
      }
    });
  
    [...CPX]
      .concat(`../../build/${dir}/src/**/*.d.ts`, `../../build/packages/${dir}/src/**/*.d.ts`)
      .forEach((src) => cpx.copySync(src, 'build'));
  }
  
  async function buildJs (dir) {
    if (!fs.existsSync(path.join(process.cwd(), '.skip-build'))) {
      const { name, version } = require(path.join(process.cwd(), './package.json'));
  
      if (!name.startsWith('@chainx-v2/')) {
        return;
      }
  
      console.log(`*** ${name} ${version}`);
  
      mkdirp.sync('build');
  
      if (fs.existsSync(path.join(process.cwd(), 'public'))) {
        buildWebpack(dir);
      } else {
        await buildBabel(dir);
      }
  
      console.log();
    }
  }
  
  async function main () {
    execSync('yarn polkadot-dev-clean-build');
  
    process.chdir('packages');
  
    execSync('yarn polkadot-exec-tsc --emitDeclarationOnly --outdir ../build');
  
    const dirs = fs
      .readdirSync('.')
      .filter((dir) => fs.statSync(dir).isDirectory() && fs.existsSync(path.join(process.cwd(), dir, 'src')));
  
    for (const dir of dirs) {
      process.chdir(dir);
  
      await buildJs(dir);
  
      process.chdir('..');
    }
  
    process.chdir('..');
  }
  
  main().catch((error) => {
    console.error(error);
    process.exit(-1);
  });
  