feat(set-script): implement workspaces · npm/cli@ec520ce (original) (raw)
`@@ -3,6 +3,7 @@ const fs = require('fs')
`
3
3
`const parseJSON = require('json-parse-even-better-errors')
`
4
4
`const rpj = require('read-package-json-fast')
`
5
5
`const { resolve } = require('path')
`
``
6
`+
const getWorkspaces = require('./workspaces/get-workspaces.js')
`
6
7
``
7
8
`const BaseCommand = require('./base-command.js')
`
8
9
`class SetScript extends BaseCommand {
`
`@@ -11,6 +12,11 @@ class SetScript extends BaseCommand {
`
11
12
`return 'Set tasks in the scripts section of package.json'
`
12
13
`}
`
13
14
``
``
15
`+
/* istanbul ignore next - see test/lib/load-all-commands.js */
`
``
16
`+
static get params () {
`
``
17
`+
return ['workspace', 'workspaces']
`
``
18
`+
}
`
``
19
+
14
20
`/* istanbul ignore next - see test/lib/load-all-commands.js */
`
15
21
`static get name () {
`
16
22
`return 'set-script'
`
`@@ -31,49 +37,90 @@ class SetScript extends BaseCommand {
`
31
37
`}
`
32
38
`}
`
33
39
``
34
``
`-
exec (args, cb) {
`
35
``
`-
this.set(args).then(() => cb()).catch(cb)
`
36
``
`-
}
`
37
``
-
38
``
`-
async set (args) {
`
``
40
`+
validate (args) {
`
39
41
`if (process.env.npm_lifecycle_event === 'postinstall')
`
40
42
`throw new Error('Scripts can’t set from the postinstall script')
`
41
43
``
42
44
`// Parse arguments
`
43
45
`if (args.length !== 2)
`
44
46
`` throw new Error(Expected 2 arguments: got ${args.length})
``
``
47
`+
}
`
45
48
``
``
49
`+
exec (args, cb) {
`
``
50
`+
this.set(args).then(() => cb()).catch(cb)
`
``
51
`+
}
`
``
52
+
``
53
`+
async set (args) {
`
``
54
`+
this.validate(args)
`
``
55
`+
const warn = this.setScript(this.npm.localPrefix, args[0], args[1])
`
``
56
`+
if (warn)
`
``
57
`` +
log.warn('set-script', Script "${args[0]}" was overwritten)
``
``
58
`+
}
`
``
59
+
``
60
`+
execWorkspaces (args, filters, cb) {
`
``
61
`+
this.setWorkspaces(args, filters).then(() => cb()).catch(cb)
`
``
62
`+
}
`
``
63
+
``
64
`+
async setWorkspaces (args, filters) {
`
``
65
`+
this.validate(args)
`
``
66
`+
const workspaces =
`
``
67
`+
await getWorkspaces(filters, { path: this.npm.localPrefix })
`
``
68
+
``
69
`+
for (const [name, path] of workspaces) {
`
``
70
`+
try {
`
``
71
`+
const warn = this.setScript(path, args[0], args[1])
`
``
72
`+
if (warn) {
`
``
73
`` +
log.warn('set-script', Script "${args[0]}" was overwritten)
``
``
74
`` +
log.warn( in workspace: ${name})
``
``
75
`` +
log.warn( at location: ${path})
``
``
76
`+
}
`
``
77
`+
} catch (err) {
`
``
78
`+
log.error('set-script', err.message)
`
``
79
`` +
log.error( in workspace: ${name})
``
``
80
`` +
log.error( at location: ${path})
``
``
81
`+
process.exitCode = 1
`
``
82
`+
}
`
``
83
`+
}
`
``
84
`+
}
`
``
85
+
``
86
`+
// returns a Boolean that will be true if
`
``
87
`+
// the requested script was overwritten
`
``
88
`+
// and false if it was set as a new script
`
``
89
`+
setScript (path, name, value) {
`
46
90
`// Set the script
`
47
91
`let manifest
`
48
92
`let warn = false
`
``
93
+
49
94
`try {
`
50
``
`-
manifest = fs.readFileSync(this.npm.localPrefix + '/package.json', 'utf-8')
`
``
95
`+
manifest = fs.readFileSync(resolve(path, 'package.json'), 'utf-8')
`
51
96
`} catch (error) {
`
52
97
`throw new Error('package.json not found')
`
53
98
`}
`
``
99
+
54
100
`try {
`
55
101
`manifest = parseJSON(manifest)
`
56
102
`} catch (error) {
`
57
103
`` throw new Error(Invalid package.json: ${error})
``
58
104
`}
`
``
105
+
59
106
`if (!manifest.scripts)
`
60
107
`manifest.scripts = {}
`
61
``
`-
if (manifest.scripts[args[0]] && manifest.scripts[args[0]] !== args[1])
`
``
108
+
``
109
`+
if (manifest.scripts[name] && manifest.scripts[name] !== value)
`
62
110
`warn = true
`
63
``
`-
manifest.scripts[args[0]] = args[1]
`
``
111
`+
manifest.scripts[name] = value
`
``
112
+
64
113
`// format content
`
65
``
`-
const packageJsonInfo = await rpj(this.npm.localPrefix + '/package.json')
`
66
114
`const {
`
67
115
`[Symbol.for('indent')]: indent,
`
68
116
`[Symbol.for('newline')]: newline,
`
69
``
`-
} = packageJsonInfo
`
70
``
`-
const format = indent === undefined ? ' ' : indent
`
71
``
`-
const eol = newline === undefined ? '\n' : newline
`
72
``
`-
const content = (JSON.stringify(manifest, null, format) + '\n')
`
73
``
`-
.replace(/\n/g, eol)
`
74
``
`-
fs.writeFileSync(this.npm.localPrefix + '/package.json', content)
`
75
``
`-
if (warn)
`
76
``
`` -
log.warn('set-script', Script "${args[0]}" was overwritten)
``
``
117
`+
} = manifest
`
``
118
+
``
119
`+
const content = (JSON.stringify(manifest, null, indent) + '\n')
`
``
120
`+
.replace(/\n/g, newline)
`
``
121
`+
fs.writeFileSync(resolve(path, 'package.json'), content)
`
``
122
+
``
123
`+
return warn
`
77
124
`}
`
78
125
`}
`
79
126
`module.exports = SetScript
`