Skip to content

Commit 401ebed

Browse files
committed
refactor(hub-ui-onboard): rename confirm option to onConfirm
1 parent ebcbd34 commit 401ebed

10 files changed

Lines changed: 24 additions & 24 deletions

File tree

‎docs/content/1.guide/23.hub-ui-onboard.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ Install changes `package.json` and the lockfile, and a dev server can be reachab
5151

5252
When stdin is not a TTY (CI, a detached container, a task runner that pipes stdin), nobody can answer. The onboarding then refuses at once with [DF9006](/errors/DF9006). Only one question is open at a time: more clicks while it is open share its answer. A `POST` from another origin is refused with `403` before any question.
5353

54-
To ask in another way, pass `confirm`. It gets the `packages`, the exact `command`, and the panel's `request`, and returns `true` to allow the install. For example, a Nuxt module can ask through `consola`:
54+
To ask in another way, pass `onConfirm`. It gets the `packages`, the exact `command`, and the panel's `request`, and returns `true` to allow the install. For example, a Nuxt module can ask through `consola`:
5555

5656
```ts
5757
import { consola } from 'consola'
5858

5959
const onboarding = createOnboarding({
6060
packages: ['@nuxt/devtools'],
61-
confirm: ({ command }) => consola.prompt(`Run \`${command.join(' ')}\` for the browser that asked?`, { type: 'confirm' }),
61+
onConfirm: ({ command }) => consola.prompt(`Run \`${command.join(' ')}\` for the browser that asked?`, { type: 'confirm' }),
6262
})
6363
```
6464

‎docs/content/6.errors/DF9005.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ description: 'A browser asked to run `{command}`. Type y and press Enter to allo
99
1010
## Cause
1111

12-
Install changes `package.json` and the lockfile, and a dev server can be reachable from other devices. So each install that the panel asks for needs the developer's permission. The panel's `POST <base>__onboard/install` waits until you answer. Without a `confirm` option, `createOnboarding()` asks in the terminal that runs the dev server.
12+
Install changes `package.json` and the lockfile, and a dev server can be reachable from other devices. So each install that the panel asks for needs the developer's permission. The panel's `POST <base>__onboard/install` waits until you answer. Without an `onConfirm` option, `createOnboarding()` asks in the terminal that runs the dev server.
1313

1414
## Fix
1515

1616
If you clicked Install, type `y` and press Enter. Any other answer refuses the install, and the panel tells the user. If you did not click Install, press Enter. Another device or page can reach this dev server, so bind it to `localhost`.
1717

18-
To ask in another way, for example with your own logger, pass a `confirm` function to `createOnboarding()`.
18+
To ask in another way, for example with your own logger, pass an `onConfirm` function to `createOnboarding()`.
1919

2020
## Source
2121

‎docs/content/6.errors/DF9006.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ description: 'A browser asked to run `{command}`, but no interactive terminal ca
99
1010
## Cause
1111

12-
Each install that the panel asks for needs the developer's permission ([DF9005](/errors/DF9005)). Without a `confirm` option, `createOnboarding()` asks in the terminal. Here stdin is not a TTY, so nobody can answer. This happens in CI, in a detached container, or when a task runner pipes stdin. The install is refused at once, and the panel tells the user.
12+
Each install that the panel asks for needs the developer's permission ([DF9005](/errors/DF9005)). Without an `onConfirm` option, `createOnboarding()` asks in the terminal. Here stdin is not a TTY, so nobody can answer. This happens in CI, in a detached container, or when a task runner pipes stdin. The install is refused at once, and the panel tells the user.
1313

1414
## Fix
1515

16-
Run the command from the panel in a terminal yourself. Or pass a `confirm` function to `createOnboarding()` that asks in another way.
16+
Run the command from the panel in a terminal yourself. Or pass an `onConfirm` function to `createOnboarding()` that asks in another way.
1717

1818
## Source
1919

‎docs/content/8.references/6.hub-api.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ What `createOnboarding()` from `@devframes/hub-ui-onboard` accepts and serves: [
9898
| `branding` | `{}` | `productName` (default `Devframes`), `logo` (URL or `{ light, dark }`), `primaryColor` |
9999
| `messages` | derived from `productName` | Overrides for `title`, `description`, `install`, `confirming`, `refused`, `hide`, `disable`, `installing`, `restart`, `retry` |
100100
| `onInstalled` | none | Runs after a verified install, or on the first request when the packages already exist; a returned handler takes over `base` |
101-
| `confirm` | `y/N` in the terminal | Gets `{ packages, command, request }` before each install the panel asks for; return `true` to allow it. Refuses when stdin is not a TTY |
101+
| `onConfirm` | `y/N` in the terminal | Gets `{ packages, command, request }` before each install the panel asks for; return `true` to allow it. Refuses when stdin is not a TTY |
102102

103103
| Route | Serves |
104104
| --- | --- |
105105
| `GET <base>embedded.js` | the floating button, a self-contained ES module |
106106
| `GET <base>__onboard/status` | `{ state, command, branding, messages, error? }` |
107-
| `POST <base>__onboard/install` | waits for `confirm`, then starts the install (`202`) or refuses it (`403`); same-origin only |
107+
| `POST <base>__onboard/install` | waits for `onConfirm`, then starts the install (`202`) or refuses it (`403`); same-origin only |
108108
| `POST <base>__onboard/disable` | writes the state file (`204`), same-origin only |
109109

110110
| `state` | Meaning |

‎packages/hub-ui-onboard/src/diagnostics.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const diagnostics = defineDiagnostics({
3535
},
3636
DF9006: {
3737
why: (p: { command: string }) => `A browser asked to run \`${p.command}\`, but no interactive terminal can confirm it. The install was refused.`,
38-
fix: 'Run the command yourself, or pass a `confirm` function to `createOnboarding()`.',
38+
fix: 'Run the command yourself, or pass an `onConfirm` function to `createOnboarding()`.',
3939
},
4040
},
4141
})

