feat: support node-options in .npmrc file (#7601)

сlose #7596
This commit is contained in:
Nacho Aldama
2024-02-01 13:21:03 +01:00
committed by GitHub
parent 4511aaf6e9
commit 1a3449e940
6 changed files with 51 additions and 4 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-script-runners": patch
"pnpm": patch
---
support `node-options` option inside `.npmrc` file when running scripts [#7596](https://github.com/pnpm/pnpm/issues/7596)

View File

@@ -187,6 +187,7 @@ export interface Config {
globalDir?: string
lockfile?: boolean
dedupeInjectedDeps?: boolean
nodeOptions?: string
}
export interface ConfigWithDeprecatedSettings extends Config {

View File

@@ -127,7 +127,6 @@ export async function handler (
opts: Required<Pick<Config, 'selectedProjectsGraph'>> & {
bail?: boolean
unsafePerm?: boolean
rawConfig: object
reverse?: boolean
sort?: boolean
workspaceConcurrency?: number
@@ -135,7 +134,7 @@ export async function handler (
resumeFrom?: string
reportSummary?: boolean
implicitlyFellbackFromRun?: boolean
} & Pick<Config, 'extraBinPaths' | 'extraEnv' | 'lockfileDir' | 'modulesDir' | 'dir' | 'userAgent' | 'recursive' | 'workspaceDir'>,
} & Pick<Config, 'extraBinPaths' | 'extraEnv' | 'lockfileDir' | 'modulesDir' | 'dir' | 'userAgent' | 'recursive' | 'workspaceDir' | 'nodeOptions'>,
params: string[]
) {
// For backward compatibility
@@ -201,6 +200,7 @@ export async function handler (
extraEnv: {
...extraEnv,
PNPM_PACKAGE_NAME: opts.selectedProjectsGraph[prefix]?.package.manifest.name,
...(opts.nodeOptions ? { NODE_OPTIONS: opts.nodeOptions } : {}),
},
prependPaths,
userAgent: opts.userAgent,

View File

@@ -149,7 +149,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' | 'extraBinPaths' | 'reporter' | 'scriptsPrependNodePath' | 'scriptShell' | 'shellEmulator' | 'enablePrePostScripts' | 'userAgent' | 'extraEnv'>
& Pick<Config, 'dir' | 'engineStrict' | 'extraBinPaths' | 'reporter' | 'scriptsPrependNodePath' | 'scriptShell' | 'shellEmulator' | 'enablePrePostScripts' | 'userAgent' | 'extraEnv' | 'nodeOptions'>
& (
& { recursive?: false }
& Partial<Pick<Config, 'allProjects' | 'selectedProjectsGraph' | 'workspaceDir'>>
@@ -224,10 +224,16 @@ so you may run "pnpm -w run ${scriptName}"`,
})
}
const concurrency = opts.workspaceConcurrency ?? 4
const extraEnv = {
...opts.extraEnv,
...(opts.nodeOptions ? { NODE_OPTIONS: opts.nodeOptions } : {}),
}
const lifecycleOpts: RunLifecycleHookOptions = {
depPath: dir,
extraBinPaths: opts.extraBinPaths,
extraEnv: opts.extraEnv,
extraEnv,
pkgRoot: dir,
rawConfig: opts.rawConfig,
rootModulesDir: await realpathMissing(path.join(dir, 'node_modules')),

View File

@@ -24,3 +24,20 @@ test('exec should set npm_config_user_agent', async () => {
}),
}))
})
test('exec should set the NODE_OPTIONS env var', async () => {
prepareEmpty()
await exec.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
selectedProjectsGraph: {},
nodeOptions: '--max-old-space-size=4096',
}, ['eslint'])
expect(execa).toBeCalledWith('eslint', [], expect.objectContaining({
env: expect.objectContaining({
NODE_OPTIONS: '--max-old-space-size=4096',
}),
}))
})

View File

@@ -609,3 +609,20 @@ test('pnpm run with slightly incorrect command suggests correct one', async () =
hint: 'Command "buil" not found. Did you mean "pnpm run build"?',
}))
})
test('pnpm run with custom node-options', async () => {
prepare({
scripts: {
build: 'node -e "if (process.env.NODE_OPTIONS !== \'--max-old-space-size=1200\') { process.exit(1) }"',
},
})
await run.handler({
dir: process.cwd(),
extraBinPaths: [],
extraEnv: {},
rawConfig: {},
nodeOptions: '--max-old-space-size=1200',
workspaceConcurrency: 1,
}, ['build'])
})