Skip to content

Commit

Permalink
Get undo env from the env with recursive eval
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Jan 8, 2025
1 parent be3650f commit 7de8c67
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ echo 'eval "$(pkgx dev --shellcode)"' >> ~/.zshrc
```

> [!NOTE]
>
> `pkgx` is a required dependency.
>
> ```sh
Expand Down
23 changes: 11 additions & 12 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ if (snuff.pkgs.length === 0 && Object.keys(snuff.env).length === 0) {
Deno.exit(1);
}

let env = '';
let env = "";
const pkgspecs = snuff.pkgs.map((pkg) => `+${utils.pkg.str(pkg)}`);

if (snuff.pkgs.length > 0) {
Expand Down Expand Up @@ -60,7 +60,7 @@ for (const envln of env.trim().split("\n")) {
const value = Deno.env.get(key);

if (value) {
undo += ` export ${key}=${shell_escape(value)}\n`;
undo += ` export ${key}=\\"$${key}\\"\n`;
} else {
undo += ` unset ${key}\n`;
}
Expand All @@ -71,21 +71,20 @@ const dir = Deno.cwd();
const bye_bye_msg = pkgspecs.map((pkgspec) => `-${pkgspec.slice(1)}`).join(" ");

console.log(`
set -a
${env}
set +a
_pkgx_dev_try_bye() {
suffix="\${PWD#"${dir}"}"
if test "$PWD" != "${dir}$suffix"; then
eval "_pkgx_dev_try_bye() {
suffix=\\"\\\${PWD#\\"${dir}\\"}\\"
if test \\"\\$PWD\\" != \\"${dir}$suffix\\"; then
${undo.trim()}
unset -f _pkgx_dev_try_bye
echo "\\033[31m${bye_bye_msg}\\033[0m" >&2
echo -e \\"\\033[31m${bye_bye_msg}\\033[0m\\" >&2
return 0
else
return 1
fi
}
`.trim());
}"
set -a
${env}
set +a`);

console.error("%c%s", "color: green", pkgspecs.join(" "));

0 comments on commit 7de8c67

Please sign in to comment.