Skip to content

Commit e498c53

Browse files
committed
fix(cli): auto-login
1 parent 455475d commit e498c53

2 files changed

Lines changed: 38 additions & 22 deletions

File tree

cli/src/cli.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
#!/usr/bin/env node
2+
import { spawn } from 'node:child_process'
23
import { defineCommand, runMain } from 'citty'
34
import { listen } from 'listhen'
45
import { toNodeListener } from 'h3'
56
import { createConnectorApp, generateToken, CONNECTOR_VERSION } from './server.ts'
67
import { getNpmUser } from './npm-client.ts'
7-
import { initLogger, showToken, logInfo, showAuthRequired } from './logger.ts'
8+
import { initLogger, showToken, logInfo, logWarning } from './logger.ts'
89

910
const DEFAULT_PORT = 31415
1011

12+
async function runNpmLogin(): Promise<boolean> {
13+
return new Promise((resolve) => {
14+
const child = spawn('npm', ['login'], {
15+
stdio: 'inherit',
16+
shell: true,
17+
})
18+
19+
child.on('close', (code) => {
20+
resolve(code === 0)
21+
})
22+
23+
child.on('error', () => {
24+
resolve(false)
25+
})
26+
})
27+
}
28+
1129
const main = defineCommand({
1230
meta: {
1331
name: 'npmx-connector',
@@ -28,11 +46,27 @@ const main = defineCommand({
2846

2947
// Check npm authentication before starting
3048
logInfo('Checking npm authentication...')
31-
const npmUser = await getNpmUser()
49+
let npmUser = await getNpmUser()
3250

3351
if (!npmUser) {
34-
showAuthRequired()
35-
process.exit(1)
52+
logWarning('Not logged in to npm. Starting npm login...')
53+
console.log() // Add spacing before npm login prompt
54+
55+
const loginSuccess = await runNpmLogin()
56+
57+
console.log() // Add spacing after npm login
58+
59+
if (!loginSuccess) {
60+
logWarning('npm login failed or was cancelled.')
61+
process.exit(1)
62+
}
63+
64+
// Check again after login
65+
npmUser = await getNpmUser()
66+
if (!npmUser) {
67+
logWarning('Still not authenticated after login attempt.')
68+
process.exit(1)
69+
}
3670
}
3771

3872
logInfo(`Authenticated as: ${npmUser}`)

cli/src/logger.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,6 @@ export function showOutro(message: string): void {
7575
p.outro(message)
7676
}
7777

78-
/**
79-
* Show authentication required error in a box
80-
*/
81-
export function showAuthRequired(): void {
82-
p.note(
83-
[
84-
pc.red('Not logged in to npm'),
85-
'',
86-
'Please run the following command to log in:',
87-
'',
88-
` ${pc.cyan('npm login')}`,
89-
'',
90-
'Then restart the connector.',
91-
].join('\n'),
92-
'Authentication required',
93-
)
94-
}
95-
9678
/**
9779
* Create a spinner for async operations
9880
*/

0 commit comments

Comments
 (0)