From d46f1b3f72e1402ada82b2e9e2824eb86e9e6734 Mon Sep 17 00:00:00 2001 From: Luna Wei Date: Thu, 28 Dec 2023 15:23:23 -0800 Subject: [PATCH] Add ability to see latest published version filtered by minor --- scripts/monorepo/print/index.js | 61 ++++++++++++++++++++++++--------- scripts/npm-utils.js | 48 +++++++++++++++++++------- 2 files changed, 80 insertions(+), 29 deletions(-) diff --git a/scripts/monorepo/print/index.js b/scripts/monorepo/print/index.js index 0b8db0b022aed4..ce41b4e9030f0d 100644 --- a/scripts/monorepo/print/index.js +++ b/scripts/monorepo/print/index.js @@ -7,40 +7,67 @@ * @format */ -const {echo, exit} = require('shelljs'); +const {getLatestVersionBySpec} = require('../../npm-utils'); const forEachPackage = require('../for-each-package'); +const {exit} = require('shelljs'); const yargs = require('yargs'); const { - argv: {private, public}, + argv: {type, minor}, } = yargs - .option('private', { - type: 'boolean', - describe: 'Only list private packages', + .option('type', { + type: 'string', + describe: 'Choose which packages to list, default is all', + choices: ['all', 'public', 'private'], + default: 'all', }) - .option('public', { - type: 'boolean', - describe: 'Only list public packages', + .option('minor', { + type: 'number', + describe: + 'List latest version for specified minor. Ex. 72, 73. Note this will make a network request to npm', + default: 0, + }) + .check(argv => { + if (argv.minor > 0 && argv.minor < 70) { + throw new Error('Invalid minor. No monorepo packages before 70'); + } + return true; }) .strict(); const main = () => { + const data = []; forEachPackage( (_packageAbsolutePath, _packageRelativePathFromRoot, packageManifest) => { - const packageName = packageManifest.name; const isPublic = !packageManifest.private; - if ((public && isPublic) || (private && !isPublic)) { - echo(`${packageName} ${packageManifest.version}`); - } else if (!private && !public) { - echo( - `${isPublic ? '\u{1F4E6}' : '\u{1F512}'} ${packageName} ${ - packageManifest.version - }`, - ); + if ( + type === 'all' || + (type === 'private' && !isPublic) || + (type === 'public' && isPublic) + ) { + const packageInfo = { + 'Public?': isPublic ? '\u{2705}' : '\u{274C}', + Name: packageManifest.name, + 'Version (main)': packageManifest.version, + }; + + if (isPublic && minor !== 0) { + try { + const version = getLatestVersionBySpec( + packageManifest.name, + `^0.${minor}.0`, + ); + packageInfo[`Version (${minor})`] = version; + } catch (e) { + packageInfo[`Version (${minor})`] = 'n/a'; + } + } + data.push(packageInfo); } }, {includeReactNative: true}, ); + console.table(data); exit(0); }; diff --git a/scripts/npm-utils.js b/scripts/npm-utils.js index 3f0bb34aa5a1e9..fc8d22435fe724 100644 --- a/scripts/npm-utils.js +++ b/scripts/npm-utils.js @@ -90,18 +90,6 @@ function getNpmInfo(buildType) { }; } -function getPackageVersionStrByTag(packageName, tag) { - const npmString = tag - ? `npm view ${packageName}@${tag} version` - : `npm view ${packageName} version`; - const result = exec(npmString, {silent: true}); - - if (result.code) { - throw new Error(`Failed to get ${tag} version from npm\n${result.stderr}`); - } - return result.stdout.trim(); -} - function publishPackage(packagePath, packageOptions, execOptions) { const {tag, otp} = packageOptions; const tagFlag = tag ? ` --tag ${tag}` : ''; @@ -165,10 +153,46 @@ function applyPackageVersions(originalPackageJson, packageVersions) { return packageJson; } +/** + * `packageName`: name of npm package + * `tag`: npm tag like `latest` or `next` + * + * This will fetch version of `packageName` with npm tag specified + */ +function getPackageVersionStrByTag(packageName, tag) { + const npmString = tag + ? `npm view ${packageName}@${tag} version` + : `npm view ${packageName} version`; + const result = exec(npmString, {silent: true}); + + if (result.code) { + throw new Error(`Failed to get ${tag} version from npm\n${result.stderr}`); + } + return result.stdout.trim(); +} + +/** + * `packageName`: name of npm package + * `spec`: spec range ex. '^0.72.0' + * + * This will fetch the latest version of the spec range + */ +function getLatestVersionBySpec(packageName, spec) { + const npmString = `npm view ${packageName}@'${spec}' version --json`; + const result = exec(npmString, {silent: true}); + + if (result.code) { + throw new Error(`Failed to get versions for ${packageName}@${spec}`); + } + const versions = JSON.parse(result.stdout).sort(); + return versions[versions.length - 1]; +} + module.exports = { applyPackageVersions, getNpmInfo, getPackageVersionStrByTag, + getLatestVersionBySpec, publishPackage, diffPackages, pack,