Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adds ability to --force publish without latest check #7995

Open
wants to merge 1 commit into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
20 changes: 11 additions & 9 deletions mock-registry/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
11 changes: 11 additions & 0 deletions test/lib/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Loading