use home dir for init
This commit is contained in:
parent
f724fc0ecb
commit
eac55e01c5
5 changed files with 45 additions and 11 deletions
11
cmd/init.ts
11
cmd/init.ts
|
@ -1,24 +1,29 @@
|
||||||
import type { Command } from "commander";
|
import type { Command } from "commander";
|
||||||
import { readdirSync, writeFileSync } from "fs";
|
import { readdirSync, writeFileSync } from "fs";
|
||||||
import * as h from "hjson";
|
import * as h from "hjson";
|
||||||
|
import { homedir } from "os";
|
||||||
|
import path from "path";
|
||||||
|
import { isDev } from "../lib";
|
||||||
|
|
||||||
export const registerInitCommand = (p: Command) => {
|
export const registerInitCommand = (p: Command) => {
|
||||||
p
|
p
|
||||||
.command("init")
|
.command("init")
|
||||||
.action(() => {
|
.action(() => {
|
||||||
const dir = readdirSync(process.cwd(), { withFileTypes: true })
|
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) {
|
if (dir.length > 0) {
|
||||||
console.error("Error: Mix project already initialized. Please remove existing .mix.hjson or mix.lock files.");
|
console.error("Error: Mix project already initialized. Please remove existing .mix.hjson or mix.lock files.");
|
||||||
process.exit(1);
|
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: {
|
default: {
|
||||||
packages: []
|
packages: []
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
writeFileSync("mix.lock", "[]");
|
writeFileSync(path.join(mixDir, "mix.lock"), "[]");
|
||||||
});
|
});
|
||||||
}
|
}
|
16
cmd/sync.ts
16
cmd/sync.ts
|
@ -3,6 +3,9 @@ import type { Command } from "commander";
|
||||||
import { createHash } from "crypto";
|
import { createHash } from "crypto";
|
||||||
import { readdirSync, readFileSync, writeFileSync } from "fs";
|
import { readdirSync, readFileSync, writeFileSync } from "fs";
|
||||||
import * as h from "hjson";
|
import * as h from "hjson";
|
||||||
|
import { homedir } from "os";
|
||||||
|
import path from "path";
|
||||||
|
import { isDev } from "../lib";
|
||||||
|
|
||||||
export const registerSyncCommand = (p: Command) => {
|
export const registerSyncCommand = (p: Command) => {
|
||||||
p
|
p
|
||||||
|
@ -10,7 +13,7 @@ export const registerSyncCommand = (p: Command) => {
|
||||||
.action(() => {
|
.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("mix.hjson") && !dirent.name.startsWith("_"))
|
||||||
.map((dirent) => ({
|
.map((dirent) => ({
|
||||||
name: dirent.name,
|
name: dirent.name,
|
||||||
content: h.parse(readFileSync(dirent.name, "utf-8"))
|
content: h.parse(readFileSync(dirent.name, "utf-8"))
|
||||||
|
@ -106,7 +109,7 @@ export const registerSyncCommand = (p: Command) => {
|
||||||
|
|
||||||
for (const pkg of installing) {
|
for (const pkg of installing) {
|
||||||
console.log(`Installing ${pkg.id} version ${pkg.version}`);
|
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) {
|
if (error) {
|
||||||
console.error(`Error installing ${pkg.id}: ${error.message}`);
|
console.error(`Error installing ${pkg.id}: ${error.message}`);
|
||||||
return;
|
return;
|
||||||
|
@ -126,7 +129,8 @@ export const registerSyncCommand = (p: Command) => {
|
||||||
|
|
||||||
lock.push(l);
|
lock.push(l);
|
||||||
const content = h.stringify(lock);
|
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) {
|
if (index !== -1) {
|
||||||
lock.splice(index, 1);
|
lock.splice(index, 1);
|
||||||
const content = h.stringify(lock);
|
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);
|
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");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
3
index.ts
3
index.ts
|
@ -6,8 +6,7 @@ const program = new Command();
|
||||||
program
|
program
|
||||||
.name('mix')
|
.name('mix')
|
||||||
.description('a declerative file-based package manager for windows.')
|
.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);
|
registerCommands(program);
|
||||||
|
|
||||||
program.parse();
|
program.parse();
|
17
lib.ts
Normal file
17
lib.ts
Normal file
|
@ -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";
|
|
@ -1,5 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "wem",
|
"name": "@grng/mix",
|
||||||
|
"version": "0.0.4",
|
||||||
|
"publishConfig": {
|
||||||
|
"access": "public"
|
||||||
|
},
|
||||||
|
"bin": {
|
||||||
|
"mix": "index.ts"
|
||||||
|
},
|
||||||
"module": "index.ts",
|
"module": "index.ts",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue