[READ-ONLY] a fast, modern browser for the npm registry
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(cli): auto-login

+38 -22
+38 -4
cli/src/cli.ts
··· 1 1 #!/usr/bin/env node 2 + import { spawn } from 'node:child_process' 2 3 import { defineCommand, runMain } from 'citty' 3 4 import { listen } from 'listhen' 4 5 import { toNodeListener } from 'h3' 5 6 import { createConnectorApp, generateToken, CONNECTOR_VERSION } from './server.ts' 6 7 import { getNpmUser } from './npm-client.ts' 7 - import { initLogger, showToken, logInfo, showAuthRequired } from './logger.ts' 8 + import { initLogger, showToken, logInfo, logWarning } from './logger.ts' 8 9 9 10 const DEFAULT_PORT = 31415 10 11 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 + 11 29 const main = defineCommand({ 12 30 meta: { 13 31 name: 'npmx-connector', ··· 28 46 29 47 // Check npm authentication before starting 30 48 logInfo('Checking npm authentication...') 31 - const npmUser = await getNpmUser() 49 + let npmUser = await getNpmUser() 32 50 33 51 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 + } 36 70 } 37 71 38 72 logInfo(`Authenticated as: ${npmUser}`)
-18
cli/src/logger.ts
··· 76 76 } 77 77 78 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 - 96 - /** 97 79 * Create a spinner for async operations 98 80 */ 99 81 export function createSpinner() {