From 6995303687ab59541b727bf611f73624d1829b6c Mon Sep 17 00:00:00 2001 From: reggi Date: Thu, 17 Oct 2024 17:14:28 -0400 Subject: [PATCH] feat!: adds `--ignore-scripts` flag to `pack` BREAKING CHANGE: `--ignore-scripts` now applies to all lifecycle scripts, include `prepare` --- lib/commands/pack.js | 1 + tap-snapshots/test/lib/docs.js.test.cjs | 3 +- workspaces/arborist/lib/arborist/rebuild.js | 4 ++- workspaces/arborist/test/arborist/rebuild.js | 35 ++++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/commands/pack.js b/lib/commands/pack.js index 6a1bb53acb8aa..bd190d88d82b6 100644 --- a/lib/commands/pack.js +++ b/lib/commands/pack.js @@ -15,6 +15,7 @@ class Pack extends BaseCommand { 'workspace', 'workspaces', 'include-workspace-root', + 'ignore-scripts', ] static usage = [''] diff --git a/tap-snapshots/test/lib/docs.js.test.cjs b/tap-snapshots/test/lib/docs.js.test.cjs index 8e1c57be0bf93..9a75dd12b04bb 100644 --- a/tap-snapshots/test/lib/docs.js.test.cjs +++ b/tap-snapshots/test/lib/docs.js.test.cjs @@ -3680,7 +3680,7 @@ npm pack Options: [--dry-run] [--json] [--pack-destination ] [-w|--workspace [-w|--workspace ...]] -[-ws|--workspaces] [--include-workspace-root] +[-ws|--workspaces] [--include-workspace-root] [--ignore-scripts] Run "npm help pack" for more info @@ -3694,6 +3694,7 @@ npm pack #### \`workspace\` #### \`workspaces\` #### \`include-workspace-root\` +#### \`ignore-scripts\` ` exports[`test/lib/docs.js TAP usage ping > must match snapshot 1`] = ` diff --git a/workspaces/arborist/lib/arborist/rebuild.js b/workspaces/arborist/lib/arborist/rebuild.js index 82f84772f9a85..3340ddaa67067 100644 --- a/workspaces/arborist/lib/arborist/rebuild.js +++ b/workspaces/arborist/lib/arborist/rebuild.js @@ -154,7 +154,9 @@ module.exports = cls => class Builder extends cls { // links should run prepare scripts and only link bins after that if (type === 'links') { - await this.#runScripts('prepare') + if (!this.options.ignoreScripts) { + await this.#runScripts('prepare') + } } if (this.options.binLinks) { await this.#linkAllBins() diff --git a/workspaces/arborist/test/arborist/rebuild.js b/workspaces/arborist/test/arborist/rebuild.js index 7be5d0059cf5a..9d906f4dedd25 100644 --- a/workspaces/arborist/test/arborist/rebuild.js +++ b/workspaces/arborist/test/arborist/rebuild.js @@ -812,3 +812,38 @@ t.test('no workspaces', async t => { }, ]) }) + +t.test('do not run lifecycle scripts of linked deps twice', async t => { + const testdir = t.testdir({ + project: { + 'package.json': JSON.stringify({ + name: 'my-project', + version: '1.0.0', + dependencies: { + foo: 'file:../foo', + }, + }), + node_modules: { + foo: t.fixture('symlink', '../../foo'), + }, + }, + foo: { + 'package.json': JSON.stringify({ + name: 'foo', + version: '1.0.0', + scripts: { + postinstall: 'echo "ok"', + }, + }), + }, + }) + + const path = resolve(testdir, 'project') + const Arborist = t.mock('../../lib/arborist/index.js', { + '@npmcli/run-script': () => { + throw new Error('should not run any scripts') + }, + }) + const arb = new Arborist({ path, ignoreScripts: true }) + await arb.rebuild() +})