Skip to content

Commit

Permalink
fix(cli): doctor command no longer causes hangups
Browse files Browse the repository at this point in the history
The way "readline" was being used caused hangups in other commands.

This should be closed after being used but it wasn't on other commands. It was being created regardless by the very existence of the imports. We now only create the interface inside the command action!
  • Loading branch information
Pkmmte committed Jan 24, 2023
1 parent 4996897 commit 57cc832
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-fans-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@waveplay/pilot': patch
---

fix(cli): doctor command no longer causes hangups
18 changes: 10 additions & 8 deletions packages/pilot/src/cli/commands/doctor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import fs from 'fs-extra'
import readline from 'readline'
import type { Logger } from 'pino'

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

const API_ROUTE = 'pilot/[...route]'

const API_ROUTE_CONTENTS = `import { createHandler } from '@waveplay/pilot/api'\nexport default createHandler()\n`
Expand All @@ -36,10 +31,17 @@ export async function action(options: OptionValues) {
}
})

// This is used to prompt the user for input
// Don't create it outside of this function, or it will cause commands to hang
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})

// Check if the API path exists in either .js or .ts
const API_PATH = process.cwd() + `/pages/api/${API_ROUTE}`
if (!fs.existsSync(`${API_PATH}.js`) && !fs.existsSync(`${API_PATH}.ts`)) {
const answer = await prompt(logger, 'API route does not exist! This is necessary for many features to work. Would you like to create it? (y/n)')
const answer = await prompt(logger, rl, 'API route does not exist! This is necessary for many features to work. Would you like to create it? (y/n)')
if (answer === 'y') {
await fs.outputFile(`${API_PATH}.js`, API_ROUTE_CONTENTS)
logger.info('[PilotJS] API route created!')
Expand All @@ -49,14 +51,14 @@ export async function action(options: OptionValues) {
rl.close()
}

async function prompt(logger: Logger, question: string) {
async function prompt(logger: Logger, rl, question: string) {
return new Promise<string>(resolve => {
rl.question(question, answer => {
if (answer === 'y' || answer === 'n') {
resolve(answer)
} else {
logger.warn('[PilotJS] Please enter "y" or "n"')
prompt(logger, question).then(resolve)
prompt(logger, rl, question).then(resolve)
}
})
})
Expand Down

0 comments on commit 57cc832

Please sign in to comment.