‎packages/hub-ui-onboard/src/index.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,14 @@ export function createOnboarding(options: CreateOnboardingOptions): Onboarding {
195195

196196
// The dev server can be reachable from other devices, and a non-browser
197197
// request can send any `Origin`. So the developer allows each install.
198-
const confirm = options.confirm ?? (({ command }) => confirmInTerminal(command))
198+
const onConfirm = options.onConfirm ?? (({ command }) => confirmInTerminal(command))
199199
let asking: Promise<boolean> | undefined
200200

201201
async function startInstall(request: Request): Promise<Response> {
202202
if (state !== 'idle' && state !== 'error')
203203
return new Response(null, { status: 202 })
204204
asking ??= commandLine()
205-
.then(command => confirm({ packages: plan.packages, command, request }))
205+
.then(command => onConfirm({ packages: plan.packages, command, request }))
206206
.finally(() => {
207207
asking = undefined
208208
})

‎packages/hub-ui-onboard/src/terminal-confirm.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import process from 'node:process'
44
import { diagnostics } from './diagnostics'
55

66
/**
7-
* The default `confirm`: a `y/N` question in the terminal that runs the dev
7+
* The default `onConfirm`: a `y/N` question in the terminal that runs the dev
88
* server. Without an interactive terminal nobody can answer, so it refuses.
99
*/
1010
export function confirmInTerminal(command: string[], input: Readable & { isTTY?: boolean } = process.stdin): Promise<boolean> {

‎packages/hub-ui-onboard/src/types.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export interface OnboardingMessages {
1717
/** Short sentence under the heading. */
1818
description: string
1919
install: string
20-
/** Install button label while `confirm` waits for the developer. */
20+
/** Install button label while `onConfirm` waits for the developer. */
2121
confirming: string
22-
/** Shown when `confirm` refused the install. */
22+
/** Shown when `onConfirm` refused the install. */
2323
refused: string
2424
/** Hides the button for this browser tab (`sessionStorage`). */
2525
hide: string
@@ -34,7 +34,7 @@ export interface OnboardingMessages {
3434
/** A web-standard request handler the hub exposes once installed. */
3535
export type OnboardingHandler = (request: Request) => Response | Promise<Response>
3636

37-
/** What `confirm` receives when the panel asks to install. */
37+
/** What `onConfirm` receives when the panel asks to install. */
3838
export interface OnboardingConfirmation {
3939
/** The packages Install adds, as given in `packages`. */
4040
packages: string[]
@@ -71,7 +71,7 @@ export interface CreateOnboardingOptions {
7171
* Default: a `y/N` question in the terminal, or a refusal when stdin is
7272
* not a TTY.
7373
*/
74-
confirm?: (confirmation: OnboardingConfirmation) => boolean | Promise<boolean>
74+
onConfirm?: (confirmation: OnboardingConfirmation) => boolean | Promise<boolean>
7575
}
7676

7777
export type OnboardingState = 'idle' | 'installing' | 'installed' | 'ready' | 'error' | 'disabled'

‎packages/hub-ui-onboard/test/onboarding.test.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ describe('createOnboarding', () => {
108108
expect((await status(origin)).state).toBe('idle')
109109
})
110110

111-
it('installs only after `confirm` allows it', async () => {
111+
it('installs only after `onConfirm` allows it', async () => {
112112
const asked: OnboardingConfirmation[] = []
113113
let allowed = false
114114
const origin = await serve(createOnboarding({
115115
cwd: tempProject(),
116116
packages: ['file:/nonexistent/onboard-missing'],
117-
confirm: (confirmation) => {
117+
onConfirm: (confirmation) => {
118118
asked.push(confirmation)
119119
return allowed
120120
},
@@ -140,7 +140,7 @@ describe('createOnboarding', () => {
140140
const onboarding = createOnboarding({
141141
cwd: tempProject(),
142142
packages: ['x'],
143-
confirm: () => {
143+
onConfirm: () => {
144144
calls++
145145
return new Promise(resolve => answer = resolve)
146146
},
@@ -155,7 +155,7 @@ describe('createOnboarding', () => {
155155
})
156156

157157
it('refuses an install when no terminal can confirm it', async () => {
158-
// Vitest gives its workers a piped stdin, so the default `confirm` sees no TTY.
158+
// Vitest gives its workers a piped stdin, so the default `onConfirm` sees no TTY.
159159
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
160160
cleanups.push(() => warn.mockRestore())
161161
const origin = await serve(createOnboarding({ cwd: tempProject(), packages: ['x'] }))
@@ -183,7 +183,7 @@ describe('createOnboarding', () => {
183183
cwd,
184184
packages: [`file:${FIXTURE}`],
185185
onInstalled: () => () => new Response('hub here'),
186-
confirm: allow,
186+
onConfirm: allow,
187187
})
188188
const origin = await serve(onboarding)
189189

@@ -220,7 +220,7 @@ describe('createOnboarding', () => {
220220

221221
it('reports a failed install as an error the client can retry', async () => {
222222
const cwd = tempProject()
223-
const origin = await serve(createOnboarding({ cwd, packages: ['file:/nonexistent/onboard-missing'], confirm: allow }))
223+
const origin = await serve(createOnboarding({ cwd, packages: ['file:/nonexistent/onboard-missing'], onConfirm: allow }))
224224
await fetch(`${origin}/__devframes/__onboard/install`, { method: 'POST', headers: { origin } })
225225
const final = await waitFor(() => status(origin), s => s.state === 'error')
226226
expect(final.error?.code).toBe('DF9001')
@@ -229,7 +229,7 @@ describe('createOnboarding', () => {
229229

230230
it('asks for a restart when onInstalled returns nothing', async () => {
231231
const cwd = tempProject()
232-
const origin = await serve(createOnboarding({ cwd, packages: [`file:${FIXTURE}`], dev: false, confirm: allow }))
232+
const origin = await serve(createOnboarding({ cwd, packages: [`file:${FIXTURE}`], dev: false, onConfirm: allow }))
233233
await fetch(`${origin}/__devframes/__onboard/install`, { method: 'POST', headers: { origin } })
234234
const final = await waitFor(() => status(origin), settled)
235235
expect(final.state).toBe('installed')

‎tests/__snapshots__/tsnapi/@devframes/hub-ui-onboard/index.snapshot.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface CreateOnboardingOptions {
1111
branding?: OnboardingBranding;
1212
messages?: Partial<OnboardingMessages>;
1313
onInstalled?: () => void | OnboardingHandler | Promise<void | OnboardingHandler>;
14-
confirm?: (_: OnboardingConfirmation) => boolean | Promise<boolean>;
14+
onConfirm?: (_: OnboardingConfirmation) => boolean | Promise<boolean>;
1515
}
1616
export interface Onboarding {
1717
handler: OnboardingHandler;

0 commit comments

Comments
 (0)