Ver código fonte

Merge pull request #5436 from jasongrout/update-dist-tag2

Polish update-dist-tag
Jason Grout 6 anos atrás
pai
commit
75736319a7

+ 2 - 1
buildutils/package.json

@@ -21,7 +21,8 @@
   "bin": {
     "get-dependency": "./lib/get-dependency.js",
     "remove-dependency": "./lib/remove-dependency.js",
-    "update-dependency": "./lib/update-dependency.js"
+    "update-dependency": "./lib/update-dependency.js",
+    "update-dist-tag": "./lib/update-dist-tag.js"
   },
   "directories": {
     "lib": "lib/"

+ 8 - 9
buildutils/src/update-dependency.ts

@@ -149,8 +149,6 @@ async function handlePackage(
   }
 }
 
-let run = false;
-
 commander
   .description('Update dependency versions')
   .usage('[options] <package> [versionspec], versionspec defaults to ^latest')
@@ -162,13 +160,14 @@ commander
   .arguments('<package> [versionspec]')
   .action(
     async (name: string | RegExp, version: string = '^latest', args: any) => {
-      run = true;
       let basePath = path.resolve(args.path || '.');
       let pkg = args.regex ? new RegExp(name) : name;
 
       if (args.lerna) {
-        let paths = utils.getLernaPaths(basePath);
-        paths.sort();
+        let paths = utils.getLernaPaths(basePath).sort();
+
+        // We use a loop instead of Promise.all so that the output is in
+        // alphabetical order.
         for (let pkgPath of paths) {
           await handlePackage(pkg, version, pkgPath, args.dryRun, args.minimal);
         }
@@ -208,11 +207,11 @@ Examples
       update-dependency --lerna --regex --minimal '^@jupyterlab/' ^next
 `);
 });
+
 commander.parse(process.argv);
 
-if (!run) {
-  console.error(`
-  error: missing required argument 'package'
-  `);
+// If no arguments supplied
+if (!process.argv.slice(2).length) {
+  commander.outputHelp();
   process.exit(1);
 }

+ 51 - 43
buildutils/src/update-dist-tag.ts

@@ -5,18 +5,15 @@
 
 import * as path from 'path';
 import * as utils from './utils';
-
-// Handle the packages
-utils.getLernaPaths().forEach(pkgPath => {
-  handlePackage(pkgPath);
-});
-handlePackage(path.resolve('.'));
+import packageJson from 'package-json';
+import commander from 'commander';
+import semver from 'semver';
 
 /**
  * Handle an individual package on the path - update the dependency.
  */
-function handlePackage(packagePath: string): void {
-  let cmd: string;
+async function handlePackage(packagePath: string): Promise<string[]> {
+  const cmds: string[] = [];
 
   // Read in the package.json.
   packagePath = path.join(packagePath, 'package.json');
@@ -25,53 +22,64 @@ function handlePackage(packagePath: string): void {
     data = utils.readJSONFile(packagePath);
   } catch (e) {
     console.log('Skipping package ' + packagePath);
-    return;
+    return cmds;
   }
 
   if (data.private) {
-    return;
+    return cmds;
   }
 
   const pkg = data.name;
 
-  cmd = `npm view ${pkg} versions --json`;
-  const versions: string[] = JSON.parse(utils.run(cmd, { stdio: 'pipe' }));
+  let npmData = await packageJson(pkg, { allVersions: true });
+  let versions = Object.keys(npmData.versions).sort(semver.rcompare);
+  let tags = npmData['dist-tags'];
 
-  // Find latest stable
-  versions.reverse();
-  let prerelease = versions.find(v => !!v.match(/-\d$/));
-  let stable = versions.find(v => !v.match(/-\d$/));
+  // Go through the versions. The latest prerelease is 'next', the latest
+  // non-prerelease should be 'stable'.
+  let next = semver.prerelease(versions[0]) ? versions[0] : undefined;
+  let latest = versions.find(i => !semver.prerelease(i));
 
-  // Make sure the prerelease we found is *after* the stable release
-  if (
-    prerelease &&
-    stable &&
-    versions.indexOf(prerelease) > versions.indexOf(stable)
-  ) {
-    prerelease = undefined;
+  if (latest && latest !== tags.latest) {
+    cmds.push(`npm dist-tag add ${pkg}@${latest} latest`);
   }
 
-  cmd = `npm dist-tag list ${pkg}`;
-  let tags = utils.run(cmd).split('\n');
-
-  console.log();
-  console.log(pkg, stable, prerelease, tags);
-
-  let stableCmd: string;
-  let prereleaseCmd: string;
-
-  if (stable) {
-    stableCmd = `npm dist-tag add ${pkg}@${stable} latest`;
-  } else {
-    stableCmd = `npm dist-tag rm ${pkg} latest`;
+  // If next is defined, but not supposed to be, remove it. If next is supposed
+  // to be defined, but is not the same as the current next, change it.
+  if (!next && tags.next) {
+    cmds.push(`npm dist-tag rm ${pkg} next`);
+  } else if (next && next !== tags.next) {
+    cmds.push(`npm dist-tag add ${pkg}@${next} next`);
   }
 
-  if (prerelease) {
-    prereleaseCmd = `npm dist-tag add ${pkg}@${prerelease} next`;
-  } else {
-    prereleaseCmd = `npm dist-tag rm ${pkg} next`;
-  }
+  return cmds;
+}
 
-  console.log(stableCmd);
-  console.log(prereleaseCmd);
+function flatten(a: any[]) {
+  return a.reduce((acc, val) => acc.concat(val), []);
 }
+
+commander
+  .description(
+    `Print out commands to update npm 'latest' and 'next' dist-tags
+so that 'latest' points to the latest stable release and 'next'
+points to the latest prerelease after it.`
+  )
+  .option('--lerna', 'Update dist-tags in all lerna packages')
+  .option('--path [path]', 'Path to package or monorepo to update')
+  .action(async (args: any) => {
+    let basePath = path.resolve(args.path || '.');
+    let cmds: string[][] = [];
+    let paths: string[] = [];
+    if (args.lerna) {
+      paths = utils.getLernaPaths(basePath).sort();
+      cmds = await Promise.all(paths.map(handlePackage));
+    }
+    cmds.push(await handlePackage(basePath));
+    let out = flatten(cmds).join('\n');
+    if (out) {
+      console.log(out);
+    }
+  });
+
+commander.parse(process.argv);

+ 1 - 1
package.json

@@ -52,7 +52,7 @@
     "prettier": "prettier --write '**/*{.ts,.tsx,.js,.jsx,.css,.json,.md}'",
     "prettier:check": "prettier --list-different '**/*{.ts,.tsx,.js,.jsx,.css,.json,.md}'",
     "update:dependency": "node buildutils/lib/update-dependency.js --lerna",
-    "update:dist-tag": "node buildutils/lib/update-dist-tag.js",
+    "update:dist-tag": "node buildutils/lib/update-dist-tag.js --lerna",
     "update:local": "node buildutils/lib/update-local.js",
     "watch": "run-p watch:dev watch:themes",
     "watch:dev": "python scripts/watch_dev.py",