@adamhl8/bask 0.1.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 +18 -4
- package/dist/shell.d.ts +53 -2
- package/dist/shell.d.ts.map +1 -1
- package/dist/shell.js +129 -6
- package/dist/shell.js.map +1 -1
- package/dist/task.d.ts +3 -3
- package/dist/task.d.ts.map +1 -1
- package/dist/task.js +5 -5
- package/dist/task.js.map +1 -1
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -91,17 +91,19 @@ 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
|
```
|
|
104
102
|
|
|
103
|
+
Bun's `$` pipes each command's output, so commands can't tell they're printing to a terminal and usually turn color off. When bask's output goes to a terminal, `sh` sets `FORCE_COLOR=1` on the commands whose output you see. Commands you capture with `.text()`, `.json()`, `.lines()`, `.blob()`, or `.arrayBuffer()` don't get it, and neither do `.quiet()` commands, so captured output is plain text. A `FORCE_COLOR` or `NO_COLOR` in your environment or in `.env()` takes precedence.
|
|
104
|
+
|
|
105
|
+
The `stdout` and `stderr` of an awaited `sh` command hold exactly what it printed, colors included.
|
|
106
|
+
|
|
105
107
|
When a command fails, bask stops and names the task it failed in, then exits with the command's exit code:
|
|
106
108
|
|
|
107
109
|
```
|
|
@@ -110,6 +112,18 @@ error: task 'lint' failed with exit code 2
|
|
|
110
112
|
|
|
111
113
|
Any other error thrown in a task still shows its stack trace.
|
|
112
114
|
|
|
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.
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
export const buildWeb = task(async ({ sh }) => {
|
|
119
|
+
sh.cwd("packages/web")
|
|
120
|
+
await sh`bun install`
|
|
121
|
+
await sh`bun run build`
|
|
122
|
+
})
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Each task and each of its hooks gets its own `sh`, so these settings never reach another task or hook. Settings on Bun's global `$` don't apply to `sh`.
|
|
126
|
+
|
|
113
127
|
### Extending tasks
|
|
114
128
|
|
|
115
129
|
`before` and `after` add hooks that run around a task. A hook added in your `bask.ts` also runs when another task calls that task, including tasks you imported. This lets a project extend shared tasks without replacing them.
|
package/dist/shell.d.ts
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
import { $, ShellExpression } from "bun";
|
|
2
2
|
//#region src/shell.d.ts
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
type ShellEnv = Parameters<$.ShellPromise["env"]>[0];
|
|
4
|
+
/** Runs commands like Bun's `$`, with settings that apply to every later command from the same shell. */
|
|
5
|
+
export interface Shell {
|
|
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;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Wraps Bun's `ShellPromise` to set `FORCE_COLOR` only on commands whose output is shown. Bun's pipes hide the terminal
|
|
20
|
+
* from commands, so they turn color off unless forced.
|
|
21
|
+
*/
|
|
22
|
+
declare class ShellCommand implements PromiseLike<$.ShellOutput> {
|
|
23
|
+
#private;
|
|
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. */
|
|
30
|
+
cwd(newCwd: string): this;
|
|
31
|
+
/** Set environment variables for the shell. */
|
|
32
|
+
env(newEnv: ShellEnv): this;
|
|
33
|
+
/** Configure the shell to only buffer the output instead of also writing it to stdout and stderr. */
|
|
34
|
+
quiet(isQuiet?: boolean): this;
|
|
35
|
+
/** Configure the shell to not throw an exception on non-zero exit codes. */
|
|
36
|
+
nothrow(): this;
|
|
37
|
+
/** Configure whether the shell should throw an exception on non-zero exit codes. */
|
|
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}. */
|
|
42
|
+
text(encoding?: BufferEncoding): Promise<string>;
|
|
43
|
+
/** Read from stdout as a JSON object. Automatically calls {@link quiet}. */
|
|
44
|
+
json(): Promise<any>;
|
|
45
|
+
/** Read from stdout as a string, line by line. Automatically calls {@link quiet}. */
|
|
46
|
+
lines(): AsyncIterable<string>;
|
|
47
|
+
/** Read from stdout as an ArrayBuffer. Automatically calls {@link quiet}. */
|
|
48
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
49
|
+
/** Read from stdout as a Blob. Automatically calls {@link quiet}. */
|
|
50
|
+
blob(): Promise<Blob>;
|
|
51
|
+
then<TResult1 = $.ShellOutput, TResult2 = never>(onfulfilled?: ((value: $.ShellOutput) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
52
|
+
catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null): Promise<TResult | $.ShellOutput>;
|
|
53
|
+
finally(onfinally?: (() => void) | null): Promise<$.ShellOutput>;
|
|
54
|
+
}
|
|
5
55
|
//#endregion
|
|
56
|
+
export {};
|
|
6
57
|
//# sourceMappingURL=shell.d.ts.map
|
package/dist/shell.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shell.d.ts","names":["BunShell"],"sources":["../src/shell.ts"],"mappings":";;;
|
|
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
|
@@ -7,13 +7,136 @@ const formatExpression = (expression) => {
|
|
|
7
7
|
if (typeof expression === "object" && expression !== null && "raw" in expression) return expression.raw;
|
|
8
8
|
return bun.$.escape(String(expression));
|
|
9
9
|
};
|
|
10
|
-
/**
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
/** Checks whether bask should turn on color for a command whose output goes to the terminal. */
|
|
11
|
+
const shouldForceColor = (env) => process.stdout.isTTY && process.stderr.isTTY && [process.env, env].every((vars) => vars["FORCE_COLOR"] === void 0 && vars["NO_COLOR"] === void 0);
|
|
12
|
+
/**
|
|
13
|
+
* Wraps Bun's `ShellPromise` to set `FORCE_COLOR` only on commands whose output is shown. Bun's pipes hide the terminal
|
|
14
|
+
* from commands, so they turn color off unless forced.
|
|
15
|
+
*/
|
|
16
|
+
var ShellCommand = class {
|
|
17
|
+
#promise;
|
|
18
|
+
#command;
|
|
19
|
+
#env;
|
|
20
|
+
#shouldEcho;
|
|
21
|
+
#isQuiet = false;
|
|
22
|
+
#isPrepared = false;
|
|
23
|
+
constructor(promise, { command, env, shouldEcho }) {
|
|
24
|
+
this.#promise = promise;
|
|
25
|
+
this.#command = command;
|
|
26
|
+
this.#env = env;
|
|
27
|
+
this.#shouldEcho = shouldEcho;
|
|
28
|
+
}
|
|
29
|
+
/** Change the current working directory of the shell. */
|
|
30
|
+
cwd(newCwd) {
|
|
31
|
+
this.#promise.cwd(newCwd);
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
/** Set environment variables for the shell. */
|
|
35
|
+
env(newEnv) {
|
|
36
|
+
this.#env = newEnv;
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
/** Configure the shell to only buffer the output instead of also writing it to stdout and stderr. */
|
|
40
|
+
quiet(isQuiet = true) {
|
|
41
|
+
this.#promise.quiet(isQuiet);
|
|
42
|
+
this.#isQuiet = isQuiet;
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
/** Configure the shell to not throw an exception on non-zero exit codes. */
|
|
46
|
+
nothrow() {
|
|
47
|
+
this.#promise.nothrow();
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
/** Configure whether the shell should throw an exception on non-zero exit codes. */
|
|
51
|
+
throws(shouldThrow) {
|
|
52
|
+
this.#promise.throws(shouldThrow);
|
|
53
|
+
return this;
|
|
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}. */
|
|
61
|
+
async text(encoding) {
|
|
62
|
+
return this.#prepare(false).text(encoding);
|
|
63
|
+
}
|
|
64
|
+
/** Read from stdout as a JSON object. Automatically calls {@link quiet}. */
|
|
65
|
+
async json() {
|
|
66
|
+
return this.#prepare(false).json();
|
|
67
|
+
}
|
|
68
|
+
/** Read from stdout as a string, line by line. Automatically calls {@link quiet}. */
|
|
69
|
+
lines() {
|
|
70
|
+
return this.#prepare(false).lines();
|
|
71
|
+
}
|
|
72
|
+
/** Read from stdout as an ArrayBuffer. Automatically calls {@link quiet}. */
|
|
73
|
+
async arrayBuffer() {
|
|
74
|
+
return this.#prepare(false).arrayBuffer();
|
|
75
|
+
}
|
|
76
|
+
/** Read from stdout as a Blob. Automatically calls {@link quiet}. */
|
|
77
|
+
async blob() {
|
|
78
|
+
return this.#prepare(false).blob();
|
|
79
|
+
}
|
|
80
|
+
async then(onfulfilled, onrejected) {
|
|
81
|
+
return this.#prepare(!this.#isQuiet).then(onfulfilled, onrejected);
|
|
82
|
+
}
|
|
83
|
+
async catch(onrejected) {
|
|
84
|
+
return this.then(void 0, onrejected);
|
|
85
|
+
}
|
|
86
|
+
async finally(onfinally) {
|
|
87
|
+
return this.#prepare(!this.#isQuiet).finally(onfinally);
|
|
88
|
+
}
|
|
89
|
+
/** Prints the command and sets its environment once, before Bun starts the command. */
|
|
90
|
+
#prepare(isShown) {
|
|
91
|
+
if (this.#isPrepared) return this.#promise;
|
|
92
|
+
this.#isPrepared = true;
|
|
93
|
+
if (this.#shouldEcho) process.stderr.write(`${styleText("bold", this.#command, { stream: process.stderr })}\n`);
|
|
94
|
+
const env = this.#env ?? process.env;
|
|
95
|
+
this.#promise.env(isShown && shouldForceColor(env) ? {
|
|
96
|
+
...env,
|
|
97
|
+
FORCE_COLOR: "1"
|
|
98
|
+
} : env);
|
|
99
|
+
return this.#promise;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
/** Creates a shell that prints each command to stderr before running it. */
|
|
103
|
+
const createShell = () => {
|
|
104
|
+
const bunShell = new bun.$.Shell();
|
|
105
|
+
let shellEnv;
|
|
106
|
+
let shouldEcho = true;
|
|
107
|
+
const run = (strings, ...expressions) => {
|
|
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
|
+
});
|
|
114
|
+
};
|
|
115
|
+
const shell = Object.assign(run, {
|
|
116
|
+
cwd: (newCwd) => {
|
|
117
|
+
bunShell.cwd(newCwd);
|
|
118
|
+
return shell;
|
|
119
|
+
},
|
|
120
|
+
env: (newEnv) => {
|
|
121
|
+
shellEnv = newEnv;
|
|
122
|
+
return shell;
|
|
123
|
+
},
|
|
124
|
+
nothrow: () => {
|
|
125
|
+
bunShell.nothrow();
|
|
126
|
+
return shell;
|
|
127
|
+
},
|
|
128
|
+
throws: (shouldThrow) => {
|
|
129
|
+
bunShell.throws(shouldThrow);
|
|
130
|
+
return shell;
|
|
131
|
+
},
|
|
132
|
+
echo: (newShouldEcho) => {
|
|
133
|
+
shouldEcho = newShouldEcho;
|
|
134
|
+
return shell;
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
return shell;
|
|
15
138
|
};
|
|
16
139
|
//#endregion
|
|
17
|
-
export {
|
|
140
|
+
export { createShell };
|
|
18
141
|
|
|
19
142
|
//# sourceMappingURL=shell.js.map
|
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\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/** Prints each command to stderr, then runs it with Bun's `$`. */\n// oxlint-disable-next-line typescript/promise-function-async - return Bun's `ShellPromise` as is, because an async function would wrap it and drop `.quiet()` and `.text()`.\nexport const ShellWrapper = (\n strings: TemplateStringsArray,\n ...expressions: ShellExpression[]\n): BunShell.ShellPromise => {\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 bun.$(strings, ...expressions)\n}\n"],"mappings":";;;;AAMA,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,SAAS,YAAY,OAAO,WAAW;CAGpG,OAAO,IAAI,EAAE,OAAO,OAAO,UAAU,CAAC;AACxC;;AAIA,MAAa,gBACX,SACA,GAAG,gBACuB;CAE1B,MAAM,UAAU,OAAO,IAAI,EAAE,KAAK,QAAQ,IAAI,GAAG,GAAG,YAAY,IAAI,gBAAgB,CAAC;CAErF,QAAQ,OAAO,MAAM,GAAG,UAAU,QAAQ,QAAQ,KAAK,GAAG,EAAE,QAAQ,QAAQ,OAAO,CAAC,EAAE,GAAG;CACzF,OAAO,IAAI,EAAE,SAAS,GAAG,WAAW;AACtC"}
|
|
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,IAAI,gBAAgB,CAAC,CAAC,KAAK,GAAG;CAC/E,IAAI,OAAO,eAAe,YAAY,eAAe,QAAQ,SAAS,YAAY,OAAO,WAAW;CAGpG,OAAO,IAAI,EAAE,OAAO,OAAO,UAAU,CAAC;AACxC;;AAGA,MAAM,oBAAoB,QACxB,QAAQ,OAAO,SACf,QAAQ,OAAO,SAEf,CAAC,QAAQ,KAAK,GAAG,CAAC,CAAC,OAAO,SAAS,KAAK,mBAAmB,KAAA,KAAa,KAAK,gBAAgB,KAAA,CAAS;;;;;AAMxG,IAAM,eAAN,MAAgE;CAC9D;CACA;CACA;CACA;CACA,WAAW;CACX,cAAc;CAEd,YACE,SACA,EAAE,SAAS,KAAK,cAChB;EACA,KAAK,WAAW;EAChB,KAAK,WAAW;EAChB,KAAK,OAAO;EACZ,KAAK,cAAc;CACrB;;CAGA,IAAW,QAAgB;EACzB,KAAU,SAAS,IAAI,MAAM;EAC7B,OAAO;CACT;;CAGA,IAAW,QAAkB;EAC3B,KAAK,OAAO;EACZ,OAAO;CACT;;CAGA,MAAa,UAAU,MAAM;EAC3B,KAAU,SAAS,MAAM,OAAO;EAChC,KAAK,WAAW;EAChB,OAAO;CACT;;CAGA,UAAiB;EACf,KAAU,SAAS,QAAQ;EAC3B,OAAO;CACT;;CAGA,OAAc,aAAsB;EAClC,KAAU,SAAS,OAAO,WAAW;EACrC,OAAO;CACT;;CAGA,KAAY,YAAqB;EAC/B,KAAK,cAAc;EACnB,OAAO;CACT;;CAGA,MAAa,KAAK,UAA2B;EAC3C,OAAO,KAAK,SAAS,KAAK,CAAC,CAAC,KAAK,QAAQ;CAC3C;;CAGA,MAAa,OAAO;EAElB,OAAO,KAAK,SAAS,KAAK,CAAC,CAAC,KAAK;CACnC;;CAGA,QAAe;EACb,OAAO,KAAK,SAAS,KAAK,CAAC,CAAC,MAAM;CACpC;;CAGA,MAAa,cAAc;EACzB,OAAO,KAAK,SAAS,KAAK,CAAC,CAAC,YAAY;CAC1C;;CAGA,MAAa,OAAO;EAClB,OAAO,KAAK,SAAS,KAAK,CAAC,CAAC,KAAK;CACnC;CAGA,MAAa,KACX,aACA,YACA;EACA,OAAO,KAAK,SAAS,CAAC,KAAK,QAAQ,CAAC,CAAC,KAAK,aAAa,UAAU;CACnE;CAEA,MAAa,MAAuB,YAA2E;EAC7G,OAAO,KAAK,KAAK,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;EAGnB,IAAI,KAAK,aAAa,QAAQ,OAAO,MAAM,GAAG,UAAU,QAAQ,KAAK,UAAU,EAAE,QAAQ,QAAQ,OAAO,CAAC,EAAE,GAAG;EAI9G,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;CACJ,IAAI,aAAa;CAEjB,MAAM,OAAO,SAA+B,GAAG,gBAAmC;EAEhF,MAAM,UAAU,OAAO,IAAI,EAAE,KAAK,QAAQ,IAAI,GAAG,GAAG,YAAY,IAAI,gBAAgB,CAAC,CAAC,CAAC,KAAK;EAC5F,OAAO,IAAI,aAAa,SAAS,SAAS,GAAG,WAAW,GAAG;GAAE;GAAS,KAAK;GAAU;EAAW,CAAC;CACnG;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;EACA,OAAO,kBAA2B;GAChC,aAAa;GACb,OAAO;EACT;CACF,CAAC;CAED,OAAO;AACT"}
|
package/dist/task.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Shell } from "./shell.js";
|
|
2
2
|
//#region src/task.d.ts
|
|
3
|
+
type AsyncOrSyncReturn = Promise<void> | void;
|
|
3
4
|
interface TaskOptions {
|
|
4
5
|
desc?: string;
|
|
5
6
|
/** Keeps the task runnable but leaves it out of the task list. */
|
|
@@ -8,9 +9,8 @@ interface TaskOptions {
|
|
|
8
9
|
alias?: string | string[];
|
|
9
10
|
}
|
|
10
11
|
interface TaskContext {
|
|
11
|
-
sh:
|
|
12
|
+
sh: Shell;
|
|
12
13
|
}
|
|
13
|
-
type AsyncOrSyncReturn = Promise<void> | void;
|
|
14
14
|
type TaskFn = (ctx: TaskContext) => AsyncOrSyncReturn;
|
|
15
15
|
export interface Task {
|
|
16
16
|
(): Promise<void>;
|
package/dist/task.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"task.d.ts","names":[],"sources":["../src/task.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"task.d.ts","names":[],"sources":["../src/task.ts"],"mappings":";;KAGK,oBAAoB;UAEf;EACR;;EAEA;;EAEA;;UAGQ;EACR,IAAI;;KAGD,UAAU,KAAK,gBAAgB;iBAEnB;MACX;EACJ,SAAS;EACT,YAAY,KAAK;EACjB,WAAW,KAAK;;qBAKL,SAAM,mBAAqB,SAAS;;wBAQjC,KAAK,IAAI,SAAS;wBAClB,KAAK,SAAS,aAAa,IAAI,SAAS"}
|
package/dist/task.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createShell } from "./shell.js";
|
|
2
2
|
//#region src/task.ts
|
|
3
3
|
const tasks = /* @__PURE__ */ new WeakSet();
|
|
4
4
|
const isTask = (value) => typeof value === "function" && tasks.has(value);
|
|
@@ -8,7 +8,6 @@ const getFailedTask = (error) => failedTasks.get(error);
|
|
|
8
8
|
function task(optionsOrFn, maybeFn) {
|
|
9
9
|
const [options, fn] = typeof optionsOrFn === "function" ? [{}, optionsOrFn] : [optionsOrFn, maybeFn];
|
|
10
10
|
if (!fn) throw new Error("task() needs a callback");
|
|
11
|
-
const ctx = { sh: ShellWrapper };
|
|
12
11
|
const beforeFns = [];
|
|
13
12
|
const afterFns = [];
|
|
14
13
|
let runPromise;
|
|
@@ -23,10 +22,11 @@ function task(optionsOrFn, maybeFn) {
|
|
|
23
22
|
assertNotStarted();
|
|
24
23
|
afterFns.push(...fns);
|
|
25
24
|
};
|
|
25
|
+
const runWithShell = async (taskFn) => taskFn({ sh: createShell() });
|
|
26
26
|
const runAll = async () => {
|
|
27
|
-
for (const beforeFn of beforeFns) await beforeFn
|
|
28
|
-
await fn
|
|
29
|
-
for (const afterFn of afterFns) await afterFn
|
|
27
|
+
for (const beforeFn of beforeFns) await runWithShell(beforeFn);
|
|
28
|
+
await runWithShell(fn);
|
|
29
|
+
for (const afterFn of afterFns) await runWithShell(afterFn);
|
|
30
30
|
};
|
|
31
31
|
const run = async () => {
|
|
32
32
|
runPromise ??= runAll();
|
package/dist/task.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"task.js","names":[],"sources":["../src/task.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"task.js","names":[],"sources":["../src/task.ts"],"sourcesContent":["import { createShell } from \"#shell.ts\"\nimport type { Shell } from \"#shell.ts\"\n\ntype AsyncOrSyncReturn = Promise<void> | void\n\ninterface TaskOptions {\n desc?: string\n /** Keeps the task runnable but leaves it out of the task list. */\n hidden?: boolean\n /** Additional names to run the task. */\n alias?: string | string[]\n}\n\ninterface TaskContext {\n sh: Shell\n}\n\ntype TaskFn = (ctx: TaskContext) => AsyncOrSyncReturn\n\nexport interface Task {\n (): Promise<void>\n options: TaskOptions\n before: (...fns: TaskFn[]) => void\n after: (...fns: TaskFn[]) => void\n}\n\nconst tasks = new WeakSet<object>()\n\nexport const isTask = (value: unknown): value is Task => typeof value === \"function\" && tasks.has(value)\n\nconst failedTasks = new WeakMap<Error, object>()\n\n/** Returns the innermost task that the error failed, so the CLI can name it. */\nexport const getFailedTask = (error: Error) => failedTasks.get(error)\n\n/** Registers a task that runs at most once per bask invocation, however many times it's called. */\nexport function task(fn: TaskFn): Task\nexport function task(options: TaskOptions, fn: TaskFn): Task\nexport function task(optionsOrFn: TaskOptions | TaskFn, maybeFn?: TaskFn): Task {\n const [options, fn] = typeof optionsOrFn === \"function\" ? [{}, optionsOrFn] : [optionsOrFn, maybeFn]\n if (!fn) throw new Error(\"task() needs a callback\")\n\n const beforeFns: TaskFn[] = []\n const afterFns: TaskFn[] = []\n\n // Store the first call's promise so overlapping and later calls share it instead of rerunning the task.\n let runPromise: Promise<void> | undefined\n\n const assertNotStarted = () => {\n if (runPromise) throw new Error(\"Can't add a hook to a task that has already started\")\n }\n\n const before = (...fns: TaskFn[]) => {\n assertNotStarted()\n beforeFns.push(...fns)\n }\n\n const after = (...fns: TaskFn[]) => {\n assertNotStarted()\n afterFns.push(...fns)\n }\n\n // Give each hook and the task its own shell so settings like `sh.cwd()` stay inside the function that made them.\n const runWithShell = async (taskFn: TaskFn) => taskFn({ sh: createShell() })\n\n const runAll = async () => {\n // oxlint-disable-next-line no-await-in-loop - hooks run in order\n for (const beforeFn of beforeFns) await runWithShell(beforeFn)\n await runWithShell(fn)\n // oxlint-disable-next-line no-await-in-loop - hooks run in order\n for (const afterFn of afterFns) await runWithShell(afterFn)\n }\n\n // Assign `runPromise` before any await so a concurrent call can't start a second run.\n const run = async () => {\n runPromise ??= runAll()\n try {\n await runPromise\n } catch (error) {\n // Record `run` only if no task has yet, because the innermost task's catch runs first.\n // `run` is the same object as the returned task.\n if (error instanceof Error && !failedTasks.has(error)) failedTasks.set(error, run)\n throw error\n }\n }\n\n const newTask: Task = Object.assign(run, { options, before, after })\n tasks.add(newTask)\n return newTask\n}\n"],"mappings":";;AA0BA,MAAM,wBAAQ,IAAI,QAAgB;AAElC,MAAa,UAAU,UAAkC,OAAO,UAAU,cAAc,MAAM,IAAI,KAAK;AAEvG,MAAM,8BAAc,IAAI,QAAuB;;AAG/C,MAAa,iBAAiB,UAAiB,YAAY,IAAI,KAAK;AAKpE,SAAgB,KAAK,aAAmC,SAAwB;CAC9E,MAAM,CAAC,SAAS,MAAM,OAAO,gBAAgB,aAAa,CAAC,CAAC,GAAG,WAAW,IAAI,CAAC,aAAa,OAAO;CACnG,IAAI,CAAC,IAAI,MAAM,IAAI,MAAM,yBAAyB;CAElD,MAAM,YAAsB,CAAC;CAC7B,MAAM,WAAqB,CAAC;CAG5B,IAAI;CAEJ,MAAM,yBAAyB;EAC7B,IAAI,YAAY,MAAM,IAAI,MAAM,qDAAqD;CACvF;CAEA,MAAM,UAAU,GAAG,QAAkB;EACnC,iBAAiB;EACjB,UAAU,KAAK,GAAG,GAAG;CACvB;CAEA,MAAM,SAAS,GAAG,QAAkB;EAClC,iBAAiB;EACjB,SAAS,KAAK,GAAG,GAAG;CACtB;CAGA,MAAM,eAAe,OAAO,WAAmB,OAAO,EAAE,IAAI,YAAY,EAAE,CAAC;CAE3E,MAAM,SAAS,YAAY;EAEzB,KAAK,MAAM,YAAY,WAAW,MAAM,aAAa,QAAQ;EAC7D,MAAM,aAAa,EAAE;EAErB,KAAK,MAAM,WAAW,UAAU,MAAM,aAAa,OAAO;CAC5D;CAGA,MAAM,MAAM,YAAY;EACtB,eAAe,OAAO;EACtB,IAAI;GACF,MAAM;EACR,SAAS,OAAO;GAGd,IAAI,iBAAiB,SAAS,CAAC,YAAY,IAAI,KAAK,GAAG,YAAY,IAAI,OAAO,GAAG;GACjF,MAAM;EACR;CACF;CAEA,MAAM,UAAgB,OAAO,OAAO,KAAK;EAAE;EAAS;EAAQ;CAAM,CAAC;CACnE,MAAM,IAAI,OAAO;CACjB,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,10 +48,11 @@
|
|
|
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
|
-
"@adamhl8/
|
|
54
|
+
"@adamhl8/bask": "file:.",
|
|
55
|
+
"@adamhl8/configs": "^3.0.1",
|
|
55
56
|
"@arethetypeswrong/core": "^0.18.5",
|
|
56
57
|
"@commitlint/cli": "^21.2.3",
|
|
57
58
|
"@commitlint/config-conventional": "^21.2.3",
|