@adamhl8/bask 0.1.0 → 0.2.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 +16 -0
- package/dist/shell.d.ts +31 -2
- package/dist/shell.d.ts.map +1 -1
- package/dist/shell.js +101 -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
|
@@ -102,6 +102,10 @@ export const release = task(async ({ sh }) => {
|
|
|
102
102
|
})
|
|
103
103
|
```
|
|
104
104
|
|
|
105
|
+
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.
|
|
106
|
+
|
|
107
|
+
The `stdout` and `stderr` of an awaited `sh` command hold exactly what it printed, colors included.
|
|
108
|
+
|
|
105
109
|
When a command fails, bask stops and names the task it failed in, then exits with the command's exit code:
|
|
106
110
|
|
|
107
111
|
```
|
|
@@ -110,6 +114,18 @@ error: task 'lint' failed with exit code 2
|
|
|
110
114
|
|
|
111
115
|
Any other error thrown in a task still shows its stack trace.
|
|
112
116
|
|
|
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 `.cwd()` or `.env()` overrides them for that command.
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
export const buildWeb = task(async ({ sh }) => {
|
|
121
|
+
sh.cwd("packages/web")
|
|
122
|
+
await sh`bun install`
|
|
123
|
+
await sh`bun run build`
|
|
124
|
+
})
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
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`.
|
|
128
|
+
|
|
113
129
|
### Extending tasks
|
|
114
130
|
|
|
115
131
|
`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,35 @@
|
|
|
1
1
|
import { $, ShellExpression } from "bun";
|
|
2
2
|
//#region src/shell.d.ts
|
|
3
|
-
|
|
4
|
-
|
|
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
|
+
/** Runs commands like Bun's `$`, with settings that apply to every later command from the same shell. */
|
|
9
|
+
export interface Shell extends Rechain<Pick<$, ShellSettingKey>, $, Shell> {
|
|
10
|
+
(strings: TemplateStringsArray, ...expressions: ShellExpression[]): ShellCommand;
|
|
11
|
+
}
|
|
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
|
+
declare class ShellCommand implements PromiseLike<$.ShellOutput>, Rechain<BunCommandMethods, $.ShellPromise, ShellCommand> {
|
|
17
|
+
#private;
|
|
18
|
+
constructor(promise: $.ShellPromise, env: ShellEnv);
|
|
19
|
+
cwd(newCwd: string): this;
|
|
20
|
+
env(newEnv: ShellEnv): this;
|
|
21
|
+
quiet(isQuiet?: boolean): this;
|
|
22
|
+
nothrow(): this;
|
|
23
|
+
throws(shouldThrow: boolean): this;
|
|
24
|
+
text(encoding?: BufferEncoding): Promise<string>;
|
|
25
|
+
json(): Promise<any>;
|
|
26
|
+
lines(): AsyncIterable<string>;
|
|
27
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
28
|
+
blob(): Promise<Blob>;
|
|
29
|
+
then<TResult1 = $.ShellOutput, TResult2 = never>(onfulfilled?: ((value: $.ShellOutput) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
30
|
+
catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null): Promise<TResult | $.ShellOutput>;
|
|
31
|
+
finally(onfinally?: (() => void) | null): Promise<$.ShellOutput>;
|
|
32
|
+
}
|
|
5
33
|
//#endregion
|
|
34
|
+
export {};
|
|
6
35
|
//# 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;;KAG/B,QAAQ,GAAG,MAAM,SACnB,WAAW,IAAI,EAAE,gBAAe,YAAY,SAAS,YAAW,MAAM,SAAS,KAAK,EAAE;KAIpF,oBAAoB,KAAKA,EAAS,oBAAoB,QAAQA,EAAS;KAGvE,kBAAkB,cAAcA;;iBAGpB,cAAc,QAAQ,KAAKA,GAAU,kBAAkBA,GAAU;GAC/E,SAAS,yBAAyB,aAAa,oBAAoB;;;;;;cAuBhE,wBACO,YAAYA,EAAS,cAAc,QAAQ,mBAAmBA,EAAS,cAAc;;EAOhG,YAAmB,SAASA,EAAS,cAAc,KAAK;EAKjD,IAAI;EAKJ,IAAI,QAAQ;EAKZ,MAAM;EAMN;EAKA,OAAO;EAKD,KAAK,WAAW,iBAAc;EAI9B,QAAI;EAKV,SAAK;EAIC,eAAW,QAAA;EAIX,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,108 @@ 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
|
+
#env;
|
|
19
|
+
#isQuiet = false;
|
|
20
|
+
#isPrepared = false;
|
|
21
|
+
constructor(promise, env) {
|
|
22
|
+
this.#promise = promise;
|
|
23
|
+
this.#env = env;
|
|
24
|
+
}
|
|
25
|
+
cwd(newCwd) {
|
|
26
|
+
this.#promise.cwd(newCwd);
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
env(newEnv) {
|
|
30
|
+
this.#env = newEnv;
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
quiet(isQuiet = true) {
|
|
34
|
+
this.#promise.quiet(isQuiet);
|
|
35
|
+
this.#isQuiet = isQuiet;
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
nothrow() {
|
|
39
|
+
this.#promise.nothrow();
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
throws(shouldThrow) {
|
|
43
|
+
this.#promise.throws(shouldThrow);
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
async text(encoding) {
|
|
47
|
+
return this.#prepare(false).text(encoding);
|
|
48
|
+
}
|
|
49
|
+
async json() {
|
|
50
|
+
return this.#prepare(false).json();
|
|
51
|
+
}
|
|
52
|
+
lines() {
|
|
53
|
+
return this.#prepare(false).lines();
|
|
54
|
+
}
|
|
55
|
+
async arrayBuffer() {
|
|
56
|
+
return this.#prepare(false).arrayBuffer();
|
|
57
|
+
}
|
|
58
|
+
async blob() {
|
|
59
|
+
return this.#prepare(false).blob();
|
|
60
|
+
}
|
|
61
|
+
async then(onfulfilled, onrejected) {
|
|
62
|
+
return this.#prepare(!this.#isQuiet).then(onfulfilled, onrejected);
|
|
63
|
+
}
|
|
64
|
+
async catch(onrejected) {
|
|
65
|
+
return this.then(void 0, onrejected);
|
|
66
|
+
}
|
|
67
|
+
async finally(onfinally) {
|
|
68
|
+
return this.#prepare(!this.#isQuiet).finally(onfinally);
|
|
69
|
+
}
|
|
70
|
+
/** Sets the command's environment once, before Bun starts the command. */
|
|
71
|
+
#prepare(isShown) {
|
|
72
|
+
if (this.#isPrepared) return this.#promise;
|
|
73
|
+
this.#isPrepared = true;
|
|
74
|
+
const env = this.#env ?? process.env;
|
|
75
|
+
this.#promise.env(isShown && shouldForceColor(env) ? {
|
|
76
|
+
...env,
|
|
77
|
+
FORCE_COLOR: "1"
|
|
78
|
+
} : env);
|
|
79
|
+
return this.#promise;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
/** Creates a shell that prints each command to stderr before running it. */
|
|
83
|
+
const createShell = () => {
|
|
84
|
+
const bunShell = new bun.$.Shell();
|
|
85
|
+
let shellEnv;
|
|
86
|
+
const run = (strings, ...expressions) => {
|
|
87
|
+
const command = String.raw({ raw: strings.raw }, ...expressions.map(formatExpression));
|
|
88
|
+
process.stderr.write(`${styleText("bold", command.trim(), { stream: process.stderr })}\n`);
|
|
89
|
+
return new ShellCommand(bunShell(strings, ...expressions), shellEnv);
|
|
90
|
+
};
|
|
91
|
+
const shell = Object.assign(run, {
|
|
92
|
+
cwd: (newCwd) => {
|
|
93
|
+
bunShell.cwd(newCwd);
|
|
94
|
+
return shell;
|
|
95
|
+
},
|
|
96
|
+
env: (newEnv) => {
|
|
97
|
+
shellEnv = newEnv;
|
|
98
|
+
return shell;
|
|
99
|
+
},
|
|
100
|
+
nothrow: () => {
|
|
101
|
+
bunShell.nothrow();
|
|
102
|
+
return shell;
|
|
103
|
+
},
|
|
104
|
+
throws: (shouldThrow) => {
|
|
105
|
+
bunShell.throws(shouldThrow);
|
|
106
|
+
return shell;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
return shell;
|
|
15
110
|
};
|
|
16
111
|
//#endregion
|
|
17
|
-
export {
|
|
112
|
+
export { createShell };
|
|
18
113
|
|
|
19
114
|
//# 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/**
|
|
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":"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"}
|
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.2.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": "bun 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",
|