Skip to content

Commit

Permalink
feat(set-script): implement workspaces for set-script
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Mar 30, 2021
1 parent 8a41e6d commit 2848c78
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 14 deletions.
55 changes: 47 additions & 8 deletions lib/set-script.js
Expand Up @@ -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 {
Expand All @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js
Expand Up @@ -759,6 +759,9 @@ All commands:
Usage:
npm set-script [<script>] [<command>]
Options:
[-w|--workspace <workspace>|-w|--workspace <workspace>] [-ws|--workspaces]
Run "npm help set-script" for more info
shrinkwrap npm shrinkwrap
Expand Down
73 changes: 67 additions & 6 deletions test/lib/set-script.js
Expand Up @@ -8,11 +8,15 @@ const { resolve } = require('path')
const flatOptions = {}
const npm = mockNpm(flatOptions)

const OUTPUT = []
const ERROR_OUTPUT = []
const WARN_OUTPUT = []
const SetScript = requireInject('../../lib/set-script.js', {
npmlog: {
warn: (prefix, msg) => {
OUTPUT.push([prefix, msg])
error: (...args) => {
ERROR_OUTPUT.push(args)
},
warn: (...args) => {
WARN_OUTPUT.push(args)
},
},
})
Expand Down Expand Up @@ -101,8 +105,8 @@ test.test('creates scripts object', (t) => {
})
})

test.test('warns before overwriting', (t) => {
OUTPUT.length = 0
test.test('warns when overwriting', (t) => {
WARN_OUTPUT.length = 0
npm.localPrefix = t.testdir({
'package.json': JSON.stringify({
scripts: {
Expand All @@ -114,7 +118,7 @@ test.test('warns before overwriting', (t) => {
t.plan(2)
setScript.exec(['arg1', 'arg2'], (error) => {
t.equal(error, undefined, 'no error')
t.hasStrict(OUTPUT[0], ['set-script', 'Script "arg1" was overwritten'], 'warning was logged')
t.hasStrict(WARN_OUTPUT[0], ['set-script', 'Script "arg1" was overwritten'], 'warning was logged')
})
})

Expand All @@ -136,3 +140,60 @@ test.test('provided indentation and eol is used', (t) => {
t.equal(data[Symbol.for('newline')], '\r\n', 'keeps newlines')
})
})

test.test('workspaces', (t) => {
ERROR_OUTPUT.length = 0
WARN_OUTPUT.length = 0
npm.localPrefix = t.testdir({
'package.json': JSON.stringify({
name: 'workspaces-test',
version: '1.0.0',
workspaces: ['workspace-a', 'workspace-b', 'workspace-c'],
}),
'workspace-a': {
'package.json': '{}',
},
'workspace-b': {
'package.json': '"notjson"',
},
'workspace-c': {
'package.json': JSON.stringify({
scripts: {
arg1: 'test',
},
}, null, ' '.repeat(6)).replace(/\n/g, '\r\n'),
},
})

setScript.execWorkspaces(['arg1', 'arg2'], [], (error) => {
t.equal(error, undefined, 'did not callback with an error')
t.equal(process.exitCode, 1, 'did set the exitCode to 1')
// force the exitCode back to 0 to make tap happy
process.exitCode = 0

// workspace-a had the script added
const contentsA = fs.readFileSync(resolve(npm.localPrefix, 'workspace-a', 'package.json'))
const dataA = parseJSON(contentsA)
t.hasStrict(dataA, { scripts: { arg1: 'arg2' } }, 'defined the script')

// workspace-b logged an error
t.match(ERROR_OUTPUT, [
['set-script', `Cannot create property 'scripts' on string 'notjson'`],
[' in workspace: workspace-b'],
[` at location: ${resolve(npm.localPrefix, 'workspace-b')}`],
], 'logged workspace-b error')

// workspace-c overwrite a script and logged a warning
const contentsC = fs.readFileSync(resolve(npm.localPrefix, 'workspace-c', 'package.json'))
const dataC = parseJSON(contentsC)
t.hasStrict(dataC, { scripts: { arg1: 'arg2' } }, 'defined the script')
t.equal(dataC[Symbol.for('indent')], ' '.repeat(6), 'kept the correct indent')
t.equal(dataC[Symbol.for('newline')], '\r\n', 'kept the correct newline')
t.match(WARN_OUTPUT, [
['set-script', 'Script "arg1" was overwritten'],
[' in workspace: workspace-c'],
[` at location: ${resolve(npm.localPrefix, 'workspace-c')}`],
], 'logged workspace-c warning')
t.end()
})
})

0 comments on commit 2848c78

Please sign in to comment.