ensure-buildutils.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* -----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. // uncomment to time script
  6. // var start = new Date();
  7. const fs = require('fs-extra');
  8. const glob = require('glob');
  9. const path = require('path');
  10. const childProcess = require('child_process');
  11. function ensurePackage(p) {
  12. const basePath = path.join(path.resolve('.'), p);
  13. // Make sure that buildutils is built and current
  14. let current = true;
  15. if (fs.existsSync(path.join(basePath, 'lib'))) {
  16. const srcFiles = glob.sync(path.join(basePath, 'src', '*'));
  17. const libFiles = glob.sync(path.join(basePath, 'lib', '*'));
  18. srcFiles.forEach(function (srcPath) {
  19. // bail early if already not current
  20. if (!current) {
  21. return;
  22. }
  23. if (srcPath.endsWith('.d.ts')) {
  24. // bail if this is a src declarations file
  25. return;
  26. }
  27. const name = path.basename(srcPath);
  28. const ext = path.extname(name);
  29. if (ext !== '.ts') {
  30. current = false;
  31. return;
  32. }
  33. const libPath = path.join(basePath, 'lib', name.replace('.ts', '.js'));
  34. if (libFiles.indexOf(libPath) === -1) {
  35. current = false;
  36. return;
  37. }
  38. const srcTime = fs.statSync(srcPath).mtime;
  39. const libTime = fs.statSync(libPath).mtime;
  40. if (libTime < srcTime) {
  41. current = false;
  42. }
  43. });
  44. } else {
  45. current = false;
  46. }
  47. if (!current) {
  48. // This must be "npm" because it is run during `pip install -e .` before
  49. // jlpm is installed.
  50. childProcess.execSync('npm run clean', {
  51. stdio: [0, 1, 2],
  52. cwd: path.resolve('./' + p)
  53. });
  54. childProcess.execSync('npm run build', {
  55. stdio: [0, 1, 2],
  56. cwd: path.resolve('./' + p)
  57. });
  58. }
  59. }
  60. ensurePackage('buildutils');
  61. ensurePackage('builder');
  62. // uncomment to time script
  63. // var end = new Date() - start;
  64. // console.info('Execution time: %dms', end);