Skip to content

Commit

Permalink
feat: improve rebuild-package-locks to accept optional lerna scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Mar 30, 2020
1 parent b13b9c5 commit 9076742
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions bin/rebuild-package-locks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,69 @@
const path = require('path');
const fs = require('fs-extra');
const Project = require('@lerna/project');
const filterPackages = require('@lerna/filter-packages');
const build = require('../packages/build');

/**
* Remove all package-lock.json and node_modules for all packages
* @param {Project} project The lerna project
* @param {string[]} scopes A list of package names to be included
*/
async function removePackageLocks(project) {
async function removePackageLocks(project, ...scopes) {
const packages = await project.getPackages();
const rootPath = project.rootPath;
const pkgRoots = [];
for (const pkg of packages) {
const matchedPackages = filterPackages(packages, scopes, [], true, true);
if (matchedPackages.length === 0) return;
for (const pkg of matchedPackages) {
pkgRoots.push(pkg.location);
}
pkgRoots.push(rootPath);
// Only clean the monorepo root package if no scopes is provided
if (scopes.length === 0) pkgRoots.push(rootPath);

console.log('Cleaning package-lock.json and node_modules...');
await Promise.all(
pkgRoots.map(async root => {
console.log(' - %s', path.relative(rootPath, root));
await fs.remove(path.join(root, 'package-lock.json'));
await fs.remove(path.join(root, 'node_modules'));
}),
);
return pkgRoots;
}

/**
* Rebuild package-lock.json files:
*
* 1. Remove node_modules and package-lock.json for all packages, including the
* root one
* 2. Run `npm install` to regenerate package-lock.json files
* 1. Remove node_modules and package-lock.json for matching packages,
* including the root one if no scopes is specified
* 2. Run `npx lerna bootstrap or npm install` to regenerate package-lock.json
* files
*
* @param {string[]} scopes - Optional lerna scopes to filter packages
*/
async function rebuildPackageLocks() {
async function rebuildPackageLocks(...scopes) {
const project = new Project(process.cwd());

await removePackageLocks(project);
console.log('Running npm install...');
build.runShell('npm', ['install'], {cwd: project.rootPth});
const removed = await removePackageLocks(project, ...scopes);
if (removed.length === 0) return;
if (scopes.length) {
const args = [];
scopes.forEach(s => args.push('--scope', s));

console.log(`Running lerna bootstrap ${args.join(' ')}...`);
build.runShell('npx', ['lerna', 'bootstrap', ...args], {
cwd: project.rootPth,
});
} else {
console.log('Running npm install...');
build.runShell('npm', ['install'], {cwd: project.rootPth});
}
}

if (require.main === module) {
rebuildPackageLocks().catch(err => {
const args = process.argv.slice(2);
rebuildPackageLocks(...args).catch(err => {
console.error(err);
process.exit(1);
});
Expand Down

0 comments on commit 9076742

Please sign in to comment.