toes
Version:
47 lines (35 loc) • 1.07 kB
text/typescript
import type { ServerConfig } from "./types";
import { homedir } from "os";
import { promisify } from "util";
import { exec } from "child_process";
import fs from "node:fs";
import os from "node:os";
const configPath = `${homedir()}/.toad/srv-config`;
export function loadConfig(): ServerConfig | undefined {
try {
const cfg = JSON.parse(
fs.readFileSync(configPath).toString()
) as ServerConfig;
if (!cfg.token) return;
cfg.dir = cfg.dir || os.homedir() + "/projects";
cfg.port = cfg.port || 3535;
saveConfig(cfg);
return cfg;
} catch (e) {
return;
}
}
export function saveConfig(cfg: ServerConfig) {
fs.writeFileSync(configPath, JSON.stringify(cfg));
}
export const sexec = (...args: Partial<Parameters<typeof exec>>) => {
return exec(args[0], args[1], (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
if (stdout) console.log(stdout);
if (stderr) console.error(stderr);
});
};
export const execa = promisify(exec);