use home dir for init

This commit is contained in:
grngxd 2025-06-24 16:07:02 +01:00
parent f724fc0ecb
commit eac55e01c5
5 changed files with 45 additions and 11 deletions

View file

@ -1,24 +1,29 @@
import type { Command } from "commander";
import { readdirSync, writeFileSync } from "fs";
import * as h from "hjson";
import { homedir } from "os";
import path from "path";
import { isDev } from "../lib";
export const registerInitCommand = (p: Command) => {
p
.command("init")
.action(() => {
const dir = readdirSync(process.cwd(), { withFileTypes: true })
.filter((dirent) => dirent.isFile() && (dirent.name.endsWith("mix.hjson") || dirent.name === "mix.lock"));
.filter((dirent) => dirent.isFile() && (dirent.name.endsWith("mix.hjson") || dirent.name === "mix.lock") && !dirent.name.startsWith("_"));
if (dir.length > 0) {
console.error("Error: Mix project already initialized. Please remove existing .mix.hjson or mix.lock files.");
process.exit(1);
}
writeFileSync("mix.hjson", h.stringify({
// userhomedir/.mix/mix.hjson
const mixDir = isDev ? "" : path.join(homedir(), ".mix");
writeFileSync(path.join(mixDir, "mix.hjson"), h.stringify({
default: {
packages: []
}
}));
writeFileSync("mix.lock", "[]");
writeFileSync(path.join(mixDir, "mix.lock"), "[]");
});
}