Skip to content

Commit

Permalink
Add ability to see latest published version filtered by minor
Browse files Browse the repository at this point in the history
  • Loading branch information
lunaleaps committed Dec 28, 2023
1 parent d73d72e commit d46f1b3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 29 deletions.
61 changes: 44 additions & 17 deletions scripts/monorepo/print/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
48 changes: 36 additions & 12 deletions scripts/npm-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}` : '';
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d46f1b3

Please sign in to comment.