From ef9ad490a0f391dee05f4d779b0c33c3f6412ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=83=9C?= <2318857637@qq.com> Date: Sat, 13 Jun 2026 13:45:12 +0800 Subject: [PATCH] fix: avoid conflicting prefer config flags --- lib/fetcher.js | 4 ++-- tap-snapshots/test/fetcher.js.test.cjs | 4 ---- test/fetcher.js | 8 ++++++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/fetcher.js b/lib/fetcher.js index 20c0d675..3cdcd188 100644 --- a/lib/fetcher.js +++ b/lib/fetcher.js @@ -104,8 +104,8 @@ class FetcherBase { // a dist-tag, but certainly wants to keep defaulting to latest. this.npmCliConfig = opts.npmCliConfig || [ `--cache=${dirname(this.cache)}`, - `--prefer-offline=${!!this.preferOffline}`, - `--prefer-online=${!!this.preferOnline}`, + ...(this.preferOffline ? ['--prefer-offline=true'] : []), + ...(this.preferOnline ? ['--prefer-online=true'] : []), `--offline=${!!this.offline}`, ...(this.before ? [`--before=${this.before.toISOString()}`] : []), '--no-progress', diff --git a/tap-snapshots/test/fetcher.js.test.cjs b/tap-snapshots/test/fetcher.js.test.cjs index d79d6957..de4976aa 100644 --- a/tap-snapshots/test/fetcher.js.test.cjs +++ b/tap-snapshots/test/fetcher.js.test.cjs @@ -37,8 +37,6 @@ Array [ exports[`test/fetcher.js TAP snapshot the npmInstallCmd and npmInstallConfig > default install config 1`] = ` Array [ "--cache={CACHE}", - "--prefer-offline=false", - "--prefer-online=false", "--offline=false", "--no-progress", "--no-save", @@ -55,8 +53,6 @@ Array [ exports[`test/fetcher.js TAP snapshot the npmInstallCmd and npmInstallConfig > default install config with before 1`] = ` Array [ "--cache={CACHE}", - "--prefer-offline=false", - "--prefer-online=false", "--offline=false", "--before=1979-07-01T19:10:00.000Z", "--no-progress", diff --git a/test/fetcher.js b/test/fetcher.js index 7cc8b52d..1421e574 100644 --- a/test/fetcher.js +++ b/test/fetcher.js @@ -69,6 +69,14 @@ t.test('snapshot the npmInstallCmd and npmInstallConfig', async t => { 'do not have a _cacache folder on cache config passed to npm cli') t.equal(basename(def.cache), '_cacache', 'have a _cacache folder on default pacote config itself') + t.not(def.npmCliConfig.includes('--prefer-offline=false'), 'does not pass disabled prefer-offline') + t.not(def.npmCliConfig.includes('--prefer-online=false'), 'does not pass disabled prefer-online') + const preferOffline = new FileFetcher(abbrevspec, { preferOffline: true }) + t.ok(preferOffline.npmCliConfig.includes('--prefer-offline=true'), 'passes prefer-offline when enabled') + t.not(preferOffline.npmCliConfig.includes('--prefer-online=true'), 'does not pass prefer-online with prefer-offline') + const preferOnline = new FileFetcher(abbrevspec, { preferOnline: true }) + t.ok(preferOnline.npmCliConfig.includes('--prefer-online=true'), 'passes prefer-online when enabled') + t.not(preferOnline.npmCliConfig.includes('--prefer-offline=true'), 'does not pass prefer-offline with prefer-online') const bef = new FileFetcher(abbrevspec, { before: new Date('1979-07-01T19:10:00.000Z'), })