-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.mjs
More file actions
65 lines (61 loc) · 1.81 KB
/
config.mjs
File metadata and controls
65 lines (61 loc) · 1.81 KB
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
55
56
57
58
59
60
61
62
63
64
65
import { createReadStream } from 'node:fs'
import { writeFile } from 'node:fs/promises'
import { normalize, join } from 'node:path'
import { homedir } from 'node:os'
import {
REGISTRIES,
getRegistryFromStream,
setRegistryFromStream,
} from './registry.mjs'
import { isFile } from './utils.mjs'
import { readNrmrc } from './nrmrc.mjs'
/**
* Set current registry
* @param {boolean|undefined} local
* @param {string} registryUrl
*/
export async function setRegistry(local, registryUrl) {
const filePath = await getConfigPath(local)
try {
const fileStream = createReadStream(filePath)
const result = await setRegistryFromStream(fileStream, registryUrl)
return writeFile(filePath, result)
} catch {
return writeFile(filePath, `registry=${registryUrl}`)
}
}
/**
* Get current registry
* @param {boolean=} local
*/
export async function getRegistry(local) {
const filePath = await getConfigPath(local)
try {
const fileStream = createReadStream(filePath) // the file may not exists
return (await getRegistryFromStream(fileStream)) || REGISTRIES['npm']
} catch {
// when rc file not found, fallback registry to default
return REGISTRIES['npm']
}
}
/**
* If `local` is not provided, check if local npmrc file exists
* @param {boolean=} local
* @noexcept
*/
export async function getConfigPath(local) {
const rc = '.npmrc'
const detectLocal = typeof local !== 'boolean' && (await isFile(rc))
if (local || detectLocal) {
return rc
}
return join(normalize(homedir()), rc).replace(/\\/g, '/')
}
/**
* Returns Map to keep order
*/
export async function getAllRegistries() {
const all = new Map(Object.entries(REGISTRIES))
for (const [k, v] of await readNrmrc().catch(() => [])) all.set(k, v)
return all
}