dev-build-ts.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 CPX = ['css', 'gif', 'hbs', 'jpg', 'js', 'png', 'svg', 'd.ts']
  14. .map((ext) => `src/**/*.${ext}`)
  15. .concat('package.json');
  16. console.log('$ polkadot-dev-build-ts', process.argv.slice(2).join(' '));
  17. function buildWebpack () {
  18. execSync('yarn polkadot-exec-webpack --config webpack.config.js --mode production');
  19. }
  20. async function buildBabel (dir) {
  21. await babel({
  22. babelOptions: {
  23. configFile: path.join(process.cwd(), '../../babel.config.js')
  24. },
  25. cliOptions: {
  26. extensions: ['.ts', '.tsx'],
  27. filenames: ['src'],
  28. ignore: '**/*.d.ts',
  29. outDir: path.join(process.cwd(), 'build')
  30. }
  31. });
  32. [...CPX]
  33. .concat(`../../build/${dir}/src/**/*.d.ts`, `../../build/packages/${dir}/src/**/*.d.ts`)
  34. .forEach((src) => cpx.copySync(src, 'build'));
  35. }
  36. async function buildJs (dir) {
  37. if (!fs.existsSync(path.join(process.cwd(), '.skip-build'))) {
  38. const { name, version } = require(path.join(process.cwd(), './package.json'));
  39. if (!name.startsWith('@polkadot/')) {
  40. return;
  41. }
  42. console.log(`*** ${name} ${version}`);
  43. mkdirp.sync('build');
  44. if (fs.existsSync(path.join(process.cwd(), 'public'))) {
  45. buildWebpack(dir);
  46. } else {
  47. await buildBabel(dir);
  48. }
  49. console.log();
  50. }
  51. }
  52. async function main () {
  53. execSync('yarn polkadot-dev-clean-build');
  54. // By default the entry point is pioneer/, so here we move to pioneer/packages
  55. process.chdir('packages');
  56. // This line below is what caused problems with the original script, because it seems like the outdir
  57. // should be relative to "/pioneer/packages", but for some reason is relative to "/pioneer" instead.
  58. // This caused the build folder to end up in the root directory of the monorepo (instead of "pioneer/build")
  59. //
  60. // execSync('yarn polkadot-exec-tsc --emitDeclarationOnly --outdir ../build');
  61. execSync('yarn tsc --emitDeclarationOnly --outdir ./build');
  62. const dirs = fs
  63. .readdirSync('.')
  64. .filter((dir) => fs.statSync(dir).isDirectory() && fs.existsSync(path.join(process.cwd(), dir, 'src')));
  65. for (const dir of dirs) {
  66. process.chdir(dir);
  67. await buildJs(dir);
  68. process.chdir('..');
  69. }
  70. process.chdir('..');
  71. }
  72. main().catch((error) => {
  73. console.error(error);
  74. process.exit(-1);
  75. });