Skip to content

Commit f846bf7

Browse files
committed
feat(dev): suggest using nuxt curl when starting a dev server in non-interactive mode
1 parent cd9735e commit f846bf7

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

packages/nuxt-cli/src/dev/shortcuts.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ function printHelp(context: ActionContext): void {
103103
console.log(`\n${lines.join('\n')}\n`)
104104
}
105105

106+
/**
107+
* Without a TTY there are no shortcuts to offer, so point at the way to talk to
108+
* the server instead: non-interactive callers (scripts, agents) otherwise have
109+
* no indication that one exists.
110+
*/
111+
function printRequestHint(): void {
112+
// eslint-disable-next-line no-console
113+
console.log(`\n ${styleText('dim', 'run')} ${styleText('bold', 'nuxt curl /api/hello')} ${styleText('dim', 'to send a request to this server')}\n`)
114+
}
115+
106116
function availableShortcuts(context: ShortcutContext): Shortcut[] {
107117
return shortcuts.filter(shortcut => shortcut.isAvailable?.(context) !== false)
108118
}
@@ -115,6 +125,9 @@ function availableShortcuts(context: ShortcutContext): Shortcut[] {
115125
*/
116126
export function setupShortcuts(context: ShortcutContext): void {
117127
if (!process.stdin.isTTY || isCI || isTest) {
128+
if (!isTest) {
129+
context.onReady(() => printRequestHint())
130+
}
118131
return
119132
}
120133

packages/nuxt-cli/test/unit/shortcuts.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ describe('setupShortcuts', () => {
8888
expect(setup().stdin.listenerCount('data')).toBe(0)
8989
})
9090

91+
it('should suggest `nuxt curl` when there is no TTY', () => {
92+
setup({}, { isTTY: false })
93+
94+
expect(vi.mocked(console.log).mock.calls.join('\n')).toContain('nuxt curl /api/hello')
95+
})
96+
97+
it('should not suggest `nuxt curl` when shortcuts are available', () => {
98+
setup()
99+
100+
expect(vi.mocked(console.log).mock.calls.join('\n')).not.toContain('nuxt curl')
101+
})
102+
91103
it('should take stdin out of raw mode', async () => {
92104
const { stdin, listener, press } = setup({}, { isRaw: true })
93105

0 commit comments

Comments
 (0)