mirror of
https://github.com/pnpm/pnpm.git
synced 2025-12-24 23:58:07 -05:00
feat: enable-pre-post-scripts (#3348)
This commit is contained in:
6
.changeset/brave-trees-hammer.md
Normal file
6
.changeset/brave-trees-hammer.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@pnpm/config": minor
|
||||
"@pnpm/plugin-commands-script-runners": minor
|
||||
---
|
||||
|
||||
New option added for: `enable-pre-post-scripts`. When it is set to `true`, lifecycle scripts with pre/post prefixes are automatically executed by pnpm.
|
||||
@@ -63,6 +63,7 @@ export interface Config {
|
||||
dev?: boolean
|
||||
ignoreCurrentPrefs?: boolean
|
||||
recursive?: boolean
|
||||
enablePrePostScripts?: boolean
|
||||
|
||||
// proxy
|
||||
httpProxy?: string
|
||||
|
||||
@@ -36,6 +36,7 @@ export const types = Object.assign({
|
||||
dev: [null, true],
|
||||
dir: String,
|
||||
'enable-modules-dir': Boolean,
|
||||
'enable-pre-post-scripts': Boolean,
|
||||
'fetching-concurrency': Number,
|
||||
filter: [String, Array],
|
||||
'frozen-lockfile': Boolean,
|
||||
|
||||
@@ -111,7 +111,7 @@ For options that may be used with `-r`, see "pnpm help recursive"',
|
||||
export type RunOpts =
|
||||
& Omit<RecursiveRunOpts, 'allProjects' | 'selectedProjectsGraph' | 'workspaceDir'>
|
||||
& { recursive?: boolean }
|
||||
& Pick<Config, 'dir' | 'engineStrict' | 'reporter' | 'scriptShell' | 'shellEmulator'>
|
||||
& Pick<Config, 'dir' | 'engineStrict' | 'reporter' | 'scriptShell' | 'shellEmulator' | 'enablePrePostScripts'>
|
||||
& (
|
||||
& { recursive?: false }
|
||||
& Partial<Pick<Config, 'allProjects' | 'selectedProjectsGraph' | 'workspaceDir'>>
|
||||
@@ -171,7 +171,21 @@ so you may run "pnpm -w ${scriptName}"`,
|
||||
lifecycleOpts.extraEnv = makeNodeRequireOption(pnpPath)
|
||||
}
|
||||
try {
|
||||
if (
|
||||
opts.enablePrePostScripts &&
|
||||
manifest.scripts?.[`pre${scriptName}`] &&
|
||||
!manifest.scripts[scriptName].includes(`pre${scriptName}`)
|
||||
) {
|
||||
await runLifecycleHooks(`pre${scriptName}`, manifest, lifecycleOpts)
|
||||
}
|
||||
await runLifecycleHooks(scriptName, manifest, { ...lifecycleOpts, args: passedThruArgs })
|
||||
if (
|
||||
opts.enablePrePostScripts &&
|
||||
manifest.scripts?.[`post${scriptName}`] &&
|
||||
!manifest.scripts[scriptName].includes(`post${scriptName}`)
|
||||
) {
|
||||
await runLifecycleHooks(`post${scriptName}`, manifest, lifecycleOpts)
|
||||
}
|
||||
} catch (err) {
|
||||
if (opts.bail !== false) {
|
||||
throw err
|
||||
|
||||
@@ -203,6 +203,45 @@ test('restart: run stop, restart and start', async () => {
|
||||
])
|
||||
})
|
||||
|
||||
test('restart: run stop, restart and start and all the pre/post scripts', async () => {
|
||||
prepare({
|
||||
scripts: {
|
||||
poststop: 'node -e "process.stdout.write(\'poststop\')" | json-append ./output.json',
|
||||
prestop: 'node -e "process.stdout.write(\'prestop\')" | json-append ./output.json',
|
||||
stop: 'pnpm prestop && node -e "process.stdout.write(\'stop\')" | json-append ./output.json && pnpm poststop',
|
||||
|
||||
postrestart: 'node -e "process.stdout.write(\'postrestart\')" | json-append ./output.json',
|
||||
prerestart: 'node -e "process.stdout.write(\'prerestart\')" | json-append ./output.json',
|
||||
restart: 'node -e "process.stdout.write(\'restart\')" | json-append ./output.json',
|
||||
|
||||
poststart: 'node -e "process.stdout.write(\'poststart\')" | json-append ./output.json',
|
||||
prestart: 'node -e "process.stdout.write(\'prestart\')" | json-append ./output.json',
|
||||
start: 'node -e "process.stdout.write(\'start\')" | json-append ./output.json',
|
||||
},
|
||||
})
|
||||
|
||||
await execa('pnpm', ['add', 'json-append@1'])
|
||||
await restart.handler({
|
||||
dir: process.cwd(),
|
||||
enablePrePostScripts: true,
|
||||
extraBinPaths: [],
|
||||
rawConfig: {},
|
||||
}, [])
|
||||
|
||||
const { default: scriptsRan } = await import(path.resolve('output.json'))
|
||||
expect(scriptsRan).toStrictEqual([
|
||||
'prestop',
|
||||
'stop',
|
||||
'poststop',
|
||||
'prerestart',
|
||||
'restart',
|
||||
'postrestart',
|
||||
'prestart',
|
||||
'start',
|
||||
'poststart',
|
||||
])
|
||||
})
|
||||
|
||||
test('"pnpm run" prints the list of available commands', async () => {
|
||||
prepare({
|
||||
scripts: {
|
||||
|
||||
Reference in New Issue
Block a user