Skip to content

Commit 5efff52

Browse files
committed
Allow dev [PATH] to activate devenvs
1 parent 30a6adc commit 5efff52

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ $ cat package.json | jq .engines
4141
"node": "^20"
4242
}
4343
44-
$ dev
44+
$ dev .
4545
activated `~/my-project` (+node^20)
4646
4747
$ node --version && which node

app.ts

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
//TODO if you step into dev-dir/subdir and type `dev` does it find the root properly?
44
//TODO dev off uses PWD which may not be correct if in subdir (obv)
55

6-
import { Path } from "libpkgx";
6+
import { Path, utils } from "libpkgx";
77
import shellcode, { datadir } from "./src/shellcode().ts";
88
import app_version from "./src/app-version.ts";
99
import integrate from "./src/integrate.ts";
1010
import { parseArgs } from "jsr:@std/cli@^1/parse-args";
1111
import dump from "./src/dump.ts";
1212
import sniff from "./src/sniff.ts";
13+
import { walk } from "jsr:@std/fs@1/walk";
1314

1415
const parsedArgs = parseArgs(Deno.args, {
1516
alias: {
@@ -28,10 +29,13 @@ const parsedArgs = parseArgs(Deno.args, {
2829
});
2930

3031
if (parsedArgs.help) {
31-
const status = await new Deno.Command("pkgx", {
32-
args: ["--quiet", "gh", "repo", "view", "pkgxdev/dev"],
32+
const { code } = await new Deno.Command("pkgx", {
33+
args: [
34+
"glow",
35+
"https://raw.githubusercontent.com/pkgxdev/dev/refs/heads/main/README.md",
36+
],
3337
}).spawn().status;
34-
Deno.exit(status.code);
38+
Deno.exit(code);
3539
} else if (parsedArgs.shellcode) {
3640
console.log(shellcode());
3741
} else if (parsedArgs.version) {
@@ -40,30 +44,84 @@ if (parsedArgs.help) {
4044
const subcommand = parsedArgs._[0];
4145
const dryrun = parsedArgs["dry-run"] as boolean;
4246
const quiet = parsedArgs["quiet"] != undefined;
47+
4348
switch (subcommand) {
4449
case "integrate":
4550
await integrate("install", { dryrun });
4651
break;
52+
4753
case "deintegrate":
4854
await integrate("uninstall", { dryrun });
4955
break;
56+
5057
case "status":
5158
{
5259
const cwd = Path.cwd();
5360
if (
5461
datadir().join(cwd.string.slice(1), "dev.pkgx.activated").isFile()
5562
) {
56-
//FIXME probably slower than necessary
63+
//FIXME probably slower than ideal
5764
const { pkgs } = await sniff(cwd);
5865
Deno.exit(pkgs.length == 0 ? 1 : 0);
5966
} else {
6067
Deno.exit(1);
6168
}
6269
}
6370
break;
71+
72+
case "ls":
73+
for await (const entry of walk(datadir().string, { includeDirs: false })) {
74+
if (entry.name === "dev.pkgx.activated") {
75+
const partial_path = new Path(entry.path).parent().relative({ to: datadir() });
76+
console.log(`/${partial_path}`);
77+
}
78+
}
79+
break;
80+
81+
case undefined: {
82+
const cwd = Path.cwd();
83+
const { pkgs } = await sniff(cwd);
84+
if (datadir().join(cwd.string.slice(1), "dev.pkgx.activated").isFile()) {
85+
console.log("%cactive", 'color: green', pkgs.map(utils.pkg.str).join(" "));
86+
} else if (pkgs.length > 0) {
87+
console.log("%cinactive", 'color: red', pkgs.map(utils.pkg.str).join(" "));
88+
} else {
89+
console.log("%cno keyfiles found", 'color: red');
90+
}
91+
}
92+
break;
93+
94+
case "off": {
95+
let dir = Path.cwd();
96+
while (dir.string != "/") {
97+
const f = datadir().join(dir.string.slice(1), "dev.pkgx.activated").isFile();
98+
if (f) {
99+
f.rm();
100+
console.log("%cdeactivated", 'color: green', dir.string);
101+
Deno.exit(0);
102+
}
103+
dir = dir.parent();
104+
}
105+
console.error("%cno devenv found", 'color: red');
106+
Deno.exit(1);
107+
break;
108+
}
109+
64110
default: {
65-
const cwd = Path.cwd().join(subcommand as string);
66-
await dump(cwd, { dryrun, quiet });
111+
if (Deno.stdout.isTerminal()) {
112+
const cwd = Path.cwd().join(subcommand as string);
113+
const { pkgs } = await sniff(cwd);
114+
if (pkgs.length > 0) {
115+
datadir().join(cwd.string.slice(1)).mkdir('p').join("dev.pkgx.activated").touch();
116+
console.log("%cactived", 'color: green', pkgs.map(utils.pkg.str).join(" "));
117+
} else {
118+
console.error("%cno keyfiles found", 'color: red');
119+
Deno.exit(1);
120+
}
121+
} else {
122+
const cwd = Path.cwd().join(subcommand as string);
123+
await dump(cwd, { dryrun, quiet });
124+
}
67125
}
68126
}
69127
}

0 commit comments

Comments
 (0)