diff --git a/cmd/init.ts b/cmd/init.ts index 18da2a0..1f410d7 100644 --- a/cmd/init.ts +++ b/cmd/init.ts @@ -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"), "[]"); }); } \ No newline at end of file diff --git a/cmd/sync.ts b/cmd/sync.ts index 6ac554b..f45401d 100644 --- a/cmd/sync.ts +++ b/cmd/sync.ts @@ -3,6 +3,9 @@ import type { Command } from "commander"; import { createHash } from "crypto"; import { readdirSync, readFileSync, writeFileSync } from "fs"; import * as h from "hjson"; +import { homedir } from "os"; +import path from "path"; +import { isDev } from "../lib"; export const registerSyncCommand = (p: Command) => { p @@ -10,7 +13,7 @@ export const registerSyncCommand = (p: Command) => { .action(() => { const getMixFiles = (): { name: string, content: MixFile}[] => { const files = readdirSync(process.cwd(), { withFileTypes: true }) - .filter((dirent) => dirent.isFile() && dirent.name.endsWith(".hjson") && !dirent.name.startsWith("_")) + .filter((dirent) => dirent.isFile() && dirent.name.endsWith("mix.hjson") && !dirent.name.startsWith("_")) .map((dirent) => ({ name: dirent.name, content: h.parse(readFileSync(dirent.name, "utf-8")) @@ -106,7 +109,7 @@ export const registerSyncCommand = (p: Command) => { for (const pkg of installing) { console.log(`Installing ${pkg.id} version ${pkg.version}`); - exec(`winget install ${pkg.id} --version ${pkg.version}`, (error, stdout, stderr) => { + exec(`winget install --id ${pkg.id} --version ${pkg.version}`, (error, stdout, stderr) => { if (error) { console.error(`Error installing ${pkg.id}: ${error.message}`); return; @@ -126,7 +129,8 @@ export const registerSyncCommand = (p: Command) => { lock.push(l); const content = h.stringify(lock); - writeFileSync("mix.lock", content, "utf-8"); + const mixDir = isDev ? "" : path.join(homedir(), ".mix"); + writeFileSync(path.join(mixDir, "mix.lock"), content, "utf-8"); }); } @@ -147,7 +151,8 @@ export const registerSyncCommand = (p: Command) => { if (index !== -1) { lock.splice(index, 1); const content = h.stringify(lock); - writeFileSync("mix.lock", content, "utf-8"); + const mixDir = isDev ? "" : path.join(homedir(), ".mix"); + writeFileSync(path.join(mixDir, "mix.lock"), content, "utf-8"); } }); } @@ -179,7 +184,8 @@ export const registerSyncCommand = (p: Command) => { } const content = h.stringify(lock); - writeFileSync("mix.lock", content, "utf-8"); + const mixDir = isDev ? "" : path.join(homedir(), ".mix"); + writeFileSync(path.join(mixDir, "mix.lock"), content, "utf-8"); }); } }); diff --git a/index.ts b/index.ts index 699b5f4..9ac76c6 100644 --- a/index.ts +++ b/index.ts @@ -6,8 +6,7 @@ const program = new Command(); program .name('mix') .description('a declerative file-based package manager for windows.') - .version('0.0.1', '-v, --version', 'output the current version'); + .version('0.0.4', '-v, --version', 'output the current version'); registerCommands(program); - program.parse(); \ No newline at end of file diff --git a/lib.ts b/lib.ts new file mode 100644 index 0000000..0aee5b0 --- /dev/null +++ b/lib.ts @@ -0,0 +1,17 @@ +export const environment: "prod" | "dev" = (() => { + const arg1 = process.argv[1] || ""; + + if (arg1.includes("bunx")) return "prod"; + + if ( + arg1.includes(".bun\\install\\global") || + arg1.includes(".bun/install/global") + ) { + return "prod"; + } + + return "dev"; +})(); + +export const isProd = environment === "prod"; +export const isDev = environment === "dev"; \ No newline at end of file diff --git a/package.json b/package.json index d5524b7..2c1b556 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,12 @@ { - "name": "wem", + "name": "@grng/mix", + "version": "0.0.4", + "publishConfig": { + "access": "public" + }, + "bin": { + "mix": "index.ts" + }, "module": "index.ts", "type": "module", "devDependencies": {