/
run.ts
361 lines (329 loc) · 10.8 KB
/
run.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import path from 'path'
import pLimit from 'p-limit'
import {
docsUrl,
readProjectManifestOnly,
tryReadProjectManifest,
} from '@pnpm/cli-utils'
import { type CompletionFunc } from '@pnpm/command'
import { FILTERING, UNIVERSAL_OPTIONS } from '@pnpm/common-cli-options-help'
import { type Config, types as allTypes } from '@pnpm/config'
import { PnpmError } from '@pnpm/error'
import {
runLifecycleHook,
makeNodeRequireOption,
type RunLifecycleHookOptions,
} from '@pnpm/lifecycle'
import { type PackageScripts, type ProjectManifest } from '@pnpm/types'
import pick from 'ramda/src/pick'
import realpathMissing from 'realpath-missing'
import renderHelp from 'render-help'
import { runRecursive, type RecursiveRunOpts, getSpecifiedScripts as getSpecifiedScriptWithoutStartCommand } from './runRecursive'
import { existsInDir } from './existsInDir'
import { handler as exec } from './exec'
import { buildCommandNotFoundHint } from './buildCommandNotFoundHint'
export const IF_PRESENT_OPTION = {
'if-present': Boolean,
}
export const IF_PRESENT_OPTION_HELP = {
description: 'Avoid exiting with a non-zero exit code when the script is undefined',
name: '--if-present',
}
export const PARALLEL_OPTION_HELP = {
description: 'Completely disregard concurrency and topological sorting, \
running a given script immediately in all matching packages \
with prefixed streaming output. This is the preferred flag \
for long-running processes such as watch run over many packages.',
name: '--parallel',
}
export const RESUME_FROM_OPTION_HELP = {
description: 'Command executed from given package',
name: '--resume-from',
}
export const SEQUENTIAL_OPTION_HELP = {
description: 'Run the specified scripts one by one',
name: '--sequential',
}
export const REPORT_SUMMARY_OPTION_HELP = {
description: 'Save the execution results of every package to "pnpm-exec-summary.json". Useful to inspect the execution time and status of each package.',
name: '--report-summary',
}
export const shorthands = {
parallel: [
'--workspace-concurrency=Infinity',
'--no-sort',
'--stream',
'--recursive',
],
sequential: [
'--workspace-concurrency=1',
],
}
export function rcOptionsTypes () {
return {
...pick([
'npm-path',
'use-node-version',
], allTypes),
}
}
export function cliOptionsTypes () {
return {
...pick([
'bail',
'sort',
'unsafe-perm',
'use-node-version',
'workspace-concurrency',
'scripts-prepend-node-path',
], allTypes),
...IF_PRESENT_OPTION,
recursive: Boolean,
reverse: Boolean,
'resume-from': String,
'report-summary': Boolean,
}
}
export const completion: CompletionFunc = async (cliOpts, params) => {
if (params.length > 0) {
return []
}
const manifest = await readProjectManifestOnly(cliOpts.dir as string ?? process.cwd(), cliOpts)
return Object.keys(manifest.scripts ?? {}).map((name) => ({ name }))
}
export const commandNames = ['run', 'run-script']
export function help () {
return renderHelp({
aliases: ['run-script'],
description: 'Runs a defined package script.',
descriptionLists: [
{
title: 'Options',
list: [
{
description: 'Run the defined package script in every package found in subdirectories \
or every workspace package, when executed inside a workspace. \
For options that may be used with `-r`, see "pnpm help recursive"',
name: '--recursive',
shortAlias: '-r',
},
{
description: 'The command will exit with a 0 exit code even if the script fails',
name: '--no-bail',
},
IF_PRESENT_OPTION_HELP,
PARALLEL_OPTION_HELP,
RESUME_FROM_OPTION_HELP,
...UNIVERSAL_OPTIONS,
SEQUENTIAL_OPTION_HELP,
REPORT_SUMMARY_OPTION_HELP,
],
},
FILTERING,
],
url: docsUrl('run'),
usages: ['pnpm run <command> [<args>...]'],
})
}
export type RunOpts =
& Omit<RecursiveRunOpts, 'allProjects' | 'selectedProjectsGraph' | 'workspaceDir'>
& { recursive?: boolean }
& Pick<Config, 'dir' | 'engineStrict' | 'extraBinPaths' | 'reporter' | 'scriptsPrependNodePath' | 'scriptShell' | 'shellEmulator' | 'enablePrePostScripts' | 'userAgent' | 'extraEnv'>
& (
& { recursive?: false }
& Partial<Pick<Config, 'allProjects' | 'selectedProjectsGraph' | 'workspaceDir'>>
| { recursive: true }
& Required<Pick<Config, 'allProjects' | 'selectedProjectsGraph' | 'workspaceDir'>>
)
& {
argv?: {
original: string[]
}
fallbackCommandUsed?: boolean
}
export async function handler (
opts: RunOpts,
params: string[]
) {
let dir: string
const [scriptName, ...passedThruArgs] = params
if (opts.recursive) {
if (scriptName || Object.keys(opts.selectedProjectsGraph).length > 1) {
return runRecursive(params, opts)
}
dir = Object.keys(opts.selectedProjectsGraph)[0]
} else {
dir = opts.dir
}
const manifest = await readProjectManifestOnly(dir, opts)
if (!scriptName) {
const rootManifest = opts.workspaceDir && opts.workspaceDir !== dir
? (await tryReadProjectManifest(opts.workspaceDir, opts)).manifest
: undefined
return printProjectCommands(manifest, rootManifest ?? undefined)
}
const specifiedScripts = getSpecifiedScripts(manifest.scripts ?? {}, scriptName)
if (specifiedScripts.length < 1) {
if (opts.ifPresent) return
if (opts.fallbackCommandUsed) {
if (opts.argv == null) throw new Error('Could not fallback because opts.argv.original was not passed to the script runner')
return exec({
selectedProjectsGraph: {},
...opts,
}, opts.argv.original.slice(1))
}
if (opts.workspaceDir) {
const { manifest: rootManifest } = await tryReadProjectManifest(opts.workspaceDir, opts)
if (getSpecifiedScripts(rootManifest?.scripts ?? {}, scriptName).length > 0 && specifiedScripts.length < 1) {
throw new PnpmError('NO_SCRIPT', `Missing script: ${scriptName}`, {
hint: `But script matched with ${scriptName} is present in the root of the workspace,
so you may run "pnpm -w run ${scriptName}"`,
})
}
}
throw new PnpmError('NO_SCRIPT', `Missing script: ${scriptName}`, {
hint: buildCommandNotFoundHint(scriptName, manifest.scripts),
})
}
const lifecycleOpts: RunLifecycleHookOptions = {
depPath: dir,
extraBinPaths: opts.extraBinPaths,
extraEnv: opts.extraEnv,
pkgRoot: dir,
rawConfig: opts.rawConfig,
rootModulesDir: await realpathMissing(path.join(dir, 'node_modules')),
scriptsPrependNodePath: opts.scriptsPrependNodePath,
scriptShell: opts.scriptShell,
silent: opts.reporter === 'silent',
shellEmulator: opts.shellEmulator,
stdio: 'inherit',
unsafePerm: true, // when running scripts explicitly, assume that they're trusted.
}
const existsPnp = existsInDir.bind(null, '.pnp.cjs')
const pnpPath = (opts.workspaceDir && await existsPnp(opts.workspaceDir)) ??
await existsPnp(dir)
if (pnpPath) {
lifecycleOpts.extraEnv = {
...lifecycleOpts.extraEnv,
...makeNodeRequireOption(pnpPath),
}
}
try {
const limitRun = pLimit(opts.workspaceConcurrency ?? 4)
const _runScript = runScript.bind(null, { manifest, lifecycleOpts, runScriptOptions: { enablePrePostScripts: opts.enablePrePostScripts ?? false }, passedThruArgs })
await Promise.all(specifiedScripts.map(script => limitRun(() => _runScript(script))))
} catch (err: any) { // eslint-disable-line
if (opts.bail !== false) {
throw err
}
}
return undefined
}
const ALL_LIFECYCLE_SCRIPTS = new Set([
'prepublish',
'prepare',
'prepublishOnly',
'prepack',
'postpack',
'publish',
'postpublish',
'preinstall',
'install',
'postinstall',
'preuninstall',
'uninstall',
'postuninstall',
'preversion',
'version',
'postversion',
'pretest',
'test',
'posttest',
'prestop',
'stop',
'poststop',
'prestart',
'start',
'poststart',
'prerestart',
'restart',
'postrestart',
'preshrinkwrap',
'shrinkwrap',
'postshrinkwrap',
])
function printProjectCommands (
manifest: ProjectManifest,
rootManifest?: ProjectManifest
) {
const lifecycleScripts = [] as string[][]
const otherScripts = [] as string[][]
for (const [scriptName, script] of Object.entries(manifest.scripts ?? {})) {
if (ALL_LIFECYCLE_SCRIPTS.has(scriptName)) {
lifecycleScripts.push([scriptName, script])
} else {
otherScripts.push([scriptName, script])
}
}
if (lifecycleScripts.length === 0 && otherScripts.length === 0) {
return 'There are no scripts specified.'
}
let output = ''
if (lifecycleScripts.length > 0) {
output += `Lifecycle scripts:\n${renderCommands(lifecycleScripts)}`
}
if (otherScripts.length > 0) {
if (output !== '') output += '\n\n'
output += `Commands available via "pnpm run":\n${renderCommands(otherScripts)}`
}
if ((rootManifest?.scripts) == null) {
return output
}
const rootScripts = Object.entries(rootManifest.scripts)
if (rootScripts.length === 0) {
return output
}
if (output !== '') output += '\n\n'
output += `Commands of the root workspace project (to run them, use "pnpm -w run"):
${renderCommands(rootScripts)}`
return output
}
export interface RunScriptOptions {
enablePrePostScripts: boolean
}
export const runScript: (opts: {
manifest: ProjectManifest
lifecycleOpts: RunLifecycleHookOptions
runScriptOptions: RunScriptOptions
passedThruArgs: string[]
}, scriptName: string) => Promise<void> = async function (opts, scriptName) {
if (
opts.runScriptOptions.enablePrePostScripts &&
opts.manifest.scripts?.[`pre${scriptName}`] &&
!opts.manifest.scripts[scriptName].includes(`pre${scriptName}`)
) {
await runLifecycleHook(`pre${scriptName}`, opts.manifest, opts.lifecycleOpts)
}
await runLifecycleHook(scriptName, opts.manifest, { ...opts.lifecycleOpts, args: opts.passedThruArgs })
if (
opts.runScriptOptions.enablePrePostScripts &&
opts.manifest.scripts?.[`post${scriptName}`] &&
!opts.manifest.scripts[scriptName].includes(`post${scriptName}`)
) {
await runLifecycleHook(`post${scriptName}`, opts.manifest, opts.lifecycleOpts)
}
}
function renderCommands (commands: string[][]) {
return commands.map(([scriptName, script]) => ` ${scriptName}\n ${script}`).join('\n')
}
function getSpecifiedScripts (scripts: PackageScripts, scriptName: string) {
const specifiedSelector = getSpecifiedScriptWithoutStartCommand(scripts, scriptName)
if (specifiedSelector.length > 0) {
return specifiedSelector
}
// if a user passes start command as scriptName, `node server.js` will be executed as a fallback, so return start command even if start command is not defined in package.json
if (scriptName === 'start') {
return [scriptName]
}
return []
}