-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.js
54 lines (46 loc) · 1.4 KB
/
registry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import Config from '@npmcli/config'
import pacote from 'pacote'
import path from 'path'
import { createRequire } from 'node:module'
import { promises as fs } from 'fs'
const require = createRequire(import.meta.url)
export class Registry {
constructor () {
this.loadNpmConfigP = this.loadNpmConfig()
}
async loadNpmConfig () {
// much of this is tediously recreated from staring at `npm` source code
const npmPath = path.dirname(require.resolve('npm'))
const { definitions, flatten, shorthands } = require('@npmcli/config/lib/definitions')
const config = new Config({
npmPath,
definitions,
flatten,
shorthands,
argv: []
})
await config.load()
this.config = config.flat
}
async getPackument (dependency) {
await this.loadNpmConfigP
// fullMetadata is required in order to get publish timestamps
return pacote.packument(dependency, {
...this.config,
fullMetadata: true
})
}
}
export class SubstituteRegistry {
async getPackument (dependency) {
try {
const file = new URL(`tests/fixtures/${dependency}-packument.json`, import.meta.url)
const data = await fs.readFile(file, 'utf8')
return JSON.parse(data)
} catch (e) {
const error = new Error(`SubstituteRegistry: getPackument: unexpected package name "${dependency}"`)
error.statusCode = 404 // like npm
throw error
}
}
}