import * as vscode from "vscode";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { parseGitRemote, isGluecronRemote, type RepoInfo } from "./git";
const execFileP = promisify(execFile);
export function getHost(): string {
const v = vscode.workspace
.getConfiguration("gluecron")
.get<string>("host");
return (v && v.trim()) || "https://gluecron.com";
}
export function getDefaultBranch(): string {
const v = vscode.workspace
.getConfiguration("gluecron")
.get<string>("defaultBranch");
return (v && v.trim()) || "main";
}
export function pickWorkspaceFolder(): vscode.WorkspaceFolder | null {
const editor = vscode.window.activeTextEditor;
if (editor) {
const folder = vscode.workspace.getWorkspaceFolder(editor.document.uri);
if (folder) return folder;
}
const folders = vscode.workspace.workspaceFolders;
if (folders && folders.length > 0) return folders[0];
return null;
}
export async function getRepoInfo(): Promise<RepoInfo | null> {
const folder = pickWorkspaceFolder();
if (!folder) return null;
let url: string;
try {
const { stdout } = await execFileP(
"git",
["remote", "get-url", "origin"],
{ cwd: folder.uri.fsPath }
);
url = stdout.trim();
} catch {
return null;
}
const info = parseGitRemote(url);
if (!info) return null;
if (!isGluecronRemote(info.host, getHost())) {
return info;
}
return info;
}
export async function getGluecronRepoInfo(): Promise<RepoInfo | null> {
const info = await getRepoInfo();
if (!info) return null;
if (!isGluecronRemote(info.host, getHost())) return null;
return info;
}
export function hostUrl(path: string): string {
const host = getHost().replace(/\/+$/, "");
const p = path.startsWith("/") ? path : `/${path}`;
return host + p;
}
|