Skip to content

Commit a310403

Browse files
committed
dev status for pkgm
1 parent 8511634 commit a310403

File tree

6 files changed

+104
-20
lines changed

6 files changed

+104
-20
lines changed

README.md

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,67 @@ dev, across your team and in production.
1212
1313
## Getting Started
1414

15+
Since `dev` v1.7.0 we integrate with `pkgm` and this is the recommended way to
16+
use `dev`.
17+
18+
> [!IMPORTANT]
19+
>
20+
> `dev` must be installed to `/usr/local/bin/dev` for this route to work:
21+
>
22+
> ```sh
23+
> sudo pkgm install dev # use of `shim` is also fine
24+
> ```
25+
26+
```sh
27+
$ cd my-project
28+
$ ls
29+
package.json
30+
31+
$ node --version
32+
command not found: node
33+
34+
$ sudo pkgm install dev node
35+
$ node --version && which node
36+
v23.11.0
37+
/usr/local/bin/node
38+
39+
$ cat package.json | jq .engines
40+
{
41+
"node": "^20"
42+
}
43+
44+
$ dev
45+
activated `~/my-project` (+node^20)
46+
47+
$ node --version && which node
48+
v20.19.0
49+
/usr/local/bin/node
50+
51+
$ cd ..
52+
$ node --version && which node
53+
v23.11.0
54+
/usr/local/bin/node
55+
56+
$ cd -
57+
$ node --version && which node
58+
v20.19.0
59+
/usr/local/bin/node
60+
```
61+
62+
`pkgm` installs `dev`-aware packages to `/usr/local/bin`. Provided you have
63+
`/usr/local/bin/dev` installed and you have activated `dev` in your project
64+
directories the `node` that is invoked is swapped out *when invoked*.
65+
66+
This is the recommended way to use `dev` because it works everywhere and not
67+
just the terminal.
68+
69+
## `dev` via Shellcode
70+
71+
Shellcode works *as well* and is your preference. It has notable caveats
72+
with regard to use in tools like editors. It also requires you to add
73+
shellcode to your `shell.rc` files and thus is more intrusive (depending on
74+
your outlook).
75+
1576
```sh
1677
pkgx dev integrate
1778
```
@@ -78,6 +139,7 @@ command not found: node
78139
> ```sh
79140
> $ cd my-project
80141
> $ eval "$(pkgx dev)"
142+
> +deno^2
81143
> ```
82144
>
83145
> The devenv will only exist for the duration of your shell session.
@@ -166,8 +228,8 @@ environment. We recommend Visual Studio Code, `dev && code .` works great.
166228
- uses: pkgxdev/dev@v1
167229
```
168230
169-
Installs needed packages and sets up the environment the same as `dev` does in
170-
your terminal.
231+
Installs needed packages (via `pkgx`) and sets up the environment the same as
232+
`dev` does in your terminal.
171233
172234
## Contributing
173235

app.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
#!/usr/bin/env -S pkgx deno^2 run -A
1+
#!/usr/bin/env -S pkgx --quiet deno^2 run -A
22

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

66
import { Path } from "libpkgx";
7-
import shellcode from "./src/shellcode().ts";
7+
import shellcode, { datadir } from "./src/shellcode().ts";
88
import app_version from "./src/app-version.ts";
99
import integrate from "./src/integrate.ts";
10-
import { parse } from "jsr:@std/flags";
10+
import { parseArgs } from "jsr:@std/cli@^1/parse-args";
1111
import dump from "./src/dump.ts";
12+
import sniff from "./src/sniff.ts";
1213

13-
const parsedArgs = parse(Deno.args, {
14+
const parsedArgs = parseArgs(Deno.args, {
1415
alias: {
1516
n: "dry-run",
1617
"just-print": "dry-run",
1718
recon: "dry-run",
1819
v: "version",
1920
h: "help",
21+
q: "quiet",
2022
},
23+
collect: ["quiet"],
2124
boolean: ["help", "version", "shellcode"],
2225
default: {
2326
"dry-run": false,
@@ -26,7 +29,7 @@ const parsedArgs = parse(Deno.args, {
2629

2730
if (parsedArgs.help) {
2831
const status = await new Deno.Command("pkgx", {
29-
args: ["gh", "repo", "view", "pkgxdev/dev"],
32+
args: ["--quiet", "gh", "repo", "view", "pkgxdev/dev"],
3033
}).spawn().status;
3134
Deno.exit(status.code);
3235
} else if (parsedArgs.shellcode) {
@@ -36,16 +39,27 @@ if (parsedArgs.help) {
3639
} else {
3740
const subcommand = parsedArgs._[0];
3841
const dryrun = parsedArgs["dry-run"] as boolean;
42+
const quiet = parsedArgs["quiet"] != undefined;
3943
switch (subcommand) {
4044
case "integrate":
4145
await integrate("install", { dryrun });
4246
break;
4347
case "deintegrate":
4448
await integrate("uninstall", { dryrun });
4549
break;
50+
case "status": {
51+
const cwd = Path.cwd();
52+
if (datadir().join(cwd.string.slice(1), "dev.pkgx.activated").isFile()) {
53+
//FIXME probably slower than necessary
54+
const { pkgs } = await sniff(cwd);
55+
Deno.exit(pkgs.length == 0 ? 1 : 0);
56+
} else {
57+
Deno.exit(1);
58+
}
59+
} break;
4660
default: {
4761
const cwd = Path.cwd().join(subcommand as string);
48-
await dump(cwd, { dryrun });
62+
await dump(cwd, { dryrun, quiet });
4963
}
5064
}
5165
}

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
"imports": {
1414
"libpkgx": "https://raw.githubusercontent.com/pkgxdev/libpkgx/refs/tags/v0.21.0/mod.ts",
15-
"libpkgx/": "https://raw.githubusercontent.com/pkgxdev/libpkgx/refs/tags/v0.20.1/src/",
15+
"libpkgx/": "https://raw.githubusercontent.com/pkgxdev/libpkgx/refs/tags/v0.21.0/src/",
1616
"is-what": "https://deno.land/x/is_what@v4.1.15/src/index.ts",
1717
"outdent": "https://deno.land/x/outdent@v0.8.0/mod.ts"
1818
}

deno.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dump.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Path, utils } from "libpkgx";
22
import sniff from "./sniff.ts";
33
import shell_escape from "./shell-escape.ts";
44

5-
export default async function (cwd: Path, opts: { dryrun: boolean }) {
5+
export default async function (cwd: Path, opts: { dryrun: boolean, quiet: boolean }) {
66
const snuff = await sniff(cwd);
77

88
if (snuff.pkgs.length === 0 && Object.keys(snuff.env).length === 0) {
@@ -20,7 +20,7 @@ export default async function (cwd: Path, opts: { dryrun: boolean }) {
2020

2121
if (snuff.pkgs.length > 0) {
2222
const cmd = new Deno.Command("pkgx", {
23-
args: [...pkgspecs],
23+
args: ["--quiet", ...pkgspecs],
2424
stdout: "piped",
2525
env: { CLICOLOR_FORCE: "1" }, // unfortunate
2626
}).spawn();
@@ -54,7 +54,9 @@ export default async function (cwd: Path, opts: { dryrun: boolean }) {
5454
" ",
5555
);
5656

57-
console.error("%c%s", "color: green", pkgspecs.join(" "));
57+
if (!opts.quiet) {
58+
console.error("%c%s", "color: green", pkgspecs.join(" "));
59+
}
5860

5961
console.log(`
6062
eval "_pkgx_dev_try_bye() {

