yes!
This commit is contained in:
parent
4fa66a2eb9
commit
ca12ec8a9c
5 changed files with 225 additions and 135 deletions
133
cmd/sync.ts
133
cmd/sync.ts
|
@ -1,133 +0,0 @@
|
|||
import type { Command } from "commander";
|
||||
import { readdirSync, readFileSync } from "fs";
|
||||
import * as h from "hjson";
|
||||
import { homedir } from "os";
|
||||
import path from "path";
|
||||
import * as z from "zod/v4";
|
||||
import { isDev } from "../lib";
|
||||
|
||||
const mixDir = isDev
|
||||
? process.cwd()
|
||||
: path.join(homedir(), ".mix");
|
||||
|
||||
const getMixFiles = (): { name: string, content: MixFile }[] => {
|
||||
return readdirSync(mixDir, { withFileTypes: true })
|
||||
.filter(d => d.isFile() && d.name.endsWith("mix.hjson") && !d.name.startsWith("_"))
|
||||
.map(d => ({
|
||||
name: d.name,
|
||||
content: MixFileSchema.parse(
|
||||
h.parse(readFileSync(path.join(mixDir, d.name), "utf-8"))
|
||||
)
|
||||
}));
|
||||
};
|
||||
|
||||
const mergeMixFiles = (files: MixFile[]): MixFile => {
|
||||
const merged: MixFile = {};
|
||||
|
||||
for (const file of files) {
|
||||
for (const group in file) {
|
||||
if (!merged[group]) {
|
||||
merged[group] = { packages: [] };
|
||||
}
|
||||
|
||||
const packageToCheck = file[group]?.packages?.[0];
|
||||
const existingPackage = packageToCheck
|
||||
? merged[group].packages.find(pkg => pkg.id === packageToCheck.id)
|
||||
: undefined;
|
||||
|
||||
if (existingPackage) {
|
||||
throw new Error(`Duplicate package id found: ${packageToCheck!.id} in group ${group}`);
|
||||
}
|
||||
|
||||
if (file[group]?.packages) {
|
||||
merged[group].packages.push(...file[group].packages);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return MixFileSchema.parse(merged);
|
||||
}
|
||||
|
||||
const getLockFile = (): MixLockFile => {
|
||||
return MixLockFileSchema.parse(h.parse(
|
||||
readFileSync(path.join(mixDir, "mix.lock"), "utf-8")
|
||||
));
|
||||
};
|
||||
|
||||
export const registerSyncCommand = (p: Command) => {
|
||||
p
|
||||
.command("sync")
|
||||
.action(() => {
|
||||
const files = getMixFiles();
|
||||
if (files.length === 0) {
|
||||
console.log("No mix files found.");
|
||||
return;
|
||||
}
|
||||
|
||||
const merged = mergeMixFiles(files.map(f => f.content));
|
||||
const lock = getLockFile();
|
||||
|
||||
console.log("files", h.stringify(merged));
|
||||
console.log("lock", h.stringify(lock));
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
type MixFile = {
|
||||
[key: string]: {
|
||||
packages: MixPackage[];
|
||||
};
|
||||
};
|
||||
|
||||
type MixPackage = {
|
||||
id: string;
|
||||
version: string;
|
||||
config: {
|
||||
type: "raw" | "json";
|
||||
path: string;
|
||||
data: unknown;
|
||||
}[]
|
||||
}
|
||||
|
||||
const MixPackageSchema = z.object({
|
||||
id: z.string(),
|
||||
version: z.string(),
|
||||
config: z.array(z.object({
|
||||
type: z.enum(["raw", "json"]),
|
||||
path: z.string(),
|
||||
data: z.unknown()
|
||||
}))
|
||||
.optional()
|
||||
.default([]),
|
||||
});
|
||||
|
||||
const MixFileSchema = z.record(
|
||||
z.string(),
|
||||
z.object({
|
||||
packages: z.array(MixPackageSchema)
|
||||
})
|
||||
);
|
||||
|
||||
type MixLockFile = MixLockPackage[]
|
||||
|
||||
type MixLockPackage = {
|
||||
id: string;
|
||||
version: string;
|
||||
config: {
|
||||
type: "raw" | "json";
|
||||
path: string;
|
||||
data: string;
|
||||
}[];
|
||||
}
|
||||
|
||||
const MixLockPackageSchema = z.object({
|
||||
id: z.string(),
|
||||
version: z.string(),
|
||||
config: z.array(z.object({
|
||||
type: z.enum(["raw", "json"]),
|
||||
path: z.string(),
|
||||
data: z.string()
|
||||
}))
|
||||
});
|
||||
|
||||
const MixLockFileSchema = z.array(MixLockPackageSchema);
|
163
cmd/sync/index.ts
Normal file
163
cmd/sync/index.ts
Normal file
|
@ -0,0 +1,163 @@
|
|||
import { exec } from "child_process";
|
||||
import type { Command } from "commander";
|
||||
import { readdirSync, readFileSync, writeFileSync } from "fs";
|
||||
import * as h from "hjson";
|
||||
import { homedir } from "os";
|
||||
import path from "path";
|
||||
import { isDev } from "../../lib";
|
||||
import { type MixFile, MixFileSchema, type MixLockFile, MixLockFileSchema, type MixLockPackage, type MixPackage } from "./types";
|
||||
|
||||
export const registerSyncCommand = (p: Command) => {
|
||||
p
|
||||
.command("sync")
|
||||
.action(() => {
|
||||
const files = getMixFiles();
|
||||
if (files.length === 0) {
|
||||
console.log("No mix files found.");
|
||||
return;
|
||||
}
|
||||
|
||||
const merged = mergeMixFiles(files.map(f => f.content));
|
||||
const lock = getLockFile();
|
||||
|
||||
const { toInstall, toRemove } = diff(merged, lock)
|
||||
|
||||
if (toInstall.length === 0 && toRemove.length === 0) {
|
||||
console.log("No changes detected. Everything is up to date.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (toInstall.length > 0) {
|
||||
toInstall.forEach(async (pkg) =>{
|
||||
console.log(`Installing ${pkg.id}@${pkg.version}`);
|
||||
await installPackage(lock, pkg.id, pkg.version);
|
||||
});
|
||||
}
|
||||
|
||||
if (toRemove.length > 0) {
|
||||
toRemove.forEach(async (pkg) => {
|
||||
console.log(`Removing ${pkg.id}@${pkg.version}`);
|
||||
await removePackage(lock, pkg.id, pkg.version);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const mixDir = isDev
|
||||
? process.cwd()
|
||||
: path.join(homedir(), ".mix");
|
||||
|
||||
const getMixFiles = (): { name: string, content: MixFile }[] => {
|
||||
return readdirSync(mixDir, { withFileTypes: true })
|
||||
.filter(d => d.isFile() && d.name.endsWith("mix.hjson") && !d.name.startsWith("_"))
|
||||
.map(d => ({
|
||||
name: d.name,
|
||||
content: MixFileSchema.parse(
|
||||
h.parse(readFileSync(path.join(mixDir, d.name), "utf-8"))
|
||||
)
|
||||
}));
|
||||
};
|
||||
|
||||
const mergeMixFiles = (files: MixFile[]): MixFile => {
|
||||
const merged: MixFile = {};
|
||||
|
||||
for (const file of files) {
|
||||
for (const group in file) {
|
||||
if (!merged[group]) {
|
||||
merged[group] = { packages: [] };
|
||||
}
|
||||
|
||||
const packageToCheck = file[group]?.packages?.[0];
|
||||
const existingPackage = packageToCheck
|
||||
? merged[group].packages.find(pkg => pkg.id === packageToCheck.id)
|
||||
: undefined;
|
||||
|
||||
if (existingPackage) {
|
||||
throw new Error(`Duplicate package id found: ${packageToCheck!.id} in group ${group}`);
|
||||
}
|
||||
|
||||
if (file[group]?.packages) {
|
||||
merged[group].packages.push(...file[group].packages);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return MixFileSchema.parse(merged);
|
||||
}
|
||||
|
||||
const getLockFile = (): MixLockFile => {
|
||||
return MixLockFileSchema.parse(h.parse(
|
||||
readFileSync(path.join(mixDir, "mix.lock"), "utf-8")
|
||||
));
|
||||
};
|
||||
|
||||
const saveLockFile = (lock: MixLockFile) => {
|
||||
const lockFilePath = path.join(mixDir, "mix.lock");
|
||||
const lockContent = h.stringify(lock, { space: 2 });
|
||||
writeFileSync(lockFilePath, lockContent, "utf-8");
|
||||
}
|
||||
|
||||
const diff = (mix: MixFile, lock: MixLockFile) => {
|
||||
const mixPkgs = Object.values(mix).flatMap(g => g.packages)
|
||||
|
||||
const toInstall: MixPackage[] = [];
|
||||
const toRemove: MixLockPackage[] = [];
|
||||
|
||||
for (const pkg of mixPkgs) {
|
||||
if (!lock.find(l => l.id === pkg.id && l.version === pkg.version)) {
|
||||
toInstall.push(pkg);
|
||||
}
|
||||
}
|
||||
|
||||
for (const pkg of lock) {
|
||||
if (!mixPkgs.find(m => m.id === pkg.id && m.version === pkg.version)) {
|
||||
toRemove.push(pkg);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
toInstall,
|
||||
toRemove
|
||||
}
|
||||
}
|
||||
|
||||
const installPackage = async (lock: MixLockFile, id: string, version: string) => {
|
||||
await exec(`winget install --id ${id} --version ${version} --exact --silent --force --disable-interactivity`, (error, _, e) => {
|
||||
if (error) {
|
||||
console.error(`Error installing package ${id}@${version}:`, error);
|
||||
return;
|
||||
}
|
||||
|
||||
const pkg: MixLockPackage = {
|
||||
id,
|
||||
version,
|
||||
config: []
|
||||
}
|
||||
|
||||
lock.push(pkg);
|
||||
saveLockFile(lock);
|
||||
|
||||
console.log(`Successfully installed ${id}@${version}`);
|
||||
e && console.error(e);
|
||||
});
|
||||
};
|
||||
|
||||
const removePackage = async (lock: MixLockFile, id: string, version: string) => {
|
||||
exec(`winget remove --id ${id} --version ${version} --exact --silent --force`, (error, _, e) => {
|
||||
if (error) {
|
||||
console.error(`Error removing package ${id}@${version}:`, error);
|
||||
return;
|
||||
}
|
||||
|
||||
const index = lock.findIndex(pkg => pkg.id === id && pkg.version === version);
|
||||
if (index !== -1) {
|
||||
lock.splice(index, 1);
|
||||
saveLockFile(lock);
|
||||
console.log(`Successfully removed ${id}@${version}`);
|
||||
} else {
|
||||
console.warn(`Package ${id}@${version} not found in lock file.`);
|
||||
}
|
||||
|
||||
e && console.error(e);
|
||||
});
|
||||
}
|
60
cmd/sync/types.ts
Normal file
60
cmd/sync/types.ts
Normal file
|
@ -0,0 +1,60 @@
|
|||
import * as z from "zod/v4";
|
||||
|
||||
export type MixFile = {
|
||||
[key: string]: {
|
||||
packages: MixPackage[];
|
||||
};
|
||||
};
|
||||
|
||||
export type MixPackage = {
|
||||
id: string;
|
||||
version: string;
|
||||
config: {
|
||||
type: "raw" | "json";
|
||||
path: string;
|
||||
data: unknown;
|
||||
}[]
|
||||
}
|
||||
|
||||
export const MixPackageSchema = z.object({
|
||||
id: z.string(),
|
||||
version: z.string(),
|
||||
config: z.array(z.object({
|
||||
type: z.enum(["raw", "json"]),
|
||||
path: z.string(),
|
||||
data: z.unknown()
|
||||
}))
|
||||
.optional()
|
||||
.default([]),
|
||||
});
|
||||
|
||||
export const MixFileSchema = z.record(
|
||||
z.string(),
|
||||
z.object({
|
||||
packages: z.array(MixPackageSchema)
|
||||
})
|
||||
);
|
||||
|
||||
export type MixLockFile = MixLockPackage[]
|
||||
|
||||
export type MixLockPackage = {
|
||||
id: string;
|
||||
version: string;
|
||||
config: {
|
||||
type: "raw" | "json";
|
||||
path: string;
|
||||
data: string;
|
||||
}[];
|
||||
}
|
||||
|
||||
export const MixLockPackageSchema = z.object({
|
||||
id: z.string(),
|
||||
version: z.string(),
|
||||
config: z.array(z.object({
|
||||
type: z.enum(["raw", "json"]),
|
||||
path: z.string(),
|
||||
data: z.string()
|
||||
}))
|
||||
});
|
||||
|
||||
export const MixLockFileSchema = z.array(MixLockPackageSchema);
|
2
index.ts
2
index.ts
|
@ -6,7 +6,7 @@ const program = new Command();
|
|||
program
|
||||
.name('mix')
|
||||
.description('a declerative file-based package manager for windows.')
|
||||
.version('0.0.6', '-v, --version', 'output the current version');
|
||||
.version('0.0.7', '-v, --version', 'output the current version');
|
||||
|
||||
registerCommands(program);
|
||||
program.parse();
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@grng/mix",
|
||||
"version": "0.0.6",
|
||||
"version": "0.0.7",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue