From 907674237c28dd00fb999f4e66ea1165fced1d04 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 27 Mar 2020 14:28:32 -0700 Subject: [PATCH] feat: improve rebuild-package-locks to accept optional lerna scopes --- bin/rebuild-package-locks.js | 44 +++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/bin/rebuild-package-locks.js b/bin/rebuild-package-locks.js index c13942da9528..8a986c5d473b 100755 --- a/bin/rebuild-package-locks.js +++ b/bin/rebuild-package-locks.js @@ -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); });