From 2848c78b999faf829838ab1f4a857e58f09a8752 Mon Sep 17 00:00:00 2001 From: nlf Date: Tue, 30 Mar 2021 09:38:07 -0700 Subject: [PATCH] feat(set-script): implement workspaces for set-script --- lib/set-script.js | 55 ++++++++++++-- .../test-lib-utils-npm-usage.js-TAP.test.js | 3 + test/lib/set-script.js | 73 +++++++++++++++++-- 3 files changed, 117 insertions(+), 14 deletions(-) diff --git a/lib/set-script.js b/lib/set-script.js index 0d7c38c3152cb..b31e123becd8b 100644 --- a/lib/set-script.js +++ b/lib/set-script.js @@ -3,6 +3,7 @@ const fs = require('fs') const parseJSON = require('json-parse-even-better-errors') const rpj = require('read-package-json-fast') const { resolve } = require('path') +const getWorkspaces = require('./workspaces/get-workspaces.js') const BaseCommand = require('./base-command.js') class SetScript extends BaseCommand { @@ -11,6 +12,11 @@ class SetScript extends BaseCommand { return 'Set tasks in the scripts section of package.json' } + /* istanbul ignore next - see test/lib/load-all-commands.js */ + static get params () { + return ['workspace', 'workspaces'] + } + /* istanbul ignore next - see test/lib/load-all-commands.js */ static get name () { return 'set-script' @@ -31,21 +37,55 @@ class SetScript extends BaseCommand { } } - exec (args, cb) { - this.set(args).then(() => cb()).catch(cb) - } - - async set (args) { + validate (args) { if (process.env.npm_lifecycle_event === 'postinstall') throw new Error('Scripts can’t set from the postinstall script') // Parse arguments if (args.length !== 2) throw new Error(`Expected 2 arguments: got ${args.length}`) + } - return this.setScript(this.npm.localPrefix, args[0], args[1]) + exec (args, cb) { + this.set(args).then(() => cb()).catch(cb) } + async set (args) { + this.validate(args) + const warn = this.setScript(this.npm.localPrefix, args[0], args[1]) + if (warn) + log.warn('set-script', `Script "${args[0]}" was overwritten`) + } + + execWorkspaces (args, filters, cb) { + this.setWorkspaces(args, filters).then(() => cb()).catch(cb) + } + + async setWorkspaces (args, filters) { + this.validate(args) + const workspaces = + await getWorkspaces(filters, { path: this.npm.localPrefix }) + + for (const [name, path] of workspaces) { + try { + const warn = this.setScript(path, args[0], args[1]) + if (warn) { + log.warn('set-script', `Script "${args[0]}" was overwritten`) + log.warn(` in workspace: ${name}`) + log.warn(` at location: ${path}`) + } + } catch (err) { + log.error('set-script', err.message) + log.error(` in workspace: ${name}`) + log.error(` at location: ${path}`) + process.exitCode = 1 + } + } + } + + // returns a Boolean that will be true if + // the requested script was overwritten + // and false if it was set as a new script setScript (path, name, value) { // Set the script let manifest @@ -80,8 +120,7 @@ class SetScript extends BaseCommand { .replace(/\n/g, newline) fs.writeFileSync(resolve(path, 'package.json'), content) - if (warn) - log.warn('set-script', `Script "${name}" was overwritten`) + return warn } } module.exports = SetScript diff --git a/tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js b/tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js index 5a860bd2ee554..d071b84d4b0fe 100644 --- a/tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js +++ b/tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js @@ -759,6 +759,9 @@ All commands: Usage: npm set-script [