bump-js-major.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* -----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. import commander from 'commander';
  6. import semver from 'semver';
  7. import path from 'path';
  8. import * as utils from './utils';
  9. /**
  10. * Get the packages that depend on a given package, recursively.
  11. */
  12. export function getDeps(
  13. pkgName: string,
  14. lut: { [key: string]: { [key: string]: string } }
  15. ): Set<string> {
  16. const deps: Set<string> = new Set();
  17. for (const name in lut) {
  18. if ('@jupyterlab/' + pkgName in lut[name]) {
  19. const otherName = name.replace('@jupyterlab/', '');
  20. deps.add(otherName);
  21. const otherDeps = getDeps(otherName, lut);
  22. otherDeps.forEach(dep => {
  23. deps.add(dep);
  24. });
  25. }
  26. }
  27. return deps;
  28. }
  29. // Specify the program signature.
  30. commander
  31. .description('Bump the major version of JS package(s)')
  32. .arguments('<package> [others...]')
  33. .option('--force', 'Force the upgrade')
  34. .option('--dry-run', 'Show what would be executed')
  35. .action((pkg: string, others: Array<string>, options: any) => {
  36. utils.exitOnUuncaughtException();
  37. others.push(pkg);
  38. const toBump: Set<string> = new Set();
  39. const ignoreBump: Set<string> = new Set();
  40. const maybeBump = (pkg: string) => {
  41. if (pkg in toBump || pkg in ignoreBump) {
  42. return;
  43. }
  44. const version = utils.getJSVersion(pkg);
  45. if (semver.minor(version) === 0 && semver.prerelease(version)) {
  46. console.warn(`${pkg} has already been bumped`);
  47. ignoreBump.add(pkg);
  48. } else {
  49. toBump.add(pkg);
  50. }
  51. };
  52. others.forEach(pkg => {
  53. maybeBump(pkg);
  54. });
  55. // Create a lut of dependencies
  56. const lut: { [key: string]: { [key: string]: string } } = {};
  57. utils.getCorePaths().forEach(corePath => {
  58. const pkgDataPath = path.join(corePath, 'package.json');
  59. const data = utils.readJSONFile(pkgDataPath);
  60. lut[data.name] = data.dependencies || {};
  61. });
  62. // Look for dependencies of bumped packages
  63. Array.from(toBump).forEach(val => {
  64. const deps = getDeps(val, lut);
  65. deps.forEach(dep => {
  66. maybeBump(dep);
  67. });
  68. });
  69. if (!toBump.size) {
  70. console.warn('No packages found to bump!');
  71. return;
  72. }
  73. const pyVersion = utils.getPythonVersion();
  74. let preId = '';
  75. if (pyVersion.includes('a')) {
  76. preId = 'alpha';
  77. } else if (pyVersion.includes('rc')) {
  78. preId = 'rc';
  79. } else {
  80. throw new Error(
  81. 'Cannot bump JS packages until we switch to prerelease mode'
  82. );
  83. }
  84. const pkgs = Array.from(toBump).join(',');
  85. let cmd = `lerna version premajor --preid=${preId} --force-publish=${pkgs} --no-push`;
  86. if (options.force) {
  87. cmd += ' --yes';
  88. }
  89. if (options.dryRun) {
  90. console.debug('Would run:');
  91. console.debug(cmd);
  92. return;
  93. }
  94. utils.run(cmd);
  95. });
  96. commander.parse(process.argv);