mirror of
https://github.com/lmstudio-ai/lms.git
synced 2025-09-23 09:44:09 +08:00
Open auth URL automatically in browser on login (#256)
* Open auth URL automatically in browser on login * Pin older version of open for CJS support * Vendor openUrl * Remove open dep * Wrap in try/catch * Promisify
This commit is contained in:
41
src/openUrl.ts
Normal file
41
src/openUrl.ts
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
import { spawn } from "child_process";
|
||||||
|
|
||||||
|
function getCommandForPlatform(): string {
|
||||||
|
switch (process.platform) {
|
||||||
|
case "darwin":
|
||||||
|
return "open";
|
||||||
|
case "win32":
|
||||||
|
return "explorer.exe";
|
||||||
|
case "linux":
|
||||||
|
return "xdg-open";
|
||||||
|
default:
|
||||||
|
return "open";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function openUrl(url: string): Promise<void> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const command = getCommandForPlatform();
|
||||||
|
const child = spawn(command, [url]);
|
||||||
|
let errorText = "";
|
||||||
|
|
||||||
|
child.stderr.setEncoding("utf8");
|
||||||
|
|
||||||
|
child.stderr.on("data", function (data) {
|
||||||
|
errorText += data;
|
||||||
|
});
|
||||||
|
|
||||||
|
child.stderr.on("end", function () {
|
||||||
|
if (errorText.length > 0) {
|
||||||
|
const error = new Error(errorText);
|
||||||
|
reject(error);
|
||||||
|
} else {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
child.on("error", (error) => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -3,6 +3,7 @@ import chalk from "chalk";
|
|||||||
import { boolean, command, flag, option, optional, string } from "cmd-ts";
|
import { boolean, command, flag, option, optional, string } from "cmd-ts";
|
||||||
import { createClient, createClientArgs } from "../createClient.js";
|
import { createClient, createClientArgs } from "../createClient.js";
|
||||||
import { createLogger, logLevelArgs } from "../logLevel.js";
|
import { createLogger, logLevelArgs } from "../logLevel.js";
|
||||||
|
import { openUrl } from "../openUrl.js";
|
||||||
|
|
||||||
export const login = command({
|
export const login = command({
|
||||||
name: "login",
|
name: "login",
|
||||||
@ -69,9 +70,20 @@ export const login = command({
|
|||||||
}
|
}
|
||||||
let askedToAuthenticate = false;
|
let askedToAuthenticate = false;
|
||||||
await client.repository.ensureAuthenticated({
|
await client.repository.ensureAuthenticated({
|
||||||
onAuthenticationUrl: url => {
|
onAuthenticationUrl: async url => {
|
||||||
askedToAuthenticate = true;
|
askedToAuthenticate = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await openUrl(url);
|
||||||
|
logger.infoText`
|
||||||
|
Opening browser for authentication...
|
||||||
|
If a browser window does not open automatically, visit the following URL directly:
|
||||||
|
`
|
||||||
|
;
|
||||||
|
} catch {
|
||||||
logger.info("Please visit the following URL to authenticate:");
|
logger.info("Please visit the following URL to authenticate:");
|
||||||
|
}
|
||||||
|
|
||||||
logger.info();
|
logger.info();
|
||||||
logger.info(chalk.greenBright(` ${url}`));
|
logger.info(chalk.greenBright(` ${url}`));
|
||||||
logger.info();
|
logger.info();
|
||||||
|
Reference in New Issue
Block a user