forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
93 lines (75 loc) · 2.29 KB
/
cli.ts
File metadata and controls
93 lines (75 loc) · 2.29 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env node
import process from 'node:process'
import { spawn } from 'node:child_process'
import { defineCommand, runMain } from 'citty'
import { listen } from 'listhen'
import { toNodeListener } from 'h3'
import { createConnectorApp, generateToken, CONNECTOR_VERSION } from './server.ts'
import { getNpmUser } from './npm-client.ts'
import { initLogger, showToken, logInfo, logWarning } from './logger.ts'
const DEFAULT_PORT = 31415
async function runNpmLogin(): Promise<boolean> {
return new Promise(resolve => {
const child = spawn('npm', ['login'], {
stdio: 'inherit',
shell: true,
})
child.on('close', code => {
resolve(code === 0)
})
child.on('error', () => {
resolve(false)
})
})
}
const main = defineCommand({
meta: {
name: 'npmx-connector',
version: CONNECTOR_VERSION,
description: 'Local connector for npmx.dev',
},
args: {
port: {
type: 'string',
description: 'Port to listen on',
default: String(DEFAULT_PORT),
},
},
async run({ args }) {
const port = Number.parseInt(args.port as string, 10) || DEFAULT_PORT
initLogger()
// Check npm authentication before starting
logInfo('Checking npm authentication...')
let npmUser = await getNpmUser()
if (!npmUser) {
logWarning('Not logged in to npm. Starting npm login...')
logWarning('This allows npmx to access your npm cli and any authenticated contexts.')
// oxlint-disable-next-line no-console -- deliberate spacing
console.log()
const loginSuccess = await runNpmLogin()
// oxlint-disable-next-line no-console -- deliberate spacing
console.log()
if (!loginSuccess) {
logWarning('npm login failed or was cancelled.')
process.exit(1)
}
// Check again after login
npmUser = await getNpmUser()
if (!npmUser) {
logWarning('Still not authenticated after login attempt.')
process.exit(1)
}
}
logInfo(`Authenticated as: ${npmUser}`)
const token = generateToken()
showToken(token, port)
const app = createConnectorApp(token)
await listen(toNodeListener(app), {
port,
hostname: '127.0.0.1',
showURL: false,
})
logInfo('Waiting for connection... (Press Ctrl+C to stop)')
},
})
runMain(main)