Skip to content

Commit

Permalink
feat: support node-options in .npmrc file (#7601)
Browse files Browse the repository at this point in the history
сlose #7596
  • Loading branch information
nachoaldamav committed Feb 1, 2024
1 parent 4511aaf commit 1a3449e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/cold-panthers-jump.md
@@ -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)
1 change: 1 addition & 0 deletions config/config/src/Config.ts
Expand Up @@ -187,6 +187,7 @@ export interface Config {
globalDir?: string
lockfile?: boolean
dedupeInjectedDeps?: boolean
nodeOptions?: string
}

export interface ConfigWithDeprecatedSettings extends Config {
Expand Down
4 changes: 2 additions & 2 deletions exec/plugin-commands-script-runners/src/exec.ts
Expand Up @@ -127,15 +127,14 @@ export async function handler (
opts: Required<Pick<Config, 'selectedProjectsGraph'>> & {
bail?: boolean
unsafePerm?: boolean
rawConfig: object
reverse?: boolean
sort?: boolean
workspaceConcurrency?: number
shellMode?: boolean
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
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions exec/plugin-commands-script-runners/src/run.ts
Expand Up @@ -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'>>
Expand Down Expand Up @@ -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')),
Expand Down
17 changes: 17 additions & 0 deletions exec/plugin-commands-script-runners/test/exec.ts
Expand Up @@ -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',
}),
}))
})
17 changes: 17 additions & 0 deletions exec/plugin-commands-script-runners/test/index.ts
Expand Up @@ -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'])
})

0 comments on commit 1a3449e

Please sign in to comment.