-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support environment variables that persist beyond single commands
- Loading branch information
1 parent
e6b9b80
commit 6cfa446
Showing
8 changed files
with
113 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Collection of environment variables that are known to a shell and are passed in and out of | ||
* commands. | ||
*/ | ||
export class Environment { | ||
constructor() { | ||
this._env.set("PS1", "\x1b[1;31mjs-shell:$\x1b[1;0m ") // red color | ||
} | ||
|
||
/** | ||
* Copy environment variables back from a command after it has run. | ||
*/ | ||
copyFromCommand(source: string[]) { | ||
for (const str of source) { | ||
const split = str.split("=") | ||
const key = split.shift() | ||
if (key && !this._ignore.has(key)) { | ||
this._env.set(key, split.join("=")) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Copy environment variables into a command before it is run. | ||
*/ | ||
copyIntoCommand(target: { [key: string]: string }) { | ||
for (const [key, value] of this._env.entries()) { | ||
target[key] = value | ||
} | ||
} | ||
|
||
get(key: string): string | null { | ||
return this._env.get(key) ?? null | ||
} | ||
|
||
getPrompt(): string { | ||
return this._env.get("PS1") ?? "$ " | ||
} | ||
|
||
set(key: string, value: string) { | ||
this._env.set(key, value) | ||
} | ||
|
||
private _env: Map<string, string> = new Map() | ||
|
||
// Keys to ignore when copying back from a command's env vars. | ||
private _ignore: Set<string> = new Set(["USER", "LOGNAME", "HOME", "LANG", "_"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { shell_setup_simple } from "../shell_setup" | ||
|
||
describe("cd command", () => { | ||
it("should update PWD", async () => { | ||
const { shell } = await shell_setup_simple() | ||
const { environment } = shell | ||
expect(environment.get("PWD")).toEqual("/drive") | ||
await shell._runCommands("cd dirA") | ||
expect(environment.get("PWD")).toEqual("/drive/dirA") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { shell_setup_simple } from "../shell_setup" | ||
|
||
describe("env command", () => { | ||
it("should write to stdout", async () => { | ||
const { shell, output } = await shell_setup_simple() | ||
const { environment } = shell | ||
expect(environment.get("MYENV")).toBeNull() | ||
|
||
await shell._runCommands("env MYENV=23") | ||
expect(environment.get("MYENV")).toBeNull() | ||
expect(output.text.trim().split("\r\n").at(-1)).toEqual("MYENV=23") | ||
}) | ||
}) |