diff --git a/src/api/install.ts b/src/api/install.ts index 96486735cc..b09c604db7 100644 --- a/src/api/install.ts +++ b/src/api/install.ts @@ -130,7 +130,14 @@ async function installInContext (installType: string, packagesToInstall: Depende } await linkBins(path.join(ctx.root, 'node_modules')) if (!opts.ignoreScripts && ctx.pkg) { - await mainPostInstall(ctx.pkg && ctx.pkg.scripts || {}, ctx.root, opts.production) + const scripts = ctx.pkg && ctx.pkg.scripts || {} + + if (scripts['postinstall']) { + npmRun('postinstall', ctx.root) + } + if (installType === 'general' && scripts['prepublish']) { + npmRun('prepublish', ctx.root) + } } } @@ -186,11 +193,6 @@ function adaptConfig (opts: StrictPnpmOptions) { } } -function mainPostInstall (scripts: Object, pkgRoot: string, isProductionInstall: boolean) { - if (scripts['postinstall']) npmRun('postinstall', pkgRoot) - if (!isProductionInstall && scripts['prepublish']) npmRun('prepublish', pkgRoot) -} - function npmRun (scriptName: string, pkgRoot: string) { const result = runScriptSync('npm', ['run', scriptName], { cwd: pkgRoot, diff --git a/test/install.ts b/test/install.ts index 39c5137421..17ca0ca636 100644 --- a/test/install.ts +++ b/test/install.ts @@ -508,7 +508,7 @@ test('postinstall is executed after installation', t => { t.end() }) -test('prepublish is executed after installation', t => { +test('prepublish is not executed after installation with arguments', t => { prepare({ scripts: { prepublish: 'echo "Hello world!"' @@ -517,6 +517,21 @@ test('prepublish is executed after installation', t => { const result = spawnSync('ts-node', [pnpmBin, 'install', 'is-negative']) + t.equal(result.status, 0, 'installation was successfull') + t.ok(result.stdout.toString().indexOf('Hello world!') === -1, 'prepublish script was not executed') + + t.end() +}) + +test('prepublish is executed after argumentless installation', t => { + prepare({ + scripts: { + prepublish: 'echo "Hello world!"' + } + }) + + const result = spawnSync('ts-node', [pnpmBin, 'install']) + t.equal(result.status, 0, 'installation was successfull') t.ok(result.stdout.toString().indexOf('Hello world!') !== -1, 'prepublish script was executed')