feat: enable-pre-post-scripts (#3348)

This commit is contained in:
Zoltan Kochan
2021-04-14 11:53:22 +03:00
committed by GitHub
parent 0749a7d3e2
commit ba5231ccf2
5 changed files with 62 additions and 1 deletions

View 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.

View File

@@ -63,6 +63,7 @@ export interface Config {
dev?: boolean
ignoreCurrentPrefs?: boolean
recursive?: boolean
enablePrePostScripts?: boolean
// proxy
httpProxy?: string

View File

@@ -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,

View File

@@ -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

View File

@@ -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: {