From a4746cd1acaf462d0d58eefaf189da227aeab6f3 Mon Sep 17 00:00:00 2001 From: reggi Date: Thu, 19 Dec 2024 14:17:54 -0500 Subject: [PATCH] fix: adds ability to --force publish without latest check --- lib/commands/publish.js | 13 ++++++++----- mock-registry/lib/index.js | 20 +++++++++++--------- test/lib/commands/publish.js | 11 +++++++++++ 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/lib/commands/publish.js b/lib/commands/publish.js index c59588fefb241..ff0535f87d915 100644 --- a/lib/commands/publish.js +++ b/lib/commands/publish.js @@ -115,6 +115,7 @@ class Publish extends BaseCommand { // so that we send the latest and greatest thing to the registry // note that publishConfig might have changed as well! manifest = await this.#getManifest(spec, opts, true) + const force = this.npm.config.get('force') const isPreRelease = Boolean(semver.parse(manifest.version).prerelease.length) const isDefaultTag = this.npm.config.isDefault('tag') @@ -157,12 +158,14 @@ class Publish extends BaseCommand { } } - const latestVersion = await this.#latestPublishedVersion(resolved, registry) - const latestSemverIsGreater = !!latestVersion && semver.gte(latestVersion, manifest.version) + if (!force) { + const latestVersion = await this.#latestPublishedVersion(resolved, registry) + const latestSemverIsGreater = !!latestVersion && semver.gte(latestVersion, manifest.version) - if (latestSemverIsGreater && isDefaultTag) { - /* eslint-disable-next-line max-len */ - throw new Error(`Cannot implicitly apply the "latest" tag because published version ${latestVersion} is higher than the new version ${manifest.version}. You must specify a tag using --tag.`) + if (latestSemverIsGreater && isDefaultTag) { + /* eslint-disable-next-line max-len */ + throw new Error(`Cannot implicitly apply the "latest" tag because published version ${latestVersion} is higher than the new version ${manifest.version}. You must specify a tag using --tag.`) + } } const access = opts.access === null ? 'default' : opts.access diff --git a/mock-registry/lib/index.js b/mock-registry/lib/index.js index 3b06681b7ed31..8248631519054 100644 --- a/mock-registry/lib/index.js +++ b/mock-registry/lib/index.js @@ -359,16 +359,18 @@ class MockRegistry { } publish (name, { - packageJson, access, noPut, putCode, manifest, packuments, + packageJson, access, noGet, noPut, putCode, manifest, packuments, } = {}) { - // this getPackage call is used to get the latest semver version before publish - if (manifest) { - this.getPackage(name, { code: 200, resp: manifest }) - } else if (packuments) { - this.getPackage(name, { code: 200, resp: this.manifest({ name, packuments }) }) - } else { - // assumes the package does not exist yet and will 404 x2 from pacote.manifest - this.getPackage(name, { times: 2, code: 404 }) + if (!noGet) { + // this getPackage call is used to get the latest semver version before publish + if (manifest) { + this.getPackage(name, { code: 200, resp: manifest }) + } else if (packuments) { + this.getPackage(name, { code: 200, resp: this.manifest({ name, packuments }) }) + } else { + // assumes the package does not exist yet and will 404 x2 from pacote.manifest + this.getPackage(name, { times: 2, code: 404 }) + } } if (!noPut) { this.putPackage(name, { code: putCode, packageJson, access }) diff --git a/test/lib/commands/publish.js b/test/lib/commands/publish.js index 10dc9b33deda4..39b5292f1b3f2 100644 --- a/test/lib/commands/publish.js +++ b/test/lib/commands/publish.js @@ -920,5 +920,16 @@ t.test('latest dist tag', (t) => { await npm.exec('publish', []) }) + /* eslint-disable-next-line max-len */ + t.test('ALLOWS publish when latest is HIGHER than publishing version and flag --force', async t => { + const version = '99.0.0' + const { npm, registry } = await loadNpmWithRegistry(t, { + ...init(version), + argv: ['--force'], + }) + registry.publish(pkg, { noGet: true, packuments }) + await npm.exec('publish', []) + }) + t.end() })