dev-build-ts.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env node
  2. // Copyright 2017-2020 @polkadot/dev authors & contributors
  3. // This software may be modified and distributed under the terms
  4. // of the Apache-2.0 license. See the LICENSE file for details.
  5. // This is the overriden version of polkadot-dev-build-ts binary
  6. // which seems have some issues with our version of yarn
  7. const babel = require('@babel/cli/lib/babel/dir').default;
  8. const execSync = require('@polkadot/dev/scripts/execSync');
  9. const cpx = require('cpx');
  10. const fs = require('fs');
  11. const mkdirp = require('mkdirp');
  12. const path = require('path');
  13. const IS_LIVE = !(process.env.IS_LIVE === false || process.env.IS_LIVE === 'false');
  14. const CPX = ['css', 'gif', 'hbs', 'jpg', 'js', 'png', 'svg', 'd.ts']
  15. .map((ext) => `src/**/*.${ext}`)
  16. .concat('package.json');
  17. console.log('$ polkadot-dev-build-ts', process.argv.slice(2).join(' '));
  18. function buildWebpack () {
  19. execSync('yarn polkadot-exec-webpack --config webpack.config.js --mode production');
  20. }
  21. async function buildBabel (dir) {
  22. await babel({
  23. babelOptions: {
  24. configFile: path.join(process.cwd(), '../../babel.config.js')
  25. },
  26. cliOptions: {
  27. extensions: ['.ts', '.tsx'],
  28. filenames: ['src'],
  29. ignore: '**/*.d.ts',
  30. outDir: path.join(process.cwd(), 'build')
  31. }
  32. });
  33. [...CPX]
  34. .concat(`../../build/${dir}/src/**/*.d.ts`, `../../build/packages/${dir}/src/**/*.d.ts`)
  35. .forEach((src) => cpx.copySync(src, 'build'));
  36. }
  37. async function buildJs (dir) {
  38. if (!fs.existsSync(path.join(process.cwd(), '.skip-build'))) {
  39. const { name, version } = require(path.join(process.cwd(), './package.json'));
  40. if (!name.startsWith('@polkadot/')) {
  41. return;
  42. }
  43. console.log(`*** ${name} ${version}`);
  44. mkdirp.sync('build');
  45. if (fs.existsSync(path.join(process.cwd(), 'public'))) {
  46. buildWebpack(dir);
  47. // Skip building anything else if we're not going for a LIVE build
  48. } else if (IS_LIVE) {
  49. await buildBabel(dir);
  50. }
  51. console.log();
  52. }
  53. }
  54. async function main () {
  55. execSync('yarn polkadot-dev-clean-build');
  56. console.log('IS_LIVE:', process.env.IS_LIVE);
  57. // By default the entry point is pioneer/, so here we move to pioneer/packages
  58. process.chdir('packages');
  59. // This line below is what caused problems with the original script, because it seems like the outdir
  60. // should be relative to "/pioneer/packages", but for some reason is relative to "/pioneer" instead.
  61. // This caused the build folder to end up in the root directory of the monorepo (instead of "pioneer/build")
  62. //
  63. // execSync('yarn polkadot-exec-tsc --emitDeclarationOnly --outdir ../build');
  64. if (IS_LIVE) {
  65. execSync('yarn tsc --emitDeclarationOnly --outdir ./build');
  66. }
  67. const dirs = fs
  68. .readdirSync('.')
  69. .filter((dir) => fs.statSync(dir).isDirectory() && fs.existsSync(path.join(process.cwd(), dir, 'src')));
  70. for (const dir of dirs) {
  71. process.chdir(dir);
  72. await buildJs(dir);
  73. process.chdir('..');
  74. }
  75. process.chdir('..');
  76. }
  77. main().catch((error) => {
  78. console.error(error);
  79. process.exit(-1);
  80. });