-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapp.ts
More file actions
executable file
·154 lines (143 loc) · 4.13 KB
/
app.ts
File metadata and controls
executable file
·154 lines (143 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env -S pkgx --quiet deno^2 run -A
//TODO if you step into dev-dir/subdir and type `dev` does it find the root properly?
//TODO dev off uses PWD which may not be correct if in subdir (obv)
import { Path, utils } from "libpkgx";
import shellcode, { datadir } from "./src/shellcode().ts";
import app_version from "./src/app-version.ts";
import integrate from "./src/integrate.ts";
import { parseArgs } from "jsr:@std/cli@^1/parse-args";
import dump from "./src/dump.ts";
import sniff from "./src/sniff.ts";
import { walk } from "jsr:@std/fs@1/walk";
const parsedArgs = parseArgs(Deno.args, {
alias: {
n: "dry-run",
"just-print": "dry-run",
recon: "dry-run",
v: "version",
h: "help",
q: "quiet",
},
collect: ["quiet"],
boolean: ["help", "version", "shellcode", "quiet"],
default: {
"dry-run": false,
},
});
if (parsedArgs.help) {
const { code } = await new Deno.Command("pkgx", {
args: [
"glow",
"https://raw.githubusercontent.com/pkgxdev/dev/refs/heads/main/README.md",
],
}).spawn().status;
Deno.exit(code);
} else if (parsedArgs.shellcode) {
console.log(shellcode());
} else if (parsedArgs.version) {
console.log(`dev ${app_version}`);
} else {
const subcommand = parsedArgs._[0];
const dryrun = parsedArgs["dry-run"] as boolean;
const quiet = Array.isArray(parsedArgs["quiet"])
? !!parsedArgs["quiet"].length
: parsedArgs["quiet"];
switch (subcommand) {
case "integrate":
await integrate("install", { dryrun });
break;
case "deintegrate":
await integrate("uninstall", { dryrun });
break;
case "status":
{
const cwd = Path.cwd();
if (
datadir().join(cwd.string.slice(1), "dev.pkgx.activated").isFile()
) {
//FIXME probably slower than ideal
const { pkgs } = await sniff(cwd);
Deno.exit(pkgs.length == 0 ? 1 : 0);
} else {
Deno.exit(1);
}
}
break;
case "ls":
for await (
const entry of walk(datadir().string, { includeDirs: false })
) {
if (entry.name === "dev.pkgx.activated") {
const partial_path = new Path(entry.path).parent().relative({
to: datadir(),
});
console.log(`/${partial_path}`);
}
}
break;
case undefined:
if (Deno.stdout.isTerminal()) {
const cwd = Path.cwd();
const { pkgs } = await sniff(cwd);
if (
datadir().join(cwd.string.slice(1), "dev.pkgx.activated").isFile()
) {
console.log(
"%cactive",
"color: green",
pkgs.map(utils.pkg.str).join(" "),
);
} else if (pkgs.length > 0) {
console.log(
"%cinactive",
"color: red",
pkgs.map(utils.pkg.str).join(" "),
);
} else {
console.log("%cno keyfiles found", "color: red");
}
} else {
const cwd = Path.cwd();
await dump(cwd, { dryrun, quiet });
}
break;
case "off": {
let dir = Path.cwd();
while (dir.string != "/") {
const f = datadir().join(dir.string.slice(1), "dev.pkgx.activated")
.isFile();
if (f) {
f.rm();
console.log("%cdeactivated", "color: green", dir.string);
Deno.exit(0);
}
dir = dir.parent();
}
console.error("%cno devenv found", "color: red");
Deno.exit(1);
break;
}
default: {
if (Deno.stdout.isTerminal()) {
const cwd = Path.cwd().join(subcommand as string);
const { pkgs } = await sniff(cwd);
if (pkgs.length > 0) {
datadir().join(cwd.string.slice(1)).mkdir("p").join(
"dev.pkgx.activated",
).touch();
console.log(
"%cactivated",
"color: green",
pkgs.map(utils.pkg.str).join(" "),
);
} else {
console.error("%cno keyfiles found", "color: red");
Deno.exit(1);
}
} else {
const cwd = Path.cwd().join(subcommand as string);
await dump(cwd, { dryrun, quiet });
}
}
}
}