clean-packages.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* -----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. import * as fs from 'fs-extra';
  6. import * as path from 'path';
  7. import * as glob from 'glob';
  8. import { exitOnUuncaughtException, readJSONFile } from './utils';
  9. exitOnUuncaughtException();
  10. // Get all of the packages.
  11. const basePath = path.resolve('.');
  12. const baseConfig = readJSONFile(path.join(basePath, 'package.json'));
  13. const packageConfig = baseConfig.workspaces.packages;
  14. const skipSource = process.argv.indexOf('packages') === -1;
  15. const skipExamples = process.argv.indexOf('examples') === -1;
  16. // Handle the packages
  17. for (let i = 0; i < packageConfig.length; i++) {
  18. if (skipSource && packageConfig[i] === 'packages/*') {
  19. continue;
  20. }
  21. if (skipExamples && packageConfig[i] === 'examples/*') {
  22. continue;
  23. }
  24. const files = glob.sync(path.join(basePath, packageConfig[i]));
  25. for (let j = 0; j < files.length; j++) {
  26. try {
  27. handlePackage(files[j]);
  28. } catch (e) {
  29. console.error(e);
  30. }
  31. }
  32. }
  33. /**
  34. * Handle an individual package on the path - update the dependency.
  35. */
  36. function handlePackage(packagePath: string): void {
  37. // Read in the package.json.
  38. const packageJSONPath = path.join(packagePath, 'package.json');
  39. let data: any;
  40. try {
  41. data = require(packageJSONPath);
  42. } catch (e) {
  43. console.debug('skipping', packagePath);
  44. return;
  45. }
  46. if (!data.scripts || !data.scripts.clean) {
  47. return;
  48. }
  49. const targets = data.scripts.clean.split('&&');
  50. for (let i = 0; i < targets.length; i++) {
  51. let target = targets[i].replace('rimraf', '').trim();
  52. target = path.join(packagePath, target);
  53. if (fs.existsSync(target)) {
  54. fs.removeSync(target);
  55. }
  56. }
  57. }