src/shellcode().ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { Path } from "libpkgx";
22

33
export default function shellcode() {
4-
const datadir = new Path(
5-
Deno.env.get("XDG_DATA_HOME")?.trim() || platform_data_home_default(),
6-
).join("pkgx", "dev");
7-
84
// find self
95
const dev_cmd = Deno.env.get("PATH")?.split(":").map((path) =>
106
Path.abs(path)?.join("dev")
@@ -18,7 +14,7 @@ _pkgx_chpwd_hook() {
1814
if ! type _pkgx_dev_try_bye >/dev/null 2>&1 || _pkgx_dev_try_bye; then
1915
dir="$PWD"
2016
while [ "$dir" != "/" ]; do
21-
if [ -f "${datadir}/$dir/dev.pkgx.activated" ]; then
17+
if [ -f "${datadir()}/$dir/dev.pkgx.activated" ]; then
2218
eval "$(${dev_cmd})"
2319
break
2420
fi
@@ -31,7 +27,7 @@ dev() {
3127
case "$1" in
3228
off)
3329
if type -f _pkgx_dev_try_bye >/dev/null 2>&1; then
34-
rm "${datadir}$PWD/dev.pkgx.activated"
30+
rm "${datadir()}$PWD/dev.pkgx.activated"
3531
PWD=/ _pkgx_dev_try_bye
3632
else
3733
echo "no devenv" >&2
@@ -40,8 +36,8 @@ dev() {
4036
if [ "$2" ]; then
4137
"${dev_cmd}" "$@"
4238
elif ! type -f _pkgx_dev_try_bye >/dev/null 2>&1; then
43-
mkdir -p "${datadir}$PWD"
44-
touch "${datadir}$PWD/dev.pkgx.activated"
39+
mkdir -p "${datadir()}$PWD"
40+
touch "${datadir()}$PWD/dev.pkgx.activated"
4541
eval "$(${dev_cmd})"
4642
else
4743
echo "devenv already active" >&2
@@ -74,6 +70,12 @@ fi
7470
`.trim();
7571
}
7672

73+
export function datadir() {
74+
return new Path(
75+
Deno.env.get("XDG_DATA_HOME")?.trim() || platform_data_home_default(),
76+
).join("pkgx", "dev");
77+
}
78+
7779
function platform_data_home_default() {
7880
const home = Path.home();
7981
switch (Deno.build.os) {

0 commit comments

Comments
 (0)