@adamhl8/bask 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -5
- package/dist/shell.d.ts +29 -7
- package/dist/shell.d.ts.map +1 -1
- package/dist/shell.js +33 -5
- package/dist/shell.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -91,13 +91,11 @@ Use `alias` rather than exporting the same task under a second name. bask can't
|
|
|
91
91
|
|
|
92
92
|
### Running commands
|
|
93
93
|
|
|
94
|
-
`sh` prints each command before running it with Bun's `$`. To run a command without printing it, use
|
|
94
|
+
`sh` prints each command before running it with Bun's `$`. To run a command without printing it, use `.echo(false)`.
|
|
95
95
|
|
|
96
96
|
```ts
|
|
97
|
-
import { $ } from "bun"
|
|
98
|
-
|
|
99
97
|
export const release = task(async ({ sh }) => {
|
|
100
|
-
const branch = await
|
|
98
|
+
const branch = await sh`git branch --show-current`.echo(false).text()
|
|
101
99
|
if (branch.trim() === "main") await sh`gh workflow run release.yml`
|
|
102
100
|
})
|
|
103
101
|
```
|
|
@@ -114,7 +112,7 @@ error: task 'lint' failed with exit code 2
|
|
|
114
112
|
|
|
115
113
|
Any other error thrown in a task still shows its stack trace.
|
|
116
114
|
|
|
117
|
-
`sh` takes the same settings as Bun's global `$`: `sh.cwd()`, `sh.env()`, `sh.nothrow()`, and `sh.throws()`. They apply to the commands the task runs after the call, and a command's own
|
|
115
|
+
`sh` takes the same settings as Bun's global `$`: `sh.cwd()`, `sh.env()`, `sh.nothrow()`, and `sh.throws()`, plus `sh.echo()`. They apply to the commands the task runs after the call, and a command's own setting overrides them for that command.
|
|
118
116
|
|
|
119
117
|
```ts
|
|
120
118
|
export const buildWeb = task(async ({ sh }) => {
|
package/dist/shell.d.ts
CHANGED
|
@@ -1,30 +1,52 @@
|
|
|
1
1
|
import { $, ShellExpression } from "bun";
|
|
2
2
|
//#region src/shell.d.ts
|
|
3
3
|
type ShellEnv = Parameters<$.ShellPromise["env"]>[0];
|
|
4
|
-
/** Changes each method of `T` that returns `From` to return `To`, keeping Bun's docs on it. */
|
|
5
|
-
type Rechain<T, From, To> = { [K in keyof T]: T[K] extends ((...args: infer Args) => From) ? (...args: Args) => To : T[K]; };
|
|
6
|
-
type BunCommandMethods = Omit<$.ShellPromise, keyof Promise<$.ShellOutput> | "stdin">;
|
|
7
|
-
type ShellSettingKey = Exclude<keyof $, "braces" | "escape" | "Shell" | "ShellError" | "ShellPromise">;
|
|
8
4
|
/** Runs commands like Bun's `$`, with settings that apply to every later command from the same shell. */
|
|
9
|
-
export interface Shell
|
|
5
|
+
export interface Shell {
|
|
10
6
|
(strings: TemplateStringsArray, ...expressions: ShellExpression[]): ShellCommand;
|
|
7
|
+
/** Change the default working directory for commands run by this shell. */
|
|
8
|
+
cwd: (newCwd?: string) => Shell;
|
|
9
|
+
/** Change the default environment variables for commands run by this shell. */
|
|
10
|
+
env: (newEnv?: ShellEnv) => Shell;
|
|
11
|
+
/** Configure the shell to not throw an exception on non-zero exit codes. */
|
|
12
|
+
nothrow: () => Shell;
|
|
13
|
+
/** Configure whether the shell should throw an exception on non-zero exit codes. */
|
|
14
|
+
throws: (shouldThrow: boolean) => Shell;
|
|
15
|
+
/** Configure whether the shell prints each command before running it. */
|
|
16
|
+
echo: (shouldEcho: boolean) => Shell;
|
|
11
17
|
}
|
|
12
18
|
/**
|
|
13
19
|
* Wraps Bun's `ShellPromise` to set `FORCE_COLOR` only on commands whose output is shown. Bun's pipes hide the terminal
|
|
14
20
|
* from commands, so they turn color off unless forced.
|
|
15
21
|
*/
|
|
16
|
-
declare class ShellCommand implements PromiseLike<$.ShellOutput
|
|
22
|
+
declare class ShellCommand implements PromiseLike<$.ShellOutput> {
|
|
17
23
|
#private;
|
|
18
|
-
constructor(promise: $.ShellPromise, env:
|
|
24
|
+
constructor(promise: $.ShellPromise, { command, env, shouldEcho }: {
|
|
25
|
+
command: string;
|
|
26
|
+
env: ShellEnv;
|
|
27
|
+
shouldEcho: boolean;
|
|
28
|
+
});
|
|
29
|
+
/** Change the current working directory of the shell. */
|
|
19
30
|
cwd(newCwd: string): this;
|
|
31
|
+
/** Set environment variables for the shell. */
|
|
20
32
|
env(newEnv: ShellEnv): this;
|
|
33
|
+
/** Configure the shell to only buffer the output instead of also writing it to stdout and stderr. */
|
|
21
34
|
quiet(isQuiet?: boolean): this;
|
|
35
|
+
/** Configure the shell to not throw an exception on non-zero exit codes. */
|
|
22
36
|
nothrow(): this;
|
|
37
|
+
/** Configure whether the shell should throw an exception on non-zero exit codes. */
|
|
23
38
|
throws(shouldThrow: boolean): this;
|
|
39
|
+
/** Configure whether the shell prints the command before running it. */
|
|
40
|
+
echo(shouldEcho: boolean): this;
|
|
41
|
+
/** Read from stdout as a string. Automatically calls {@link quiet}. */
|
|
24
42
|
text(encoding?: BufferEncoding): Promise<string>;
|
|
43
|
+
/** Read from stdout as a JSON object. Automatically calls {@link quiet}. */
|
|
25
44
|
json(): Promise<any>;
|
|
45
|
+
/** Read from stdout as a string, line by line. Automatically calls {@link quiet}. */
|
|
26
46
|
lines(): AsyncIterable<string>;
|
|
47
|
+
/** Read from stdout as an ArrayBuffer. Automatically calls {@link quiet}. */
|
|
27
48
|
arrayBuffer(): Promise<ArrayBuffer>;
|
|
49
|
+
/** Read from stdout as a Blob. Automatically calls {@link quiet}. */
|
|
28
50
|
blob(): Promise<Blob>;
|
|
29
51
|
then<TResult1 = $.ShellOutput, TResult2 = never>(onfulfilled?: ((value: $.ShellOutput) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
30
52
|
catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null): Promise<TResult | $.ShellOutput>;
|
package/dist/shell.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shell.d.ts","names":["BunShell"],"sources":["../src/shell.ts"],"mappings":";;KAKK,WAAW,WAAWA,EAAS;;
|
|
1
|
+
{"version":3,"file":"shell.d.ts","names":["BunShell"],"sources":["../src/shell.ts"],"mappings":";;KAKK,WAAW,WAAWA,EAAS;;iBAGnB;GACd,SAAS,yBAAyB,aAAa,oBAAoB;;EAEpE,MAAM,oBAAoB;;EAE1B,MAAM,SAAS,aAAa;;EAE5B,eAAe;;EAEf,SAAS,yBAAyB;;EAElC,OAAO,wBAAwB;;;;;;cAuB3B,wBAAwB,YAAYA,EAAS;;EAQjD,YACE,SAASA,EAAS,gBAChB,SAAS,KAAK;IAAgB;IAAiB,KAAK;IAAU;;;EAS3D,IAAI;;EAMJ,IAAI,QAAQ;;EAMZ,MAAM;;EAON;;EAMA,OAAO;;EAMP,KAAK;;EAMC,KAAK,WAAW,iBAAc;;EAK9B,QAAI;;EAMV,SAAK;;EAKC,eAAW,QAAA;;EAKX,QAAI,QAAA;EAKJ,KAAK,WAAWA,EAAS,aAAa,kBACjD,gBAAgB,OAAOA,EAAS,gBAAgB,WAAW,YAAY,mBACvE,eAAe,oBAAoB,WAAW,YAAY,oBAAiB,QAAA,WAAA;EAKhE,MAAM,iBAAiB,eAAe,oBAAoB,UAAU,YAAY,mBAAgB,QAAA,UAAAA,EAAA;EAIhG,QAAQ,kCAA+B,QAAAA,EAAA"}
|
package/dist/shell.js
CHANGED
|
@@ -15,46 +15,65 @@ const shouldForceColor = (env) => process.stdout.isTTY && process.stderr.isTTY &
|
|
|
15
15
|
*/
|
|
16
16
|
var ShellCommand = class {
|
|
17
17
|
#promise;
|
|
18
|
+
#command;
|
|
18
19
|
#env;
|
|
20
|
+
#shouldEcho;
|
|
19
21
|
#isQuiet = false;
|
|
20
22
|
#isPrepared = false;
|
|
21
|
-
constructor(promise, env) {
|
|
23
|
+
constructor(promise, { command, env, shouldEcho }) {
|
|
22
24
|
this.#promise = promise;
|
|
25
|
+
this.#command = command;
|
|
23
26
|
this.#env = env;
|
|
27
|
+
this.#shouldEcho = shouldEcho;
|
|
24
28
|
}
|
|
29
|
+
/** Change the current working directory of the shell. */
|
|
25
30
|
cwd(newCwd) {
|
|
26
31
|
this.#promise.cwd(newCwd);
|
|
27
32
|
return this;
|
|
28
33
|
}
|
|
34
|
+
/** Set environment variables for the shell. */
|
|
29
35
|
env(newEnv) {
|
|
30
36
|
this.#env = newEnv;
|
|
31
37
|
return this;
|
|
32
38
|
}
|
|
39
|
+
/** Configure the shell to only buffer the output instead of also writing it to stdout and stderr. */
|
|
33
40
|
quiet(isQuiet = true) {
|
|
34
41
|
this.#promise.quiet(isQuiet);
|
|
35
42
|
this.#isQuiet = isQuiet;
|
|
36
43
|
return this;
|
|
37
44
|
}
|
|
45
|
+
/** Configure the shell to not throw an exception on non-zero exit codes. */
|
|
38
46
|
nothrow() {
|
|
39
47
|
this.#promise.nothrow();
|
|
40
48
|
return this;
|
|
41
49
|
}
|
|
50
|
+
/** Configure whether the shell should throw an exception on non-zero exit codes. */
|
|
42
51
|
throws(shouldThrow) {
|
|
43
52
|
this.#promise.throws(shouldThrow);
|
|
44
53
|
return this;
|
|
45
54
|
}
|
|
55
|
+
/** Configure whether the shell prints the command before running it. */
|
|
56
|
+
echo(shouldEcho) {
|
|
57
|
+
this.#shouldEcho = shouldEcho;
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
/** Read from stdout as a string. Automatically calls {@link quiet}. */
|
|
46
61
|
async text(encoding) {
|
|
47
62
|
return this.#prepare(false).text(encoding);
|
|
48
63
|
}
|
|
64
|
+
/** Read from stdout as a JSON object. Automatically calls {@link quiet}. */
|
|
49
65
|
async json() {
|
|
50
66
|
return this.#prepare(false).json();
|
|
51
67
|
}
|
|
68
|
+
/** Read from stdout as a string, line by line. Automatically calls {@link quiet}. */
|
|
52
69
|
lines() {
|
|
53
70
|
return this.#prepare(false).lines();
|
|
54
71
|
}
|
|
72
|
+
/** Read from stdout as an ArrayBuffer. Automatically calls {@link quiet}. */
|
|
55
73
|
async arrayBuffer() {
|
|
56
74
|
return this.#prepare(false).arrayBuffer();
|
|
57
75
|
}
|
|
76
|
+
/** Read from stdout as a Blob. Automatically calls {@link quiet}. */
|
|
58
77
|
async blob() {
|
|
59
78
|
return this.#prepare(false).blob();
|
|
60
79
|
}
|
|
@@ -67,10 +86,11 @@ var ShellCommand = class {
|
|
|
67
86
|
async finally(onfinally) {
|
|
68
87
|
return this.#prepare(!this.#isQuiet).finally(onfinally);
|
|
69
88
|
}
|
|
70
|
-
/**
|
|
89
|
+
/** Prints the command and sets its environment once, before Bun starts the command. */
|
|
71
90
|
#prepare(isShown) {
|
|
72
91
|
if (this.#isPrepared) return this.#promise;
|
|
73
92
|
this.#isPrepared = true;
|
|
93
|
+
if (this.#shouldEcho) process.stderr.write(`${styleText("bold", this.#command, { stream: process.stderr })}\n`);
|
|
74
94
|
const env = this.#env ?? process.env;
|
|
75
95
|
this.#promise.env(isShown && shouldForceColor(env) ? {
|
|
76
96
|
...env,
|
|
@@ -83,10 +103,14 @@ var ShellCommand = class {
|
|
|
83
103
|
const createShell = () => {
|
|
84
104
|
const bunShell = new bun.$.Shell();
|
|
85
105
|
let shellEnv;
|
|
106
|
+
let shouldEcho = true;
|
|
86
107
|
const run = (strings, ...expressions) => {
|
|
87
|
-
const command = String.raw({ raw: strings.raw }, ...expressions.map(formatExpression));
|
|
88
|
-
|
|
89
|
-
|
|
108
|
+
const command = String.raw({ raw: strings.raw }, ...expressions.map(formatExpression)).trim();
|
|
109
|
+
return new ShellCommand(bunShell(strings, ...expressions), {
|
|
110
|
+
command,
|
|
111
|
+
env: shellEnv,
|
|
112
|
+
shouldEcho
|
|
113
|
+
});
|
|
90
114
|
};
|
|
91
115
|
const shell = Object.assign(run, {
|
|
92
116
|
cwd: (newCwd) => {
|
|
@@ -104,6 +128,10 @@ const createShell = () => {
|
|
|
104
128
|
throws: (shouldThrow) => {
|
|
105
129
|
bunShell.throws(shouldThrow);
|
|
106
130
|
return shell;
|
|
131
|
+
},
|
|
132
|
+
echo: (newShouldEcho) => {
|
|
133
|
+
shouldEcho = newShouldEcho;
|
|
134
|
+
return shell;
|
|
107
135
|
}
|
|
108
136
|
});
|
|
109
137
|
return shell;
|
package/dist/shell.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shell.js","names":[],"sources":["../src/shell.ts"],"sourcesContent":["import { styleText } from \"node:util\"\n\nimport bun from \"bun\"\nimport type { $ as BunShell, ShellExpression } from \"bun\"\n\ntype ShellEnv = Parameters<BunShell.ShellPromise[\"env\"]>[0]\n\n/** Changes each method of `T` that returns `From` to return `To`, keeping Bun's docs on it. */\ntype Rechain<T, From, To> = {\n [K in keyof T]: T[K] extends (...args: infer Args) => From ? (...args: Args) => To : T[K]\n}\n\n// Leave out `stdin` because Bun's types declare it but the runtime doesn't have it.\ntype BunCommandMethods = Omit<BunShell.ShellPromise, keyof Promise<BunShell.ShellOutput> | \"stdin\">\n\n// Treat every other member of Bun's `$` as a setting, so a new one fails to compile until `createShell` handles it.\ntype ShellSettingKey = Exclude<keyof BunShell, \"braces\" | \"escape\" | \"Shell\" | \"ShellError\" | \"ShellPromise\">\n\n/** Runs commands like Bun's `$`, with settings that apply to every later command from the same shell. */\nexport interface Shell extends Rechain<Pick<BunShell, ShellSettingKey>, BunShell, Shell> {\n (strings: TemplateStringsArray, ...expressions: ShellExpression[]): ShellCommand\n}\n\n/** Formats an interpolated value the way Bun's shell escapes it. */\nconst formatExpression = (expression: ShellExpression): string => {\n if (Array.isArray(expression)) return expression.map(formatExpression).join(\" \")\n if (typeof expression === \"object\" && expression !== null && \"raw\" in expression) return expression.raw\n // Print streams, files, and subprocesses as their default string because they have no command text.\n // oxlint-disable-next-line typescript/no-base-to-string\n return bun.$.escape(String(expression))\n}\n\n/** Checks whether bask should turn on color for a command whose output goes to the terminal. */\nconst shouldForceColor = (env: NonNullable<ShellEnv>) =>\n process.stdout.isTTY &&\n process.stderr.isTTY &&\n // oxlint-disable-next-line node/no-process-env - check the user's own color settings\n [process.env, env].every((vars) => vars[\"FORCE_COLOR\"] === undefined && vars[\"NO_COLOR\"] === undefined)\n\n/**\n * Wraps Bun's `ShellPromise` to set `FORCE_COLOR` only on commands whose output is shown. Bun's pipes hide the terminal\n * from commands, so they turn color off unless forced.\n */\nclass ShellCommand\n implements PromiseLike<BunShell.ShellOutput>, Rechain<BunCommandMethods, BunShell.ShellPromise, ShellCommand>\n{\n readonly #promise: BunShell.ShellPromise\n #env: ShellEnv\n #isQuiet = false\n #isPrepared = false\n\n public constructor(promise: BunShell.ShellPromise, env: ShellEnv) {\n this.#promise = promise\n this.#env = env\n }\n\n public cwd(newCwd: string) {\n void this.#promise.cwd(newCwd)\n return this\n }\n\n public env(newEnv: ShellEnv) {\n this.#env = newEnv\n return this\n }\n\n public quiet(isQuiet = true) {\n void this.#promise.quiet(isQuiet)\n this.#isQuiet = isQuiet\n return this\n }\n\n public nothrow() {\n void this.#promise.nothrow()\n return this\n }\n\n public throws(shouldThrow: boolean) {\n void this.#promise.throws(shouldThrow)\n return this\n }\n\n public async text(encoding?: BufferEncoding) {\n return this.#prepare(false).text(encoding)\n }\n\n public async json() {\n // oxlint-disable-next-line typescript/no-unsafe-return - match Bun's `json()`, which returns `any`\n return this.#prepare(false).json()\n }\n\n public lines() {\n return this.#prepare(false).lines()\n }\n\n public async arrayBuffer() {\n return this.#prepare(false).arrayBuffer()\n }\n\n public async blob() {\n return this.#prepare(false).blob()\n }\n\n // oxlint-disable-next-line unicorn/no-thenable - awaiting a command runs it, like Bun's `ShellPromise`\n public async then<TResult1 = BunShell.ShellOutput, TResult2 = never>(\n onfulfilled?: ((value: BunShell.ShellOutput) => TResult1 | PromiseLike<TResult1>) | null,\n onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,\n ) {\n return this.#prepare(!this.#isQuiet).then(onfulfilled, onrejected)\n }\n\n public async catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null) {\n return this.then(undefined, onrejected)\n }\n\n public async finally(onfinally?: (() => void) | null) {\n return this.#prepare(!this.#isQuiet).finally(onfinally)\n }\n\n /** Sets the command's environment once, before Bun starts the command. */\n // oxlint-disable-next-line typescript/promise-function-async - an async function would await the command and start it\n #prepare(isShown: boolean) {\n // Skip a command that already started, because Bun rejects `env` on it.\n if (this.#isPrepared) return this.#promise\n this.#isPrepared = true\n\n // Pass `process.env` even without color because Bun copies it when the command is created, not when it runs.\n // oxlint-disable-next-line node/no-process-env - read the environment as it is when the command runs\n const env = this.#env ?? process.env\n void this.#promise.env(isShown && shouldForceColor(env) ? { ...env, FORCE_COLOR: \"1\" } : env)\n return this.#promise\n }\n}\n\n/** Creates a shell that prints each command to stderr before running it. */\nexport const createShell = () => {\n // Use a separate instance so settings on Bun's global `$` or another shell don't apply here.\n const bunShell = new bun.$.Shell()\n // Keep `env` here instead of on `bunShell` because `ShellCommand` passes it when each command runs.\n let shellEnv: ShellEnv\n\n const run = (strings: TemplateStringsArray, ...expressions: ShellExpression[]) => {\n // Use `String.raw` because Bun's `$` also reads the raw template strings.\n const command = String.raw({ raw: strings.raw }, ...expressions.map(formatExpression))\n // Write to `process.stderr` directly because Bun colors `console.error` output red in a terminal.\n process.stderr.write(`${styleText(\"bold\", command.trim(), { stream: process.stderr })}\\n`)\n return new ShellCommand(bunShell(strings, ...expressions), shellEnv)\n }\n\n const shell: Shell = Object.assign(run, {\n cwd: (newCwd?: string) => {\n bunShell.cwd(newCwd)\n return shell\n },\n env: (newEnv?: ShellEnv) => {\n shellEnv = newEnv\n return shell\n },\n nothrow: () => {\n bunShell.nothrow()\n return shell\n },\n throws: (shouldThrow: boolean) => {\n bunShell.throws(shouldThrow)\n return shell\n },\n })\n\n return shell\n}\n"],"mappings":";;;;AAwBA,MAAM,oBAAoB,eAAwC;CAChE,IAAI,MAAM,QAAQ,UAAU,GAAG,OAAO,WAAW,IAAI,gBAAgB,CAAC,CAAC,KAAK,GAAG;CAC/E,IAAI,OAAO,eAAe,YAAY,eAAe,QAAQ,SA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AK,KAAA,GAAW,UAAU;CACxC;CAEA,MAAa,QAAQ,WAAiC;EACpD,OAAO,KAAK,SAAS,CAAC,KAAK,QAAQ,CAAC,CAAC,QAAQ,SAAS;CACxD;;CAIA,SAAS,SAAkB;EAEzB,IAAI,KAAK,aAAa,OAAO,KAAK;EAClC,KAAK,cAAc;EAInB,MAAM,MAAM,KAAK,QAAQ,QAAQ;EACjC,KAAU,SAAS,IAAI,WAAW,iBAAiB,GAAG,IAAI;GAAE,GAAG;GAAK,aAAa;EAAI,IAAI,GAAG;EAC5F,OAAO,KAAK;CACd;AACF;;AAGA,MAAa,oBAAoB;CAE/B,MAAM,WAAW,IAAI,IAAI,EAAE,MAAM;CAEjC,IAAI;CAEJ,MAAM,OAAO,SAA+B,GAAG,gBAAmC;EAEhF,MAAM,UAAU,OAAO,IAAI,EAAE,KAAK,QAAQ,IAAI,GAAG,GAAG,YAAY,IAAI,gBAAgB,CAAC;EAErF,QAAQ,OAAO,MAAM,GAAG,UAAU,QAAQ,QAAQ,KAAK,GAAG,EAAE,QAAQ,QAAQ,OAAO,CAAC,EAAE,GAAG;EACzF,OAAO,IAAI,aAAa,SAAS,SAAS,GAAG,WAAW,GAAG,QAAQ;CACrE;CAEA,MAAM,QAAe,OAAO,OAAO,KAAK;EACtC,MAAM,WAAoB;GACxB,SAAS,IAAI,MAAM;GACnB,OAAO;EACT;EACA,MAAM,WAAsB;GAC1B,WAAW;GACX,OAAO;EACT;EACA,eAAe;GACb,SAAS,QAAQ;GACjB,OAAO;EACT;EACA,SAAS,gBAAyB;GAChC,SAAS,OAAO,WAAW;GAC3B,OAAO;EACT;CACF,CAAC;CAED,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"shell.js","names":[],"sources":["../src/shell.ts"],"sourcesContent":["import { styleText } from \"node:util\"\n\nimport bun from \"bun\"\nimport type { $ as BunShell, ShellExpression } from \"bun\"\n\ntype ShellEnv = Parameters<BunShell.ShellPromise[\"env\"]>[0]\n\n/** Runs commands like Bun's `$`, with settings that apply to every later command from the same shell. */\nexport interface Shell {\n (strings: TemplateStringsArray, ...expressions: ShellExpression[]): ShellCommand\n /** Change the default working directory for commands run by this shell. */\n cwd: (newCwd?: string) => Shell\n /** Change the default environment variables for commands run by this shell. */\n env: (newEnv?: ShellEnv) => Shell\n /** Configure the shell to not throw an exception on non-zero exit codes. */\n nothrow: () => Shell\n /** Configure whether the shell should throw an exception on non-zero exit codes. */\n throws: (shouldThrow: boolean) => Shell\n /** Configure whether the shell prints each command before running it. */\n echo: (shouldEcho: boolean) => Shell\n}\n\n/** Formats an interpolated value the way Bun's shell escapes it. */\nconst formatExpression = (expression: ShellExpression): string => {\n if (Array.isArray(expression)) return expression.map(formatExpression).join(\" \")\n if (typeof expression === \"object\" && expression !== null && \"raw\" in expression) return expression.raw\n // Print streams, files, and subprocesses as their default string because they have no command text.\n // oxlint-disable-next-line typescript/no-base-to-string\n return bun.$.escape(String(expression))\n}\n\n/** Checks whether bask should turn on color for a command whose output goes to the terminal. */\nconst shouldForceColor = (env: NonNullable<ShellEnv>) =>\n process.stdout.isTTY &&\n process.stderr.isTTY &&\n // oxlint-disable-next-line node/no-process-env - check the user's own color settings\n [process.env, env].every((vars) => vars[\"FORCE_COLOR\"] === undefined && vars[\"NO_COLOR\"] === undefined)\n\n/**\n * Wraps Bun's `ShellPromise` to set `FORCE_COLOR` only on commands whose output is shown. Bun's pipes hide the terminal\n * from commands, so they turn color off unless forced.\n */\nclass ShellCommand implements PromiseLike<BunShell.ShellOutput> {\n readonly #promise: BunShell.ShellPromise\n readonly #command: string\n #env: ShellEnv\n #shouldEcho: boolean\n #isQuiet = false\n #isPrepared = false\n\n public constructor(\n promise: BunShell.ShellPromise,\n { command, env, shouldEcho }: { command: string; env: ShellEnv; shouldEcho: boolean },\n ) {\n this.#promise = promise\n this.#command = command\n this.#env = env\n this.#shouldEcho = shouldEcho\n }\n\n /** Change the current working directory of the shell. */\n public cwd(newCwd: string) {\n void this.#promise.cwd(newCwd)\n return this\n }\n\n /** Set environment variables for the shell. */\n public env(newEnv: ShellEnv) {\n this.#env = newEnv\n return this\n }\n\n /** Configure the shell to only buffer the output instead of also writing it to stdout and stderr. */\n public quiet(isQuiet = true) {\n void this.#promise.quiet(isQuiet)\n this.#isQuiet = isQuiet\n return this\n }\n\n /** Configure the shell to not throw an exception on non-zero exit codes. */\n public nothrow() {\n void this.#promise.nothrow()\n return this\n }\n\n /** Configure whether the shell should throw an exception on non-zero exit codes. */\n public throws(shouldThrow: boolean) {\n void this.#promise.throws(shouldThrow)\n return this\n }\n\n /** Configure whether the shell prints the command before running it. */\n public echo(shouldEcho: boolean) {\n this.#shouldEcho = shouldEcho\n return this\n }\n\n /** Read from stdout as a string. Automatically calls {@link quiet}. */\n public async text(encoding?: BufferEncoding) {\n return this.#prepare(false).text(encoding)\n }\n\n /** Read from stdout as a JSON object. Automatically calls {@link quiet}. */\n public async json() {\n // oxlint-disable-next-line typescript/no-unsafe-return - match Bun's `json()`, which returns `any`\n return this.#prepare(false).json()\n }\n\n /** Read from stdout as a string, line by line. Automatically calls {@link quiet}. */\n public lines() {\n return this.#prepare(false).lines()\n }\n\n /** Read from stdout as an ArrayBuffer. Automatically calls {@link quiet}. */\n public async arrayBuffer() {\n return this.#prepare(false).arrayBuffer()\n }\n\n /** Read from stdout as a Blob. Automatically calls {@link quiet}. */\n public async blob() {\n return this.#prepare(false).blob()\n }\n\n // oxlint-disable-next-line unicorn/no-thenable - awaiting a command runs it, like Bun's `ShellPromise`\n public async then<TResult1 = BunShell.ShellOutput, TResult2 = never>(\n onfulfilled?: ((value: BunShell.ShellOutput) => TResult1 | PromiseLike<TResult1>) | null,\n onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,\n ) {\n return this.#prepare(!this.#isQuiet).then(onfulfilled, onrejected)\n }\n\n public async catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null) {\n return this.then(undefined, onrejected)\n }\n\n public async finally(onfinally?: (() => void) | null) {\n return this.#prepare(!this.#isQuiet).finally(onfinally)\n }\n\n /** Prints the command and sets its environment once, before Bun starts the command. */\n // oxlint-disable-next-line typescript/promise-function-async - an async function would await the command and start it\n #prepare(isShown: boolean) {\n // Skip a command that already started, because Bun rejects `env` on it.\n if (this.#isPrepared) return this.#promise\n this.#isPrepared = true\n\n // Write to `process.stderr` directly because Bun colors `console.error` output red in a terminal.\n if (this.#shouldEcho) process.stderr.write(`${styleText(\"bold\", this.#command, { stream: process.stderr })}\\n`)\n\n // Pass `process.env` even without color because Bun copies it when the command is created, not when it runs.\n // oxlint-disable-next-line node/no-process-env - read the environment as it is when the command runs\n const env = this.#env ?? process.env\n void this.#promise.env(isShown && shouldForceColor(env) ? { ...env, FORCE_COLOR: \"1\" } : env)\n return this.#promise\n }\n}\n\n/** Creates a shell that prints each command to stderr before running it. */\nexport const createShell = () => {\n // Use a separate instance so settings on Bun's global `$` or another shell don't apply here.\n const bunShell = new bun.$.Shell()\n // Keep `env` here instead of on `bunShell` because `ShellCommand` passes it when each command runs.\n let shellEnv: ShellEnv\n let shouldEcho = true\n\n const run = (strings: TemplateStringsArray, ...expressions: ShellExpression[]) => {\n // Use `String.raw` because Bun's `$` also reads the raw template strings.\n const command = String.raw({ raw: strings.raw }, ...expressions.map(formatExpression)).trim()\n return new ShellCommand(bunShell(strings, ...expressions), { command, env: shellEnv, shouldEcho })\n }\n\n const shell: Shell = Object.assign(run, {\n cwd: (newCwd?: string) => {\n bunShell.cwd(newCwd)\n return shell\n },\n env: (newEnv?: ShellEnv) => {\n shellEnv = newEnv\n return shell\n },\n nothrow: () => {\n bunShell.nothrow()\n return shell\n },\n throws: (shouldThrow: boolean) => {\n bunShell.throws(shouldThrow)\n return shell\n },\n echo: (newShouldEcho: boolean) => {\n shouldEcho = newShouldEcho\n return shell\n },\n })\n\n return shell\n}\n"],"mappings":";;;;AAuBA,MAAM,oBAAoB,eAAwC;CAChE,IAAI,MAAM,QAAQ,UAAU,GAAG,OAAO,WAAW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AO;EACT;EACA,SAAS,gBAAyB;GAChC,SAAS,OAAO,WAAW;GAC3B,OAAO;EACT;EACA,OAAO,kBAA2B;GAChC,aAAa;GACb,OAAO;EACT;CACF,CAAC;CAED,OAAO;AACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adamhl8/bask",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Write your tasks in TypeScript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"build-tool",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"access": "public"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
|
-
"prepare": "
|
|
51
|
+
"prepare": "tsdown && bun install --ignore-scripts --no-summary && bunx bask prepare"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@adamhl8/bask": "file:.",
|