constatic
Version:
Constatic is a CLI for creating and managing modern TypeScript projects, providing an organized structure and features that streamline development.
55 lines (53 loc) • 1.33 kB
JavaScript
// src/cli/shell.ts
import { Result } from "#lib/result.js";
import { execSync, spawn } from "node:child_process";
class CLIShell {
get agent() {
const userAgent = (process.env.npm_config_user_agent ?? "").toLowerCase();
for (const agent of ["npm", "bun", "yarn", "pnpm"]) {
if (userAgent.startsWith(agent))
return agent;
}
return "npm";
}
get isBun() {
return this.agent === "bun";
}
run(cwd, ...args) {
return new Promise((resolve) => {
const child = spawn(this.agent, args, {
shell: true,
cwd,
env: {
...process.env,
NODE_ENV: "development",
DISABLE_OPENCOLLECTIVE: "1"
}
});
child.on("exit", (code) => {
return resolve(code === 0 ? Result.ok(code, "code") : Result.fail("fail", code ?? undefined));
});
child.on("error", (error) => {
return resolve(Result.fail(error.message, 1));
});
});
}
isRepoDirty(cwd) {
try {
let stdout = execSync("git status --porcelain", {
encoding: "utf-8",
cwd,
stdio: "pipe"
});
return stdout.trim() !== "";
} catch (error) {
if (error?.toString?.().includes("not a git repository")) {
return false;
}
return true;
}
}
}
export {
CLIShell
};