( opts: RunCliOptions, deps: RunCommandDeps = DEFAULT_RUN_COMMAND_DEPS, )
| 175 | } |
| 176 | |
| 177 | export async function handleRunCommand( |
| 178 | opts: RunCliOptions, |
| 179 | deps: RunCommandDeps = DEFAULT_RUN_COMMAND_DEPS, |
| 180 | ): Promise<void> { |
| 181 | const parsed = parseServerOptions(opts); |
| 182 | if (parsed.daemon) { |
| 183 | await startServerDaemon(parsed); |
| 184 | return; |
| 185 | } |
| 186 | // Resolve the persistent token once: it is printed in the ready banner and |
| 187 | // rides in the opened Web UI URL's `#token=` fragment (M5.5). Falls back to |
| 188 | // the plain origin / no token line when unavailable. |
| 189 | const writeReady = (result: { origin: string; reused?: boolean; host?: string }): void => { |
| 190 | const { origin } = result; |
| 191 | const host = result.host ?? parsed.host; |
| 192 | const token = deps.resolveToken?.(); |
| 193 | let output = ''; |
| 194 | if (result.reused === true) { |
| 195 | // A daemon was already running, so this command's --host/--port/etc. did |
| 196 | // not start a new one. Say so loudly, then print the actual running |
| 197 | // server's URLs (using its real bind host, not the requested one). |
| 198 | output += formatReuseNotice(origin); |
| 199 | } |
| 200 | output += |
| 201 | parsed.logLevel === DEFAULT_FOREGROUND_LOG_LEVEL |
| 202 | ? formatReadyBanner(origin, host, { |
| 203 | token, |
| 204 | networkAddresses: deps.networkAddresses, |
| 205 | }) |
| 206 | : formatReadyLine(origin, token); |
| 207 | deps.stdout.write(output); |
| 208 | if (opts.open === true) { |
| 209 | deps.openUrl(token !== undefined ? buildWebUrl(origin, token) : origin); |
| 210 | } |
| 211 | }; |
| 212 | if (opts.foreground === true) { |
| 213 | const run = deps.startServerForeground ?? startServerForeground; |
| 214 | await run(parsed, { |
| 215 | onReady: (origin) => { |
| 216 | writeReady({ origin, reused: false, host: parsed.host }); |
| 217 | }, |
| 218 | }); |
| 219 | return; |
| 220 | } |
| 221 | const result = await deps.startServerBackground(parsed); |
| 222 | writeReady(result); |
| 223 | } |
| 224 | |
| 225 | function formatReuseNotice(origin: string): string { |
| 226 | return ( |
no test coverage detected