init command

This commit is contained in:
grngxd 2025-06-24 13:56:56 +01:00
parent 1642272fae
commit f724fc0ecb
5 changed files with 251 additions and 220 deletions

View file

@ -9,3 +9,9 @@
- windows 10+ ofc - windows 10+ ofc
- recent version of [Bun](https://bun.sh/) - recent version of [Bun](https://bun.sh/)
- [winget](https://github.com/microsoft/winget-cli) - [winget](https://github.com/microsoft/winget-cli)
## usage
- `bun i mix -g`, then `mix <command> <args>`
- OR `bunx mix <command> <args>`
- `mix init` to create your mixfile & lock file
- `mix sync` to install/remove/update/config packages

View file

@ -1,6 +1,8 @@
import type { Command } from "commander"; import type { Command } from "commander";
import { registerInitCommand } from "./init";
import { registerSyncCommand } from "./sync"; import { registerSyncCommand } from "./sync";
export const registerCommands = (p: Command) => { export const registerCommands = (p: Command) => {
registerSyncCommand(p); registerSyncCommand(p);
registerInitCommand(p);
} }

24
cmd/init.ts Normal file
View file

@ -0,0 +1,24 @@
import type { Command } from "commander";
import { readdirSync, writeFileSync } from "fs";
import * as h from "hjson";
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"));
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({
default: {
packages: []
}
}));
writeFileSync("mix.lock", "[]");
});
}

View file

@ -7,7 +7,7 @@ import * as h from "hjson";
export const registerSyncCommand = (p: Command) => { export const registerSyncCommand = (p: Command) => {
p p
.command("sync") .command("sync")
.action((string: string) => { .action(() => {
const getMixFiles = (): { name: string, content: MixFile}[] => { const getMixFiles = (): { name: string, content: MixFile}[] => {
const files = readdirSync(process.cwd(), { withFileTypes: true }) 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(".hjson") && !dirent.name.startsWith("_"))
@ -28,7 +28,6 @@ export const registerSyncCommand = (p: Command) => {
merged[group] = { packages: [] }; merged[group] = { packages: [] };
} }
// if 2 packages with the same id exist, error out
const packageToCheck = file[group]?.packages?.[0]; const packageToCheck = file[group]?.packages?.[0];
const existingPackage = packageToCheck const existingPackage = packageToCheck
? merged[group].packages.find(pkg => pkg.id === packageToCheck.id) ? merged[group].packages.find(pkg => pkg.id === packageToCheck.id)

View file

@ -4,9 +4,9 @@ import { registerCommands } from './cmd';
const program = new Command(); const program = new Command();
program program
.name('string-util') .name('mix')
.description('CLI to some JavaScript string utilities') .description('a declerative file-based package manager for windows.')
.version('0.8.0'); .version('0.0.1', '-v, --version', 'output the current version');
registerCommands(program); registerCommands(program);