mirror of
https://github.com/lmstudio-ai/lms.git
synced 2025-09-23 01:33:35 +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);
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user