Skip to content

Commit 5ba693c

Browse files
fix(cli): force npmjs registry for login and npm cli operations (#2213)
Co-authored-by: Willow (GHOST) <git@willow.sh>
1 parent 376ccec commit 5ba693c

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

cli/src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as p from '@clack/prompts'
66
import { defineCommand, runMain } from 'citty'
77
import { serve } from 'srvx'
88
import { createConnectorApp, generateToken, CONNECTOR_VERSION } from './server.ts'
9-
import { getNpmUser } from './npm-client.ts'
9+
import { getNpmUser, NPM_REGISTRY_URL } from './npm-client.ts'
1010
import { initLogger, showToken, logInfo, logWarning, logError } from './logger.ts'
1111

1212
const DEFAULT_PORT = 31415
@@ -15,7 +15,7 @@ const DEV_FRONTEND_URL = 'http://127.0.0.1:3000/'
1515

1616
async function runNpmLogin(): Promise<boolean> {
1717
return new Promise(resolve => {
18-
const child = spawn('npm', ['login'], {
18+
const child = spawn('npm', ['login', `--registry=${NPM_REGISTRY_URL}`], {
1919
stdio: 'inherit',
2020
shell: true,
2121
})

cli/src/npm-client.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ import { PackageNameSchema, UsernameSchema, OrgNameSchema, ScopeTeamSchema } fro
1010
import { logCommand, logSuccess, logError, logDebug } from './logger.ts'
1111

1212
const execFileAsync = promisify(execFile)
13+
export const NPM_REGISTRY_URL = 'https://registry.npmjs.org/'
14+
15+
function createNpmEnv(overrides: Record<string, string> = {}): Record<string, string> {
16+
return {
17+
...process.env,
18+
...overrides,
19+
FORCE_COLOR: '0',
20+
npm_config_registry: NPM_REGISTRY_URL,
21+
}
22+
}
1323

1424
/**
1525
* Validates an npm package name using the official npm validation package
@@ -191,10 +201,7 @@ async function execNpmInteractive(
191201
let authUrlTimeout: ReturnType<typeof setTimeout> | null = null
192202
let authUrlTimedOut = false
193203

194-
const env: Record<string, string> = {
195-
...(process.env as Record<string, string>),
196-
FORCE_COLOR: '0',
197-
}
204+
const env = createNpmEnv()
198205

199206
// When openUrls is false, tell npm not to open the browser.
200207
// npm still prints the auth URL and polls doneUrl
@@ -330,7 +337,7 @@ async function execNpm(args: string[], options: ExecNpmOptions = {}): Promise<Np
330337
// On Unix, we keep it false for better security and performance
331338
const { stdout, stderr } = await execFileAsync('npm', npmArgs, {
332339
timeout: 60000,
333-
env: { ...process.env, FORCE_COLOR: '0' },
340+
env: createNpmEnv(),
334341
shell: process.platform === 'win32',
335342
})
336343

@@ -606,7 +613,7 @@ export async function packageInit(
606613
const { stdout, stderr } = await execFileAsync('npm', npmArgs, {
607614
timeout: 60000,
608615
cwd: tempDir,
609-
env: { ...process.env, FORCE_COLOR: '0' },
616+
env: createNpmEnv(),
610617
shell: process.platform === 'win32',
611618
})
612619

0 commit comments

Comments
 (0)