Skip to content

Commit 9dfef52

Browse files
committed
replace rc with @pnpm/npm-conf
1 parent 47b1d24 commit 9dfef52

File tree

4 files changed

+44
-102
lines changed

4 files changed

+44
-102
lines changed

index.ts

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import pkg from "./package.json" with {type: "json"};
99
import {parse, coerce, diff, gt, gte, lt, neq, valid, validRange} from "semver";
1010
import {timerel} from "timerel";
1111
import {npmTypes, poetryTypes, uvTypes, goTypes, parseUvDependencies, nonPackageEngines} from "./utils.ts";
12-
import type {default as registryAuthToken, AuthOptions} from "registry-auth-token";
12+
import type {default as registryAuthToken} from "registry-auth-token";
1313

1414
export type Config = {
1515
/** Array of packages to include */
@@ -24,17 +24,6 @@ export type Config = {
2424
cooldown?: number,
2525
};
2626

27-
type Npmrc = {
28-
registry: string,
29-
ca?: string,
30-
cafile?: string,
31-
cert?: string,
32-
certfile?: string,
33-
key?: string,
34-
keyfile?: string,
35-
[other: string]: any,
36-
};
37-
3827
type Dep = {
3928
old: string,
4029
new: string,
@@ -90,6 +79,7 @@ const esc = (str: string) => str.replace(/[|\\{}()[\]^$+*?.-]/g, "\\$&");
9079
const normalizeUrl = (url: string) => url.endsWith("/") ? url.substring(0, url.length - 1) : url;
9180
const packageVersion = pkg.version;
9281
const sep = "\0";
82+
const defaultRegistry = "https://registry.npmjs.org";
9383

9484
const modeByFileName: Record<string, string> = {
9585
"package.json": "npm",
@@ -201,11 +191,6 @@ function matchesAny(str: string, set: Set<RegExp> | boolean): boolean {
201191
return false;
202192
}
203193

204-
function registryUrl(scope: string, npmrc: Npmrc): string {
205-
const url: string = npmrc[`${scope}:registry`] || npmrc.registry;
206-
return url.endsWith("/") ? url : `${url}/`;
207-
}
208-
209194
function getProperty(obj: Record<string, any>, path: string): Record<string, any> {
210195
return path.split(".").reduce((obj: Record<string, any>, prop: string) => obj?.[prop] ?? null, obj);
211196
}
@@ -231,33 +216,38 @@ type AuthAndRegistry = {
231216
registry: string,
232217
};
233218

234-
const defaultRegistry = "https://registry.npmjs.org";
235-
let authOpts: AuthOptions | null = null;
236-
let npmrc: Npmrc | null = null;
237-
let rat: typeof registryAuthToken | null = null;
219+
type Conf = {config: {get: (key: string) => string | undefined}};
220+
let conf: Conf | undefined;
221+
let rat: typeof registryAuthToken | undefined;
222+
223+
async function getRegistry(scope?: string): Promise<string> {
224+
if (!conf) conf = (await import("@pnpm/npm-conf")).default();
225+
226+
let url: string | undefined;
227+
if (scope) {
228+
url = conf!.config.get(`${scope}:registry`) || conf!.config.get(`registry`) || defaultRegistry;
229+
} else {
230+
url = conf!.config.get(`registry`) || defaultRegistry;
231+
}
238232

239-
async function getNpmrc() {
240-
if (npmrc) return npmrc;
241-
return (await import("rc")).default("npm", {registry: defaultRegistry});
233+
return url.endsWith("/") ? url : `${url}/`;
242234
}
243235

244236
async function getAuthAndRegistry(name: string, registry: string): Promise<AuthAndRegistry> {
245-
if (!npmrc) npmrc = await getNpmrc();
246-
if (!authOpts) authOpts = {npmrc, recursive: true};
247237
if (!rat) rat = (await import("registry-auth-token")).default;
248238

249239
if (!name.startsWith("@")) {
250-
return {auth: rat(registry, authOpts), registry};
240+
return {auth: rat(registry, {recursive: true}), registry};
251241
} else {
252242
const scope = (/@[a-z0-9][\w-.]+/.exec(name) || [""])[0];
253-
const url = normalizeUrl(registryUrl(scope, npmrc));
243+
const url = normalizeUrl(await getRegistry(scope));
254244
if (url !== registry) {
255245
try {
256-
const newAuth = rat(url, authOpts);
246+
const newAuth = rat(url, {recursive: true});
257247
if (newAuth?.token) return {auth: newAuth, registry: url};
258248
} catch {}
259249
}
260-
return {auth: rat(registry, authOpts), registry};
250+
return {auth: rat(registry, {recursive: true}), registry};
261251
}
262252
}
263253

@@ -301,9 +291,8 @@ async function doFetch(url: string, opts?: RequestInit): Promise<Response> {
301291
type PackageInfo = [Record<string, any>, string, string | null, string];
302292

303293
async function fetchNpmInfo(name: string, type: string, config: Config): Promise<PackageInfo> {
304-
if (!npmrc) npmrc = await getNpmrc();
305294
const originalRegistry = normalizeUrl((typeof args.registry === "string" ? args.registry : false) ||
306-
config.registry || npmrc.registry || defaultRegistry,
295+
config.registry || (await getRegistry()),
307296
);
308297

309298
const {auth, registry} = await getAuthAndRegistry(name, originalRegistry);

package-lock.json

Lines changed: 19 additions & 68 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
"node": ">=22"
1717
},
1818
"devDependencies": {
19+
"@pnpm/npm-conf": "3.0.1",
1920
"@types/node": "24.10.2",
20-
"@types/rc": "1.2.4",
2121
"@types/semver": "7.7.1",
2222
"@typescript/native-preview": "7.0.0-dev.20251209.1",
2323
"eslint": "9.39.1",
2424
"eslint-config-silverwind": "112.2.2",
2525
"nano-spawn": "2.0.0",
2626
"p-map": "7.0.4",
27-
"rc": "1.2.8",
2827
"registry-auth-token": "5.1.0",
2928
"restana": "5.1.0",
3029
"semver": "7.7.3",

types.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module "@pnpm/npm-conf" {
2+
export = any;
3+
}

0 commit comments

Comments
 (0)