Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

update-dependency.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/usr/bin/env node
  2. "use strict";
  3. /* -----------------------------------------------------------------------------
  4. | Copyright (c) Jupyter Development Team.
  5. | Distributed under the terms of the Modified BSD License.
  6. |----------------------------------------------------------------------------*/
  7. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  8. if (k2 === undefined) k2 = k;
  9. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  10. }) : (function(o, m, k, k2) {
  11. if (k2 === undefined) k2 = k;
  12. o[k2] = m[k];
  13. }));
  14. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  15. Object.defineProperty(o, "default", { enumerable: true, value: v });
  16. }) : function(o, v) {
  17. o["default"] = v;
  18. });
  19. var __importStar = (this && this.__importStar) || function (mod) {
  20. if (mod && mod.__esModule) return mod;
  21. var result = {};
  22. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  23. __setModuleDefault(result, mod);
  24. return result;
  25. };
  26. var __importDefault = (this && this.__importDefault) || function (mod) {
  27. return (mod && mod.__esModule) ? mod : { "default": mod };
  28. };
  29. Object.defineProperty(exports, "__esModule", { value: true });
  30. const path = __importStar(require("path"));
  31. const utils = __importStar(require("./utils"));
  32. const package_json_1 = __importDefault(require("package-json"));
  33. const commander_1 = __importDefault(require("commander"));
  34. const semver_1 = __importDefault(require("semver"));
  35. const versionCache = new Map();
  36. /**
  37. * Matches a simple semver range, where the version number could be an npm tag.
  38. */
  39. const SEMVER_RANGE = /^(~|\^|=|<|>|<=|>=)?([\w\-.]*)$/;
  40. /**
  41. * Get the specifier we should use
  42. *
  43. * @param currentSpecifier - The current package version.
  44. * @param suggestedSpecifier - The package version we would like to use.
  45. *
  46. * #### Notes
  47. * If the suggested specifier is not a valid range, we assume it is of the
  48. * form ${RANGE}${TAG}, where TAG is an npm tag (such as 'latest') and RANGE
  49. * is either a semver range indicator (one of `~, ^, >, <, =, >=, <=`), or is
  50. * not given (in which case the current specifier range prefix is used).
  51. */
  52. async function getSpecifier(currentSpecifier, suggestedSpecifier) {
  53. var _a;
  54. if (semver_1.default.validRange(suggestedSpecifier)) {
  55. return suggestedSpecifier;
  56. }
  57. // The suggested specifier is not a valid range, so we assume it
  58. // references a tag
  59. let [, suggestedSigil, suggestedTag] = (_a = suggestedSpecifier.match(SEMVER_RANGE)) !== null && _a !== void 0 ? _a : [];
  60. if (!suggestedTag) {
  61. throw Error(`Invalid version specifier: ${suggestedSpecifier}`);
  62. }
  63. // A tag with no sigil adopts the sigil from the current specification
  64. if (!suggestedSigil) {
  65. const match = currentSpecifier.match(SEMVER_RANGE);
  66. if (match === null) {
  67. throw Error(`Current version range is not recognized: ${currentSpecifier}`);
  68. }
  69. suggestedSigil = match[1];
  70. }
  71. return `${suggestedSigil !== null && suggestedSigil !== void 0 ? suggestedSigil : ''}${suggestedTag}`;
  72. }
  73. async function getVersion(pkg, specifier) {
  74. var _a;
  75. const key = JSON.stringify([pkg, specifier]);
  76. if (versionCache.has(key)) {
  77. return versionCache.get(key);
  78. }
  79. if (semver_1.default.validRange(specifier) === null) {
  80. // We have a tag, with possibly a range specifier, such as ^latest
  81. const match = specifier.match(SEMVER_RANGE);
  82. if (match === null) {
  83. throw Error(`Invalid version specifier: ${specifier}`);
  84. }
  85. // Look up the actual version corresponding to the tag
  86. const { version } = await package_json_1.default(pkg, { version: match[2] });
  87. specifier = `${(_a = match[1]) !== null && _a !== void 0 ? _a : ''}${version}`;
  88. if (semver_1.default.validRange(specifier) === null) {
  89. throw Error(`Could not find valid version range for ${pkg}: ${specifier}`);
  90. }
  91. }
  92. versionCache.set(key, specifier);
  93. return specifier;
  94. }
  95. /**
  96. * A very simple subset comparator
  97. *
  98. * @returns true if we can determine if range1 is a subset of range2, otherwise false
  99. *
  100. * #### Notes
  101. * This will not be able to determine if range1 is a subset of range2 in many cases.
  102. */
  103. function subset(range1, range2) {
  104. var _a, _b;
  105. try {
  106. const [, r1, version1] = (_a = range1.match(SEMVER_RANGE)) !== null && _a !== void 0 ? _a : [];
  107. const [, r2] = (_b = range2.match(SEMVER_RANGE)) !== null && _b !== void 0 ? _b : [];
  108. return (['', '>=', '=', '~', '^'].includes(r1) &&
  109. r1 === r2 &&
  110. !!semver_1.default.valid(version1) &&
  111. semver_1.default.satisfies(version1, range2));
  112. }
  113. catch (e) {
  114. return false;
  115. }
  116. }
  117. async function handleDependency(dependencies, dep, suggestedSpecifier, minimal) {
  118. const log = [];
  119. let updated = false;
  120. const oldRange = dependencies[dep];
  121. const specifier = await getSpecifier(oldRange, suggestedSpecifier);
  122. const newRange = await getVersion(dep, specifier);
  123. if (minimal && subset(newRange, oldRange)) {
  124. log.push(`SKIPPING ${dep} ${oldRange} -> ${newRange}`);
  125. }
  126. else {
  127. log.push(`${dep} ${oldRange} -> ${newRange}`);
  128. dependencies[dep] = newRange;
  129. updated = true;
  130. }
  131. return { updated, log };
  132. }
  133. /**
  134. * Handle an individual package on the path - update the dependency.
  135. */
  136. async function handlePackage(name, specifier, packagePath, dryRun = false, minimal = false) {
  137. let fileUpdated = false;
  138. const fileLog = [];
  139. // Read in the package.json.
  140. packagePath = path.join(packagePath, 'package.json');
  141. let data;
  142. try {
  143. data = utils.readJSONFile(packagePath);
  144. }
  145. catch (e) {
  146. console.debug('Skipping package ' + packagePath);
  147. return;
  148. }
  149. // Update dependencies as appropriate.
  150. for (const dtype of ['dependencies', 'devDependencies']) {
  151. const deps = data[dtype] || {};
  152. if (typeof name === 'string') {
  153. const dep = name;
  154. if (dep in deps) {
  155. const { updated, log } = await handleDependency(deps, dep, specifier, minimal);
  156. if (updated) {
  157. fileUpdated = true;
  158. }
  159. fileLog.push(...log);
  160. }
  161. }
  162. else {
  163. const keys = Object.keys(deps);
  164. keys.sort();
  165. for (const dep of keys) {
  166. if (dep.match(name)) {
  167. const { updated, log } = await handleDependency(deps, dep, specifier, minimal);
  168. if (updated) {
  169. fileUpdated = true;
  170. }
  171. fileLog.push(...log);
  172. }
  173. }
  174. }
  175. }
  176. if (fileLog.length > 0) {
  177. console.debug(packagePath);
  178. console.debug(fileLog.join('\n'));
  179. console.debug();
  180. }
  181. // Write the file back to disk.
  182. if (!dryRun && fileUpdated) {
  183. utils.writePackageData(packagePath, data);
  184. }
  185. }
  186. commander_1.default
  187. .description('Update dependency versions')
  188. .usage('[options] <package> [versionspec], versionspec defaults to ^latest')
  189. .option('--dry-run', 'Do not perform actions, just print output')
  190. .option('--regex', 'Package is a regular expression')
  191. .option('--lerna', 'Update dependencies in all lerna packages')
  192. .option('--path <path>', 'Path to package or monorepo to update')
  193. .option('--minimal', 'only update if the change is substantial')
  194. .arguments('<package> [versionspec]')
  195. .action(async (name, version = '^latest', args) => {
  196. const basePath = path.resolve(args.path || '.');
  197. const pkg = args.regex ? new RegExp(name) : name;
  198. if (args.lerna) {
  199. const paths = utils.getLernaPaths(basePath).sort();
  200. // We use a loop instead of Promise.all so that the output is in
  201. // alphabetical order.
  202. for (const pkgPath of paths) {
  203. await handlePackage(pkg, version, pkgPath, args.dryRun, args.minimal);
  204. }
  205. }
  206. await handlePackage(pkg, version, basePath, args.dryRun, args.minimal);
  207. });
  208. commander_1.default.on('--help', function () {
  209. console.debug(`
  210. Examples
  211. --------
  212. Update the package 'webpack' to a specific version range:
  213. update-dependency webpack ^4.0.0
  214. Update all packages to the latest version, with a caret.
  215. Only update if the update is substantial:
  216. update-dependency --minimal --regex '.*' ^latest
  217. Update all packages, that does not start with '@jupyterlab',
  218. to the latest version and use the same version specifier currently
  219. being used
  220. update:dependency --regex '^(?!@jupyterlab).*' latest --dry-run
  221. Print the log of the above without actually making any changes.
  222. update-dependency --dry-run --minimal --regex '.*' ^latest
  223. Update all packages starting with '@jupyterlab/' to the version
  224. the 'latest' tag currently points to, with a caret range:
  225. update-dependency --regex '^@jupyterlab/' ^latest
  226. Update all packages starting with '@jupyterlab/' in all lerna
  227. workspaces and the root package.json to whatever version the 'next'
  228. tag for each package currently points to (with a caret tag).
  229. Update the version range only if the change is substantial.
  230. update-dependency --lerna --regex --minimal '^@jupyterlab/' ^next
  231. `);
  232. });
  233. commander_1.default.parse(process.argv);
  234. // If no arguments supplied
  235. if (!process.argv.slice(2).length) {
  236. commander_1.default.outputHelp();
  237. process.exit(1);
  238. }
  239. //# sourceMappingURL=update-dependency.js.map