Skip to content

Commit

Permalink
remove readline to stop hangup (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
jairad26 authored Oct 24, 2024
1 parent eed085e commit 910dc6f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 23 deletions.
17 changes: 5 additions & 12 deletions src/commands/login/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Command} from '@oclif/core'
import chalk from 'chalk'
import * as fs from 'node:fs'
import * as http from 'node:http'
import {createInterface} from 'node:readline'
import {URL} from 'node:url'
import open from 'open'

Expand Down Expand Up @@ -73,7 +73,7 @@ export default class LoginIndex extends Command {
public async openLoginPage() {
// Open the Hypermode sign-in page in the default browser
const loginUrl = 'https://hypermode.com/app/callback?port=5051&type=cli'
await open(loginUrl)
await open(loginUrl, {wait: false})
}

public async run(): Promise<void> {
Expand All @@ -90,23 +90,16 @@ export default class LoginIndex extends Command {
res.writeHead(200, {'Content-Type': 'text/html'})
res.end(loginHTML)

// Close the server once JWT and email are captured
// Close the server once JWT and eamail are captured
server.close()

const rl = createInterface({
input: process.stdin,
output: process.stdout,
})

const orgs = await sendGraphQLRequest(jwt)
const selectedOrg = await promptOrgSelection(rl, orgs)
const selectedOrg = await promptOrgSelection(orgs)
// Store JWT and email securely
this.writeToEnvFile(jwt, email, selectedOrg.id)

// Confirm successful login in the CLI
this.log('Successfully logged in as ' + email + '! 🎉')

rl.close()
this.log('Successfully logged in as ' + chalk.dim(email) + '! 🎉')
} else {
// Respond with an error if JWT or email is missing
res.writeHead(400, {'Content-Type': 'text/plain'})
Expand Down
10 changes: 1 addition & 9 deletions src/commands/org/switch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Command} from '@oclif/core'
import chalk from 'chalk'
import * as fs from 'node:fs'
import {createInterface} from 'node:readline'

import {
fileExists, getEnvFilePath, promptOrgSelection, readSettingsJson, sendGraphQLRequest,
Expand Down Expand Up @@ -30,13 +29,8 @@ export default class OrgSwitch extends Command {
return
}

const rl = createInterface({
input: process.stdin,
output: process.stdout,
})

const orgs = await sendGraphQLRequest(res.jwt)
const selectedOrg = await promptOrgSelection(rl, orgs)
const selectedOrg = await promptOrgSelection(orgs)

const updatedContent = {
HYP_EMAIL: res.email,
Expand All @@ -45,7 +39,5 @@ export default class OrgSwitch extends Command {
}

fs.writeFileSync(envFilePath, JSON.stringify(updatedContent, null, 2), {flag: 'w'})

rl.close()
}
}
4 changes: 2 additions & 2 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as inquirer from '@inquirer/prompts'
import chalk from 'chalk'
import * as fs from 'node:fs'
import * as path from 'node:path'
import {Interface, createInterface} from 'node:readline'
import {Interface} from 'node:readline'
import fetch from 'node-fetch'

type Org = {
Expand All @@ -19,7 +19,7 @@ export function ask(question: string, rl: Interface, placeholder?: string): Prom
})
}

export async function promptOrgSelection(rl: ReturnType<typeof createInterface>, orgs: Org[]): Promise<Org> {
export async function promptOrgSelection(orgs: Org[]): Promise<Org> {
const choices = orgs.map(org => ({
name: org.slug,
value: org,
Expand Down

0 comments on commit 910dc6f

Please sign in to comment.