|
| 1 | +import os from "node:os"; |
| 2 | +import path from "node:path"; |
| 3 | +import fs from "fs-extra"; |
| 4 | +import {getPlatform} from "../../bindings/utils/getPlatform.js"; |
| 5 | + |
| 6 | +const defaultNpmRegistry = "https://registry.npmjs.org/"; |
| 7 | + |
| 8 | +export async function getCurrentNpmrcConfig() { |
| 9 | + const layers = await getNpmConfigLayers(process.cwd()); |
| 10 | + |
| 11 | + const mergedConfig: Record<string, string> = { |
| 12 | + ...layers.builtin, |
| 13 | + ...layers.global, |
| 14 | + ...layers.user, |
| 15 | + ...layers.project, |
| 16 | + ...layers.env |
| 17 | + }; |
| 18 | + |
| 19 | + return mergedConfig; |
| 20 | +} |
| 21 | + |
| 22 | +export function getNpmrcRegistry(npmrcConfig: Record<string, string>) { |
| 23 | + const registryUrl = npmrcConfig.registry ?? defaultNpmRegistry; |
| 24 | + let cleanRegistryUrl: string = registryUrl; |
| 25 | + |
| 26 | + try { |
| 27 | + const url = new URL(registryUrl); |
| 28 | + url.search = ""; |
| 29 | + url.hash = ""; |
| 30 | + cleanRegistryUrl = url.href; |
| 31 | + } catch (err) { |
| 32 | + // do nothing |
| 33 | + } |
| 34 | + |
| 35 | + if (!cleanRegistryUrl.endsWith("/")) |
| 36 | + cleanRegistryUrl += "/"; |
| 37 | + |
| 38 | + return { |
| 39 | + isDefault: cleanRegistryUrl === defaultNpmRegistry, |
| 40 | + registryUrl, |
| 41 | + cleanRegistryUrl: cleanRegistryUrl |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +async function findNearestProjectNpmrc(startDir: string): Promise<string | undefined> { |
| 46 | + let currentDirPath = path.resolve(startDir); |
| 47 | + |
| 48 | + while (true) { |
| 49 | + const npmrcPath = path.join(currentDirPath, ".npmrc"); |
| 50 | + if (await fs.pathExists(npmrcPath)) |
| 51 | + return npmrcPath; |
| 52 | + |
| 53 | + const parentDirPath = path.dirname(currentDirPath); |
| 54 | + if (parentDirPath === currentDirPath) |
| 55 | + return undefined; |
| 56 | + |
| 57 | + currentDirPath = parentDirPath; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function parseNpmrc(content: string, env: NodeJS.ProcessEnv = process.env): Record<string, string> { |
| 62 | + const result: Record<string, string> = {}; |
| 63 | + |
| 64 | + for (const rawLine of content.split(/\r?\n/u)) { |
| 65 | + const line = rawLine.trim(); |
| 66 | + |
| 67 | + if (line === "" || line.startsWith(";") || line.startsWith("#")) |
| 68 | + continue; |
| 69 | + |
| 70 | + const eqIndex = line.indexOf("="); |
| 71 | + if (eqIndex <= 0) |
| 72 | + continue; |
| 73 | + |
| 74 | + const key = line.slice(0, eqIndex).trim(); |
| 75 | + const value = line.slice(eqIndex + 1) |
| 76 | + .trim() |
| 77 | + .replace(/\$\{([^}]+)\}/gu, (match: string, envVarName: string) => (env[envVarName] ?? "")); |
| 78 | + |
| 79 | + result[key] = value; |
| 80 | + } |
| 81 | + |
| 82 | + return result; |
| 83 | +} |
| 84 | + |
| 85 | +async function readNpmrc(filePath: string | undefined): Promise<Record<string, string>> { |
| 86 | + if (filePath == null || !(await fs.pathExists(filePath))) |
| 87 | + return {}; |
| 88 | + |
| 89 | + return parseNpmrc(fs.readFileSync(filePath, "utf8")); |
| 90 | +} |
| 91 | + |
| 92 | +async function getDefaultGlobalConfigPath(npmPrefixConfig?: string): Promise<string | undefined> { |
| 93 | + const prefix = npmPrefixConfig ?? process.env.PREFIX; |
| 94 | + |
| 95 | + if (prefix != null && prefix !== "") |
| 96 | + return path.join(prefix, "etc", "npmrc"); |
| 97 | + |
| 98 | + const platform = getPlatform(); |
| 99 | + if (platform === "win") { |
| 100 | + const appData = process.env.APPDATA; |
| 101 | + if (appData != null && appData !== "") |
| 102 | + return path.join(appData, "npm", "etc", "npmrc"); |
| 103 | + } else if (platform === "mac") { |
| 104 | + const npmrcLocations = [ |
| 105 | + "/opt/homebrew/etc/npmrc", |
| 106 | + "/usr/local/etc/npmrc" |
| 107 | + ]; |
| 108 | + |
| 109 | + for (const candidate of npmrcLocations) { |
| 110 | + if (await fs.pathExists(candidate)) |
| 111 | + return candidate; |
| 112 | + } |
| 113 | + } else if (platform === "linux") |
| 114 | + return "/etc/npmrc"; |
| 115 | + |
| 116 | + return undefined; |
| 117 | +} |
| 118 | + |
| 119 | +async function getNpmConfigLayers(startDir: string): Promise<NpmConfigLayers> { |
| 120 | + const envConfig: Record<string, string> = {}; |
| 121 | + |
| 122 | + for (const [key, value] of Object.entries(process.env)) { |
| 123 | + if (value == null) |
| 124 | + continue; |
| 125 | + |
| 126 | + const lowerKey = key.toLowerCase(); |
| 127 | + if (lowerKey.startsWith("npm_config_")) { |
| 128 | + const configKey = lowerKey.slice("npm_config_".length).replaceAll("_", "-"); |
| 129 | + envConfig[configKey] = value; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + const globalConfigPath = envConfig["globalconfig"] ?? await getDefaultGlobalConfigPath(envConfig["prefix"]); |
| 134 | + const userConfigPath = envConfig["userconfig"] ?? path.join(os.homedir(), ".npmrc"); |
| 135 | + const projectConfigPath = await findNearestProjectNpmrc(startDir); |
| 136 | + |
| 137 | + const [ |
| 138 | + globalConfig, |
| 139 | + userConfig, |
| 140 | + projectConfig |
| 141 | + ] = await Promise.all([ |
| 142 | + await readNpmrc(globalConfigPath), |
| 143 | + await readNpmrc(userConfigPath), |
| 144 | + await readNpmrc(projectConfigPath) |
| 145 | + ]); |
| 146 | + |
| 147 | + return { |
| 148 | + builtin: { |
| 149 | + registry: defaultNpmRegistry |
| 150 | + }, |
| 151 | + global: globalConfig, |
| 152 | + user: userConfig, |
| 153 | + project: projectConfig, |
| 154 | + env: envConfig, |
| 155 | + paths: { |
| 156 | + project: projectConfigPath, |
| 157 | + user: userConfigPath, |
| 158 | + global: globalConfigPath |
| 159 | + } |
| 160 | + }; |
| 161 | +} |
| 162 | + |
| 163 | +export type NpmConfigLayers = { |
| 164 | + builtin: Record<string, string>, |
| 165 | + global: Record<string, string>, |
| 166 | + user: Record<string, string>, |
| 167 | + project: Record<string, string>, |
| 168 | + env: Record<string, string>, |
| 169 | + paths: { |
| 170 | + project: string | undefined, |
| 171 | + user: string | undefined, |
| 172 | + global: string | undefined |
| 173 | + } |
| 174 | +}; |
0 commit comments