Open OAuth login URLs during setup
This commit is contained in:
@@ -3,6 +3,7 @@ import { writeFileSync } from "node:fs";
|
||||
|
||||
import { readJson } from "../pi/settings.js";
|
||||
import { promptChoice, promptText } from "../setup/prompts.js";
|
||||
import { openUrl } from "../system/open-url.js";
|
||||
import { printInfo, printSection, printSuccess, printWarning } from "../ui/terminal.js";
|
||||
import {
|
||||
buildModelStatusSnapshotFromRecords,
|
||||
@@ -126,7 +127,13 @@ export async function loginModelProvider(authPath: string, providerId?: string,
|
||||
await authStorage.login(provider.id, {
|
||||
onAuth: (info: { url: string; instructions?: string }) => {
|
||||
printSection(`Login: ${provider.name ?? provider.id}`);
|
||||
printInfo(`Open this URL: ${info.url}`);
|
||||
const opened = openUrl(info.url);
|
||||
if (opened) {
|
||||
printInfo("Opened the login URL in your browser.");
|
||||
} else {
|
||||
printWarning("Couldn't open your browser automatically.");
|
||||
}
|
||||
printInfo(`Auth URL: ${info.url}`);
|
||||
if (info.instructions) {
|
||||
printInfo(info.instructions);
|
||||
}
|
||||
|
||||
51
src/system/open-url.ts
Normal file
51
src/system/open-url.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { spawn } from "node:child_process";
|
||||
|
||||
import { resolveExecutable } from "./executables.js";
|
||||
|
||||
type ResolveExecutableFn = (name: string, fallbackPaths?: string[]) => string | undefined;
|
||||
|
||||
type OpenUrlCommand = {
|
||||
command: string;
|
||||
args: string[];
|
||||
};
|
||||
|
||||
export function getOpenUrlCommand(
|
||||
url: string,
|
||||
platform = process.platform,
|
||||
resolveCommand: ResolveExecutableFn = resolveExecutable,
|
||||
): OpenUrlCommand | undefined {
|
||||
if (platform === "win32") {
|
||||
return {
|
||||
command: "cmd",
|
||||
args: ["/c", "start", "", url],
|
||||
};
|
||||
}
|
||||
|
||||
if (platform === "darwin") {
|
||||
const command = resolveCommand("open");
|
||||
return command ? { command, args: [url] } : undefined;
|
||||
}
|
||||
|
||||
const command = resolveCommand("xdg-open");
|
||||
return command ? { command, args: [url] } : undefined;
|
||||
}
|
||||
|
||||
export function openUrl(url: string): boolean {
|
||||
const command = getOpenUrlCommand(url);
|
||||
if (!command) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const child = spawn(command.command, command.args, {
|
||||
detached: true,
|
||||
stdio: "ignore",
|
||||
windowsHide: true,
|
||||
});
|
||||
child.on("error", () => {});
|
||||
child.unref();
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user