mirror of
https://github.com/Nystik-gh/ignis.git
synced 2026-06-17 04:35:53 +00:00
62 lines
1.3 KiB
JavaScript
62 lines
1.3 KiB
JavaScript
const { spawn, execSync } = require("child_process");
|
|
const os = require("os");
|
|
|
|
const isWindows = process.platform === "win32";
|
|
|
|
function checkInstalled() {
|
|
try {
|
|
const output = execSync("ob --version", {
|
|
stdio: "pipe",
|
|
windowsHide: true,
|
|
})
|
|
.toString()
|
|
.trim();
|
|
|
|
return { installed: true, version: output || "unknown" };
|
|
} catch {
|
|
return { installed: false, version: null };
|
|
}
|
|
}
|
|
|
|
function spawnOb(args, opts = {}) {
|
|
return spawn("ob", args, {
|
|
env: { ...process.env, HOME: os.homedir() },
|
|
shell: isWindows,
|
|
windowsHide: true,
|
|
...opts,
|
|
});
|
|
}
|
|
|
|
function runCommand(args, opts = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
let stdout = "";
|
|
let stderr = "";
|
|
|
|
const proc = spawnOb(args, opts);
|
|
|
|
proc.stdout.on("data", (data) => {
|
|
stdout += data.toString();
|
|
});
|
|
|
|
proc.stderr.on("data", (data) => {
|
|
stderr += data.toString();
|
|
});
|
|
|
|
proc.on("close", (code) => {
|
|
if (code === 0) {
|
|
resolve({ stdout, stderr });
|
|
} else {
|
|
reject(
|
|
new Error(`ob ${args[0]} failed (code ${code}): ${stderr || stdout}`),
|
|
);
|
|
}
|
|
});
|
|
|
|
proc.on("error", (err) => {
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
|
|
module.exports = { checkInstalled, spawnOb, runCommand };
|