Skip to content

Commit 0f4be49

Browse files
committed
fix: adds registry.publish
1 parent 5149b59 commit 0f4be49

File tree

3 files changed

+101
-133
lines changed

3 files changed

+101
-133
lines changed

mock-registry/lib/index.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,30 @@ class MockRegistry {
351351
}
352352

353353
// full unpublish of an entire package
354-
async unpublish ({ manifest }) {
354+
unpublish ({ manifest }) {
355355
let nock = this.nock
356356
const spec = npa(manifest.name)
357357
nock = nock.delete(this.fullPath(`/${spec.escapedName}/-rev/${manifest._rev}`)).reply(201)
358358
return nock
359359
}
360360

361+
publish (name, {
362+
packageJson, access, noPut, putCode, manifest, packuments,
363+
} = {}) {
364+
// this getPackage call is used to get the latest semver version before publish
365+
if (manifest) {
366+
this.getPackage(name, { code: 200, resp: manifest })
367+
} else if (packuments) {
368+
this.getPackage(name, { code: 200, resp: this.manifest({ name, packuments }) })
369+
} else {
370+
// assumes the package does not exist yet and will 404 x2 from pacote.manifest
371+
this.getPackage(name, { times: 2, code: 404 })
372+
}
373+
if (!noPut) {
374+
this.putPackage(name, { code: putCode, packageJson, access })
375+
}
376+
}
377+
361378
getPackage (name, { times = 1, code = 200, query, resp = {} }) {
362379
let nock = this.nock
363380
nock = nock.get(`/${npa(name).escapedName}`).times(times)
@@ -372,6 +389,48 @@ class MockRegistry {
372389
this.nock = nock
373390
}
374391

392+
putPackage (name, { code = 200, resp = {}, ...putPackagePayload }) {
393+
this.nock.put(`/${npa(name).escapedName}`, body => {
394+
return this.#tap.match(body, this.putPackagePayload({ name, ...putPackagePayload }))
395+
}).reply(code, resp)
396+
}
397+
398+
putPackagePayload (opts) {
399+
const pkg = opts.packageJson
400+
const name = opts.name || pkg?.name
401+
const registry = opts.registry || pkg?.publishConfig?.registry || 'https://registry.npmjs.org'
402+
const access = opts.access || null
403+
404+
const nameProperties = !name ? {} : {
405+
_id: name,
406+
name: name,
407+
}
408+
409+
const packageProperties = !pkg ? {} : {
410+
'dist-tags': { latest: pkg.version },
411+
versions: {
412+
[pkg.version]: {
413+
_id: `${pkg.name}@${pkg.version}`,
414+
dist: {
415+
shasum: /\.*/,
416+
tarball:
417+
`http://${new URL(registry).host}/${pkg.name}/-/${pkg.name}-${pkg.version}.tgz`,
418+
},
419+
...pkg,
420+
},
421+
},
422+
_attachments: {
423+
[`${pkg.name}-${pkg.version}.tgz`]: {},
424+
},
425+
}
426+
427+
return {
428+
access,
429+
...nameProperties,
430+
...packageProperties,
431+
}
432+
}
433+
375434
getTokens (tokens) {
376435
return this.nock.get('/-/npm/v1/tokens')
377436
.reply(200, {

test/fixtures/mock-npm.js

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const os = require('node:os')
22
const fs = require('node:fs').promises
33
const fsSync = require('node:fs')
44
const path = require('node:path')
5-
const npmFetch = require('npm-registry-fetch')
65
const tap = require('tap')
76
const mockLogs = require('./mock-logs.js')
87
const mockGlobals = require('@npmcli/mock-globals')
@@ -464,74 +463,8 @@ function workspaceMock (t, opts) {
464463
}
465464
}
466465

467-
const mockNpmRegistryFetch = (tags) => {
468-
const fetchOpts = {}
469-
const getRequest = async (url, opts) => {
470-
if (fetchOpts[url]) {
471-
fetchOpts[url].push(opts)
472-
} else {
473-
fetchOpts[url] = [opts]
474-
}
475-
const find = ({ ...tags })[url]
476-
if (!find) {
477-
throw new Error(`no npm-registry-fetch mock for ${url}`)
478-
}
479-
if (typeof find === 'function') {
480-
return find()
481-
}
482-
return find
483-
}
484-
const nrf = async (url, opts) => {
485-
return {
486-
json: () => getRequest(url, opts),
487-
}
488-
}
489-
const mock = Object.assign(nrf, npmFetch, { json: getRequest })
490-
const mocks = { 'npm-registry-fetch': mock }
491-
const getOpts = (url) => fetchOpts[url]
492-
return { mocks, mock, fetchOpts, getOpts }
493-
}
494-
495-
const putPackagePayload = (opts) => {
496-
const package = opts.packageJson
497-
const name = opts.name || package?.name
498-
const registry = opts.registry || package?.publishConfig?.registry || 'https://registry.npmjs.org'
499-
const access = opts.access || null
500-
501-
const nameProperties = !name ? {} : {
502-
_id: name,
503-
name: name,
504-
}
505-
506-
const packageProperties = !package ? {} : {
507-
'dist-tags': { latest: package.version },
508-
versions: {
509-
[package.version]: {
510-
_id: `${package.name}@${package.version}`,
511-
dist: {
512-
shasum: /\.*/,
513-
tarball:
514-
`http://${new URL(registry).host}/${package.name}/-/${package.name}-${package.version}.tgz`,
515-
},
516-
...package,
517-
},
518-
},
519-
_attachments: {
520-
[`${package.name}-${package.version}.tgz`]: {},
521-
},
522-
}
523-
524-
return {
525-
access,
526-
...nameProperties,
527-
...packageProperties,
528-
}
529-
}
530-
531466
module.exports = setupMockNpm
532467
module.exports.load = setupMockNpm
533468
module.exports.setGlobalNodeModules = setGlobalNodeModules
534469
module.exports.loadNpmWithRegistry = loadNpmWithRegistry
535470
module.exports.workspaceMock = workspaceMock
536-
module.exports.mockNpmRegistryFetch = mockNpmRegistryFetch
537-
module.exports.putPackagePayload = putPackagePayload

0 commit comments

Comments
 (0)