Skip to content

Commit ea36951

Browse files
committed
fix cli issues
1 parent e6f7485 commit ea36951

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

spec/cli.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ describe('CLI dev command', () => {
5454
let signalHandlers: Record<string, Array<(...args: unknown[]) => void>>
5555

5656
beforeEach(() => {
57+
_spawnCalled = false
5758
tmpDir = mkdtempSync(join(os.tmpdir(), 'nuxt-processor-cli-'))
5859
// minimal package.json
5960
writeFileSync(join(tmpDir, 'package.json'), JSON.stringify({ name: 'app', version: '0.0.0', scripts: {} }, null, 2))
@@ -147,10 +148,29 @@ describe('CLI dev command', () => {
147148
expect(pkg2.scripts && pkg2.scripts['processor:dev']).toBe('nuxt-processor dev')
148149
})
149150

151+
it('exits when entry exists but processor:dev script could not be ensured', async () => {
152+
const entryDir = join(tmpDir, '.nuxt', 'dev', 'workers')
153+
mkdirSync(entryDir, { recursive: true })
154+
writeFileSync(join(entryDir, 'index.mjs'), 'export {}\n')
155+
promptAnswer = 'n'
156+
const { main } = await importCli()
157+
try {
158+
await main({ rawArgs: ['dev', tmpDir] })
159+
}
160+
catch (e) {
161+
expect(String(e)).toContain('process.exit(1)')
162+
}
163+
expect(_spawnCalled).toBe(false)
164+
})
165+
150166
it('kills child process on SIGINT signal', async () => {
151167
const entryDir = join(tmpDir, '.nuxt', 'dev', 'workers')
152168
mkdirSync(entryDir, { recursive: true })
153169
writeFileSync(join(entryDir, 'index.mjs'), 'export {}\n')
170+
const pkgPath = join(tmpDir, 'package.json')
171+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8')) as { scripts?: Record<string, string> }
172+
pkg.scripts = { ...(pkg.scripts || {}), 'processor:dev': 'nuxt-processor dev' }
173+
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2))
154174

155175
promptAnswer = 'n'
156176
const { main } = await importCli()
@@ -175,6 +195,10 @@ describe('CLI dev command', () => {
175195
const entryDir = join(tmpDir, '.nuxt', 'dev', 'workers')
176196
mkdirSync(entryDir, { recursive: true })
177197
writeFileSync(join(entryDir, 'index.mjs'), 'export {}\n')
198+
const pkgPath = join(tmpDir, 'package.json')
199+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8')) as { scripts?: Record<string, string> }
200+
pkg.scripts = { ...(pkg.scripts || {}), 'processor:dev': 'nuxt-processor dev' }
201+
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2))
178202

179203
promptAnswer = 'n'
180204
const { main } = await importCli()

src/cli.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const main = createMain({
4242
const indexFile = resolve(projectRoot, '.nuxt/dev/workers/index.mjs')
4343
const watchDir = resolve(projectRoot, '.nuxt/dev/workers')
4444

45-
await ensureProcessorDevScript(projectRoot)
45+
const scriptEnsured = await ensureProcessorDevScript(projectRoot)
4646

4747
if (!existsSync(indexFile)) {
4848
logger.error('No entry file found at .nuxt/dev/workers/index.mjs')
@@ -51,6 +51,11 @@ export const main = createMain({
5151
process.exit(1)
5252
}
5353

54+
if (!scriptEnsured) {
55+
logger.error('Could not ensure processor:dev script in package.json.')
56+
process.exit(1)
57+
}
58+
5459
const nodeBin = process.execPath
5560
const nodeArgsInput = Array.isArray(args.nodeArgs)
5661
? args.nodeArgs

src/utils/ensure-processor-dev-script.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,19 @@ export async function ensureProcessorDevScript(
3939

4040
logger.warn('No "processor:dev" script found in package.json.')
4141

42-
const answer = options?.ask
43-
? await options.ask()
44-
: await createInterface({ input, output }).question('Add script to package.json? (y/N) ')
42+
let answer: string
43+
if (options?.ask) {
44+
answer = await options.ask()
45+
}
46+
else {
47+
const rl = createInterface({ input, output })
48+
try {
49+
answer = await rl.question('Add script to package.json? (y/N) ')
50+
}
51+
finally {
52+
rl.close()
53+
}
54+
}
4555

4656
const isYes = typeof answer === 'string' && /^y(?:es)?$/i.test(answer.trim())
4757
if (!isYes) {

0 commit comments

Comments
 (0)