@adamhl8/bask 0.5.0 → 0.6.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.
Files changed (48) hide show
  1. package/README.md +29 -3
  2. package/dist/bask-file/bask-file.d.ts +3 -0
  3. package/dist/bask-file/bask-file.js +59 -0
  4. package/dist/bask-file/bask-file.js.map +1 -0
  5. package/dist/bask-file/list.d.ts +4 -0
  6. package/dist/bask-file/list.d.ts.map +1 -0
  7. package/dist/bask-file/list.js +32 -0
  8. package/dist/bask-file/list.js.map +1 -0
  9. package/dist/bask-file/loaded.js +18 -0
  10. package/dist/bask-file/loaded.js.map +1 -0
  11. package/dist/bask-file/task-index.d.ts +2 -0
  12. package/dist/bask-file/task-index.js +69 -0
  13. package/dist/bask-file/task-index.js.map +1 -0
  14. package/dist/cli.js +45 -78
  15. package/dist/cli.js.map +1 -1
  16. package/dist/config.d.ts +46 -0
  17. package/dist/config.d.ts.map +1 -0
  18. package/dist/config.js +26 -0
  19. package/dist/config.js.map +1 -0
  20. package/dist/index.d.ts +3 -2
  21. package/dist/index.js +4 -4
  22. package/dist/index.js.map +1 -1
  23. package/dist/shell/bin-dirs.js +15 -0
  24. package/dist/shell/bin-dirs.js.map +1 -0
  25. package/dist/shell/command.d.ts +57 -0
  26. package/dist/shell/command.d.ts.map +1 -0
  27. package/dist/shell/command.js +159 -0
  28. package/dist/shell/command.js.map +1 -0
  29. package/dist/shell/node-shim.js +47 -0
  30. package/dist/shell/node-shim.js.map +1 -0
  31. package/dist/shell/shell.d.ts +41 -0
  32. package/dist/shell/shell.d.ts.map +1 -0
  33. package/dist/shell/shell.js +55 -0
  34. package/dist/shell/shell.js.map +1 -0
  35. package/dist/task.d.ts +1 -3
  36. package/dist/task.d.ts.map +1 -1
  37. package/dist/task.js +15 -11
  38. package/dist/task.js.map +1 -1
  39. package/dist/utils.js +1 -2
  40. package/package.json +20 -11
  41. package/dist/registry.d.ts +0 -5
  42. package/dist/registry.d.ts.map +0 -1
  43. package/dist/registry.js +0 -96
  44. package/dist/registry.js.map +0 -1
  45. package/dist/shell.d.ts +0 -57
  46. package/dist/shell.d.ts.map +0 -1
  47. package/dist/shell.js +0 -142
  48. package/dist/shell.js.map +0 -1
@@ -0,0 +1,15 @@
1
+ import path from "node:path";
2
+ // src/shell/bin-dirs.ts
3
+ /** Lists `node_modules/.bin` for `dir` and each of its parents, nearest first. */
4
+ const getBinDirs = (dir) => {
5
+ const binDir = path.join(dir, "node_modules", ".bin");
6
+ const parent = path.dirname(dir);
7
+ return parent === dir ? [binDir] : [binDir, ...getBinDirs(parent)];
8
+ };
9
+ /** Prepends the project's `.bin` folders to the process `PATH`, like `bun run`, so anything a task starts finds them. */
10
+ const addBinDirsToPath = (projectDir) => {
11
+ process.env["PATH"] = [...getBinDirs(projectDir), process.env["PATH"]].filter(Boolean).join(path.delimiter);
12
+ };
13
+ export { addBinDirsToPath };
14
+
15
+ //# sourceMappingURL=bin-dirs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bin-dirs.js","names":[],"sources":["../../src/shell/bin-dirs.ts"],"sourcesContent":["import path from \"node:path\"\n\n/** Lists `node_modules/.bin` for `dir` and each of its parents, nearest first. */\nconst getBinDirs = (dir: string): string[] => {\n const binDir = path.join(dir, \"node_modules\", \".bin\")\n const parent = path.dirname(dir)\n return parent === dir ? [binDir] : [binDir, ...getBinDirs(parent)]\n}\n\n/** Prepends the project's `.bin` folders to the process `PATH`, like `bun run`, so anything a task starts finds them. */\nexport const addBinDirsToPath = (projectDir: string) => {\n process.env[\"PATH\"] = [...getBinDirs(projectDir), process.env[\"PATH\"]].filter(Boolean).join(path.delimiter)\n}\n"],"mappings":";;;AAGA,MAAM,cAAc,QAA0B;CAC5C,MAAM,SAAS,KAAK,KAAK,KAAK,gBAAgB,MAAM;CACpD,MAAM,SAAS,KAAK,QAAQ,GAAG;CAC/B,OAAO,WAAW,MAAM,CAAC,MAAM,IAAI,CAAC,QAAQ,GAAG,WAAW,MAAM,CAAC;AACnE;;AAGA,MAAa,oBAAoB,eAAuB;CACtD,QAAQ,IAAI,UAAU,CAAC,GAAG,WAAW,UAAU,GAAG,QAAQ,IAAI,OAAO,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,KAAK,KAAK,SAAS;AAC5G"}
@@ -0,0 +1,57 @@
1
+ import { ShellConfig } from "../config.js";
2
+ import { $ } from "bun";
3
+ export type ShellEnv = Record<string, string | undefined>;
4
+ /** Wraps Bun's `ShellPromise` to print the command and apply its settings when it runs. */
5
+ export declare class ShellCommand implements PromiseLike<$.ShellOutput> {
6
+ #private;
7
+ /** Starts from the shell's settings, which the command's own methods can then override. */
8
+ constructor(shellPromise: $.ShellPromise, commandString: string, shellConfig: ShellConfig);
9
+ /**
10
+ * Change the working directory for the command.
11
+ *
12
+ * @default process.cwd()
13
+ */
14
+ cwd(newCwd: string): this;
15
+ /** Add environment variables for the command, on top of the shell's. Set one to `undefined` to remove it. */
16
+ env(newEnv: ShellEnv): this;
17
+ /** Configure the command to only buffer its output instead of also writing it to stdout and stderr. */
18
+ quiet(isQuiet?: boolean): this;
19
+ /** Configure the command to not throw an exception on a non-zero exit code. */
20
+ nothrow(): this;
21
+ /**
22
+ * Configure whether the command throws an exception on a non-zero exit code.
23
+ *
24
+ * @default true
25
+ */
26
+ throws(shouldThrow: boolean): this;
27
+ /**
28
+ * Configure whether the command is printed before it runs.
29
+ *
30
+ * @default true
31
+ */
32
+ echo(shouldEcho: boolean): this;
33
+ /**
34
+ * Configure whether the command runs `node_modules` bins and `node` under Bun (like Bun's `--bun` flag).
35
+ *
36
+ * @default true
37
+ */
38
+ bun(shouldUseBun: boolean): this;
39
+ /** Read from stdout as a string. Automatically calls {@link quiet}. */
40
+ text(encoding?: BufferEncoding): Promise<string>;
41
+ /** Read from stdout as a JSON object. Automatically calls {@link quiet}. */
42
+ json(): Promise<any>;
43
+ /** Read from stdout as an ArrayBuffer. Automatically calls {@link quiet}. */
44
+ arrayBuffer(): Promise<ArrayBuffer>;
45
+ /** Read from stdout as a Blob. Automatically calls {@link quiet}. */
46
+ blob(): Promise<Blob>;
47
+ /**
48
+ * Read from stdout as a string, line by line. Automatically calls {@link quiet}.
49
+ *
50
+ * @yields {string} Each line of stdout.
51
+ */
52
+ lines(): AsyncGenerator<string, void, any>;
53
+ then<TResult1 = $.ShellOutput, TResult2 = never>(onfulfilled?: ((value: $.ShellOutput) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
54
+ catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null): Promise<TResult | $.ShellOutput>;
55
+ finally(onfinally?: (() => void) | null): Promise<this>;
56
+ }
57
+ //# sourceMappingURL=command.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command.d.ts","names":["BunShell"],"sources":["../../src/shell/command.ts"],"mappings":";;YAQY,WAAW;;qBAoBV,wBAAwB,YAAYA,EAAS;;;EAOxD,YAAmB,cAAcA,EAAS,cAAc,uBAAuB,aAAa;;;;;;EAYrF,IAAI;;EAMJ,IAAI,QAAQ;;EAMZ,MAAM;;EAMN;;;;;;EAUA,OAAO;;;;;;EAUP,KAAK;;;;;;EAUL,IAAI;;EAME,KAAK,WAAW,iBAAc;;EAO9B,QAAI;;EAQJ,eAAW,QAAA;;EAOX,QAAI,QAAA;;;;;;EAWH,SAAK;EAYN,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"}
@@ -0,0 +1,159 @@
1
+ import { addNodeShim, removeNodeShims } from "./node-shim.js";
2
+ import bun from "bun";
3
+ import { styleText } from "node:util";
4
+ // src/shell/command.ts
5
+ /**
6
+ * Checks whether bask should turn on color for a command whose output goes to the terminal. Bun's pipes hide the
7
+ * terminal from commands, so they turn color off unless forced.
8
+ */
9
+ const shouldForceColor = (env) => process.stdout.isTTY && process.stderr.isTTY && env["FORCE_COLOR"] === void 0 && env["NO_COLOR"] === void 0;
10
+ const errorsWithHiddenOutput = /* @__PURE__ */ new WeakSet();
11
+ /** Checks whether the command that failed with `error` hid its output, so the CLI can print it. */
12
+ const hasHiddenOutput = (error) => errorsWithHiddenOutput.has(error);
13
+ /** Records that a failed command hid its output, so the CLI can print it. */
14
+ const markHiddenOutput = (error) => {
15
+ if (error instanceof bun.$.ShellError) errorsWithHiddenOutput.add(error);
16
+ };
17
+ /** Wraps Bun's `ShellPromise` to print the command and apply its settings when it runs. */
18
+ var ShellCommand = class {
19
+ #shellPromise;
20
+ #commandString;
21
+ #settings;
22
+ #isPrepared = false;
23
+ /** Starts from the shell's settings, which the command's own methods can then override. */
24
+ constructor(shellPromise, commandString, shellConfig) {
25
+ this.#shellPromise = shellPromise;
26
+ this.#commandString = commandString;
27
+ this.#settings = { ...shellConfig };
28
+ }
29
+ /**
30
+ * Change the working directory for the command.
31
+ *
32
+ * @default process.cwd()
33
+ */
34
+ cwd(newCwd) {
35
+ this.#settings.cwd = newCwd;
36
+ return this;
37
+ }
38
+ /** Add environment variables for the command, on top of the shell's. Set one to `undefined` to remove it. */
39
+ env(newEnv) {
40
+ this.#settings.env = {
41
+ ...this.#settings.env,
42
+ ...newEnv
43
+ };
44
+ return this;
45
+ }
46
+ /** Configure the command to only buffer its output instead of also writing it to stdout and stderr. */
47
+ quiet(isQuiet = true) {
48
+ this.#settings.quiet = isQuiet;
49
+ return this;
50
+ }
51
+ /** Configure the command to not throw an exception on a non-zero exit code. */
52
+ nothrow() {
53
+ this.#settings.throws = false;
54
+ return this;
55
+ }
56
+ /**
57
+ * Configure whether the command throws an exception on a non-zero exit code.
58
+ *
59
+ * @default true
60
+ */
61
+ throws(shouldThrow) {
62
+ this.#settings.throws = shouldThrow;
63
+ return this;
64
+ }
65
+ /**
66
+ * Configure whether the command is printed before it runs.
67
+ *
68
+ * @default true
69
+ */
70
+ echo(shouldEcho) {
71
+ this.#settings.echo = shouldEcho;
72
+ return this;
73
+ }
74
+ /**
75
+ * Configure whether the command runs `node_modules` bins and `node` under Bun (like Bun's `--bun` flag).
76
+ *
77
+ * @default true
78
+ */
79
+ bun(shouldUseBun) {
80
+ this.#settings.bun = shouldUseBun;
81
+ return this;
82
+ }
83
+ /** Read from stdout as a string. Automatically calls {@link quiet}. */
84
+ async text(encoding) {
85
+ this.quiet();
86
+ return (await this.#run()).text(encoding);
87
+ }
88
+ /** Read from stdout as a JSON object. Automatically calls {@link quiet}. */
89
+ async json() {
90
+ this.quiet();
91
+ return (await this.#run()).json();
92
+ }
93
+ /** Read from stdout as an ArrayBuffer. Automatically calls {@link quiet}. */
94
+ async arrayBuffer() {
95
+ this.quiet();
96
+ return (await this.#run()).arrayBuffer();
97
+ }
98
+ /** Read from stdout as a Blob. Automatically calls {@link quiet}. */
99
+ async blob() {
100
+ this.quiet();
101
+ return (await this.#run()).blob();
102
+ }
103
+ /**
104
+ * Read from stdout as a string, line by line. Automatically calls {@link quiet}.
105
+ *
106
+ * @yields {string} Each line of stdout.
107
+ */
108
+ async *lines() {
109
+ this.quiet();
110
+ try {
111
+ yield* this.#prepare().lines();
112
+ } catch (error) {
113
+ markHiddenOutput(error);
114
+ throw error;
115
+ }
116
+ }
117
+ async then(onfulfilled, onrejected) {
118
+ return this.#run().then(onfulfilled, onrejected);
119
+ }
120
+ async catch(onrejected) {
121
+ return this.then(void 0, onrejected);
122
+ }
123
+ async finally(onfinally) {
124
+ try {
125
+ return await this;
126
+ } finally {
127
+ onfinally?.();
128
+ }
129
+ }
130
+ /** Runs the command, and records a failure whose output was hidden so the CLI can print it. */
131
+ async #run() {
132
+ try {
133
+ return await this.#prepare();
134
+ } catch (error) {
135
+ if (this.#settings.quiet) markHiddenOutput(error);
136
+ throw error;
137
+ }
138
+ }
139
+ /** Prints the command and applies its settings once, before Bun starts the command. */
140
+ #prepare() {
141
+ if (this.#isPrepared) return this.#shellPromise;
142
+ this.#isPrepared = true;
143
+ const { cwd, env: settingsEnv, quiet: isQuiet, throws: shouldThrow, echo: shouldEcho, bun: shouldUseBun } = this.#settings;
144
+ const env = shouldUseBun ? addNodeShim(settingsEnv) : removeNodeShims(settingsEnv);
145
+ const commandEnv = !isQuiet && shouldForceColor(env) ? {
146
+ ...env,
147
+ FORCE_COLOR: "1"
148
+ } : env;
149
+ this.#shellPromise.env(commandEnv);
150
+ if (isQuiet) this.#shellPromise.quiet();
151
+ if (cwd !== void 0) this.#shellPromise.cwd(cwd);
152
+ this.#shellPromise.throws(shouldThrow);
153
+ if (shouldEcho) process.stderr.write(`${styleText("bold", this.#commandString, { stream: process.stderr })}\n`);
154
+ return this.#shellPromise;
155
+ }
156
+ };
157
+ export { ShellCommand, hasHiddenOutput };
158
+
159
+ //# sourceMappingURL=command.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command.js","names":[],"sources":["../../src/shell/command.ts"],"sourcesContent":["import { styleText } from \"node:util\"\n\nimport bun from \"bun\"\nimport type { $ as BunShell } from \"bun\"\n\nimport type { ShellConfig } from \"#config.ts\"\nimport { addNodeShim, removeNodeShims } from \"#shell/node-shim.ts\"\n\nexport type ShellEnv = Record<string, string | undefined>\n\n/**\n * Checks whether bask should turn on color for a command whose output goes to the terminal. Bun's pipes hide the\n * terminal from commands, so they turn color off unless forced.\n */\nconst shouldForceColor = (env: ShellEnv) =>\n process.stdout.isTTY && process.stderr.isTTY && env[\"FORCE_COLOR\"] === undefined && env[\"NO_COLOR\"] === undefined\n\nconst errorsWithHiddenOutput = new WeakSet<BunShell.ShellError>()\n\n/** Checks whether the command that failed with `error` hid its output, so the CLI can print it. */\nexport const hasHiddenOutput = (error: BunShell.ShellError) => errorsWithHiddenOutput.has(error)\n\n/** Records that a failed command hid its output, so the CLI can print it. */\nconst markHiddenOutput = (error: unknown) => {\n if (error instanceof bun.$.ShellError) errorsWithHiddenOutput.add(error)\n}\n\n/** Wraps Bun's `ShellPromise` to print the command and apply its settings when it runs. */\nexport class ShellCommand implements PromiseLike<BunShell.ShellOutput> {\n readonly #shellPromise: BunShell.ShellPromise\n readonly #commandString: string\n readonly #settings: ShellConfig\n #isPrepared = false\n\n /** Starts from the shell's settings, which the command's own methods can then override. */\n public constructor(shellPromise: BunShell.ShellPromise, commandString: string, shellConfig: ShellConfig) {\n this.#shellPromise = shellPromise\n this.#commandString = commandString\n // Copy `shellConfig` so later changes to the shell don't reach this command.\n this.#settings = { ...shellConfig }\n }\n\n /**\n * Change the working directory for the command.\n *\n * @default process.cwd()\n */\n public cwd(newCwd: string) {\n this.#settings.cwd = newCwd\n return this\n }\n\n /** Add environment variables for the command, on top of the shell's. Set one to `undefined` to remove it. */\n public env(newEnv: ShellEnv) {\n this.#settings.env = { ...this.#settings.env, ...newEnv }\n return this\n }\n\n /** Configure the command to only buffer its output instead of also writing it to stdout and stderr. */\n public quiet(isQuiet = true) {\n this.#settings.quiet = isQuiet\n return this\n }\n\n /** Configure the command to not throw an exception on a non-zero exit code. */\n public nothrow() {\n this.#settings.throws = false\n return this\n }\n\n /**\n * Configure whether the command throws an exception on a non-zero exit code.\n *\n * @default true\n */\n public throws(shouldThrow: boolean) {\n this.#settings.throws = shouldThrow\n return this\n }\n\n /**\n * Configure whether the command is printed before it runs.\n *\n * @default true\n */\n public echo(shouldEcho: boolean) {\n this.#settings.echo = shouldEcho\n return this\n }\n\n /**\n * Configure whether the command runs `node_modules` bins and `node` under Bun (like Bun's `--bun` flag).\n *\n * @default true\n */\n public bun(shouldUseBun: boolean) {\n this.#settings.bun = shouldUseBun\n return this\n }\n\n /** Read from stdout as a string. Automatically calls {@link quiet}. */\n public async text(encoding?: BufferEncoding) {\n this.quiet()\n const output = await this.#run()\n return output.text(encoding)\n }\n\n /** Read from stdout as a JSON object. Automatically calls {@link quiet}. */\n public async json() {\n this.quiet()\n const output = await this.#run()\n // oxlint-disable-next-line typescript/no-unsafe-return - match Bun's `json()`, which returns `any`\n return output.json()\n }\n\n /** Read from stdout as an ArrayBuffer. Automatically calls {@link quiet}. */\n public async arrayBuffer() {\n this.quiet()\n const output = await this.#run()\n return output.arrayBuffer()\n }\n\n /** Read from stdout as a Blob. Automatically calls {@link quiet}. */\n public async blob() {\n this.quiet()\n const output = await this.#run()\n return output.blob()\n }\n\n /**\n * Read from stdout as a string, line by line. Automatically calls {@link quiet}.\n *\n * @yields {string} Each line of stdout.\n */\n public async *lines() {\n this.quiet()\n // Use Bun's `lines()` because `ShellOutput` has no line reader.\n try {\n yield* this.#prepare().lines()\n } catch (error) {\n markHiddenOutput(error)\n throw error\n }\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.#run().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 try {\n return await this\n } finally {\n onfinally?.()\n }\n }\n\n /** Runs the command, and records a failure whose output was hidden so the CLI can print it. */\n async #run() {\n try {\n return await this.#prepare()\n } catch (error) {\n if (this.#settings.quiet) markHiddenOutput(error)\n throw error\n }\n }\n\n /** Prints the command and applies its settings 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() {\n // Skip a command that already started, because Bun rejects settings on it.\n if (this.#isPrepared) return this.#shellPromise\n this.#isPrepared = true\n\n const {\n cwd,\n env: settingsEnv,\n quiet: isQuiet,\n throws: shouldThrow,\n echo: shouldEcho,\n bun: shouldUseBun,\n } = this.#settings\n\n const env = shouldUseBun ? addNodeShim(settingsEnv) : removeNodeShims(settingsEnv)\n const shouldAddColor = !isQuiet && shouldForceColor(env)\n const commandEnv = shouldAddColor ? { ...env, FORCE_COLOR: \"1\" } : env\n\n void this.#shellPromise.env(commandEnv)\n if (isQuiet) void this.#shellPromise.quiet()\n if (cwd !== undefined) void this.#shellPromise.cwd(cwd)\n void this.#shellPromise.throws(shouldThrow)\n\n // Write to `process.stderr` directly because Bun colors `console.error` output red in a terminal.\n if (shouldEcho) process.stderr.write(`${styleText(\"bold\", this.#commandString, { stream: process.stderr })}\\n`)\n\n return this.#shellPromise\n }\n}\n"],"mappings":"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"}
@@ -0,0 +1,47 @@
1
+ import path from "node:path";
2
+ import fs from "node:fs";
3
+ import os from "node:os";
4
+ // src/shell/node-shim.ts
5
+ let nodeShimDir;
6
+ /** Creates a temp folder whose `node` links to the running Bun, the same shim `bun run --bun` puts on `PATH`. */
7
+ const getNodeShimDir = () => {
8
+ if (nodeShimDir !== void 0) return nodeShimDir;
9
+ const tempShimDir = fs.mkdtempSync(path.join(os.tmpdir(), "bask-node-"));
10
+ fs.symlinkSync(process.execPath, path.join(tempShimDir, "node"));
11
+ process.once("exit", () => {
12
+ fs.rmSync(tempShimDir, {
13
+ recursive: true,
14
+ force: true
15
+ });
16
+ });
17
+ nodeShimDir = tempShimDir;
18
+ return nodeShimDir;
19
+ };
20
+ const NODE_SHIM_DIR = /^(?:bask|bun)-node-/v;
21
+ /** Checks for a `node` shim folder from bask or from `bun run`, including one a parent process put on `PATH`. */
22
+ const isNodeShimDir = (dir) => NODE_SHIM_DIR.test(path.basename(dir));
23
+ /** Lists the `PATH` dirs without any `node` shim, including Bun's own. */
24
+ const getPathDirsWithoutShims = (PATH) => PATH.split(path.delimiter).filter((dir) => !isNodeShimDir(dir));
25
+ /** Takes every `node` shim off `PATH` so `node` is the real Node. */
26
+ const removeNodeShims = (env) => {
27
+ const currentPath = env["PATH"];
28
+ if (currentPath === void 0) return env;
29
+ const PATH = getPathDirsWithoutShims(currentPath).join(path.delimiter);
30
+ return {
31
+ ...env,
32
+ PATH
33
+ };
34
+ };
35
+ /** Puts bask's `node` shim first on `PATH`, in place of any other `node` shim. */
36
+ const addNodeShim = (env) => {
37
+ const currentPath = env["PATH"];
38
+ if (currentPath === void 0) return env;
39
+ const PATH = [getNodeShimDir(), ...getPathDirsWithoutShims(currentPath)].join(path.delimiter);
40
+ return {
41
+ ...env,
42
+ PATH
43
+ };
44
+ };
45
+ export { addNodeShim, removeNodeShims };
46
+
47
+ //# sourceMappingURL=node-shim.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node-shim.js","names":[],"sources":["../../src/shell/node-shim.ts"],"sourcesContent":["import fs from \"node:fs\"\nimport os from \"node:os\"\nimport path from \"node:path\"\n\nimport type { ShellEnv } from \"#shell/command.ts\"\n\nlet nodeShimDir: string | undefined\n\n/** Creates a temp folder whose `node` links to the running Bun, the same shim `bun run --bun` puts on `PATH`. */\nconst getNodeShimDir = () => {\n if (nodeShimDir !== undefined) return nodeShimDir\n\n // Use a fresh folder because another user could plant a `node` in a shared folder with a predictable name.\n const tempShimDir = fs.mkdtempSync(path.join(os.tmpdir(), \"bask-node-\"))\n fs.symlinkSync(process.execPath, path.join(tempShimDir, \"node\"))\n process.once(\"exit\", () => {\n fs.rmSync(tempShimDir, { recursive: true, force: true })\n })\n\n nodeShimDir = tempShimDir\n return nodeShimDir\n}\n\nconst NODE_SHIM_DIR = /^(?:bask|bun)-node-/v\n\n/** Checks for a `node` shim folder from bask or from `bun run`, including one a parent process put on `PATH`. */\nconst isNodeShimDir = (dir: string) => NODE_SHIM_DIR.test(path.basename(dir))\n\n/** Lists the `PATH` dirs without any `node` shim, including Bun's own. */\nconst getPathDirsWithoutShims = (PATH: string) => PATH.split(path.delimiter).filter((dir) => !isNodeShimDir(dir))\n\n/** Takes every `node` shim off `PATH` so `node` is the real Node. */\nexport const removeNodeShims = (env: ShellEnv) => {\n const currentPath = env[\"PATH\"]\n if (currentPath === undefined) return env\n\n const PATH = getPathDirsWithoutShims(currentPath).join(path.delimiter)\n\n return { ...env, PATH }\n}\n\n/** Puts bask's `node` shim first on `PATH`, in place of any other `node` shim. */\nexport const addNodeShim = (env: ShellEnv) => {\n const currentPath = env[\"PATH\"]\n // Skip an env without `PATH` because adding one would change which commands it can find.\n if (currentPath === undefined) return env\n\n const PATH = [getNodeShimDir(), ...getPathDirsWithoutShims(currentPath)].join(path.delimiter)\n\n return { ...env, PATH }\n}\n"],"mappings":";;;;AAMA,IAAI;;AAGJ,MAAM,uBAAuB;CAC3B,IAAI,gBAAgB,KAAA,GAAW,OAAO;CAGtC,MAAM,cAAc,GAAG,YAAY,KAAK,KAAK,GAAG,OAAO,GAAG,YAAY,CAAC;CACvE,GAAG,YAAY,QAAQ,UAAU,KAAK,KAAK,aAAa,MAAM,CAAC;CAC/D,QAAQ,KAAK,cAAc;EACzB,GAAG,OAAO,aAAa;GAAE,WAAW;GAAM,OAAO;EAAK,CAAC;CACzD,CAAC;CAED,cAAc;CACd,OAAO;AACT;AAEA,MAAM,gBAAgB;;AAGtB,MAAM,iBAAiB,QAAgB,cAAc,KAAK,KAAK,SAAS,GAAG,CAAC;;AAG5E,MAAM,2BAA2B,SAAiB,KAAK,MAAM,KAAK,SAAS,CAAC,CAAC,QAAQ,QAAQ,CAAC,cAAc,GAAG,CAAC;;AAGhH,MAAa,mBAAmB,QAAkB;CAChD,MAAM,cAAc,IAAI;CACxB,IAAI,gBAAgB,KAAA,GAAW,OAAO;CAEtC,MAAM,OAAO,wBAAwB,WAAW,CAAC,CAAC,KAAK,KAAK,SAAS;CAErE,OAAO;EAAE,GAAG;EAAK;CAAK;AACxB;;AAGA,MAAa,eAAe,QAAkB;CAC5C,MAAM,cAAc,IAAI;CAExB,IAAI,gBAAgB,KAAA,GAAW,OAAO;CAEtC,MAAM,OAAO,CAAC,eAAe,GAAG,GAAG,wBAAwB,WAAW,CAAC,CAAC,CAAC,KAAK,KAAK,SAAS;CAE5F,OAAO;EAAE,GAAG;EAAK;CAAK;AACxB"}
@@ -0,0 +1,41 @@
1
+ import { ShellCommand, ShellEnv } from "./command.js";
2
+ import "../config.js";
3
+ import { ShellExpression } from "bun";
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
+ /**
8
+ * Change the default working directory for commands run by this shell.
9
+ *
10
+ * @default process.cwd()
11
+ */
12
+ cwd: (newCwd?: string) => Shell;
13
+ /**
14
+ * Add environment variables for commands run by this shell, on top of `process.env` as it was when `bask.ts` loaded
15
+ * and `baskConfig`. Set one to `undefined` to remove it. With no argument, go back to the `baskConfig` variables.
16
+ */
17
+ env: (newEnv?: ShellEnv) => Shell;
18
+ /** Configure commands run by this shell to only buffer their output instead of also writing it to stdout and stderr. */
19
+ quiet: (isQuiet?: boolean) => Shell;
20
+ /** Configure commands run by this shell to not throw an exception on non-zero exit codes. */
21
+ nothrow: () => Shell;
22
+ /**
23
+ * Configure whether commands run by this shell throw an exception on non-zero exit codes.
24
+ *
25
+ * @default true
26
+ */
27
+ throws: (shouldThrow: boolean) => Shell;
28
+ /**
29
+ * Configure whether this shell prints each command before running it.
30
+ *
31
+ * @default true
32
+ */
33
+ echo: (shouldEcho: boolean) => Shell;
34
+ /**
35
+ * Configure whether this shell runs `node_modules` bins and `node` under Bun (like Bun's `--bun` flag).
36
+ *
37
+ * @default true
38
+ */
39
+ bun: (shouldUseBun: boolean) => Shell;
40
+ }
41
+ //# sourceMappingURL=shell.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shell.d.ts","names":[],"sources":["../../src/shell/shell.ts"],"mappings":";;;;iBAQiB;GACd,SAAS,yBAAyB,aAAa,oBAAoB;;;;;;EAMpE,MAAM,oBAAoB;;;;;EAK1B,MAAM,SAAS,aAAa;;EAE5B,QAAQ,sBAAsB;;EAE9B,eAAe;;;;;;EAMf,SAAS,yBAAyB;;;;;;EAMlC,OAAO,wBAAwB;;;;;;EAM/B,MAAM,0BAA0B"}
@@ -0,0 +1,55 @@
1
+ import { ShellCommand } from "./command.js";
2
+ import bun from "bun";
3
+ // src/shell/shell.ts
4
+ /** Formats an interpolated value the way Bun's shell escapes it. */
5
+ const formatExpression = (expression) => {
6
+ if (Array.isArray(expression)) return expression.map(formatExpression).join(" ");
7
+ if (typeof expression === "object" && expression !== null && "raw" in expression) return expression.raw;
8
+ return bun.$.escape(String(expression));
9
+ };
10
+ /** Creates a shell that prints each command to stderr before running it, starting from `shellConfig`. */
11
+ const createShell = (shellConfig) => {
12
+ const bunShell = new bun.$.Shell();
13
+ const settings = { ...shellConfig };
14
+ const run = (strings, ...expressions) => {
15
+ const commandString = String.raw({ raw: strings.raw }, ...expressions.map(formatExpression)).trim();
16
+ return new ShellCommand(bunShell(strings, ...expressions), commandString, settings);
17
+ };
18
+ const shell = Object.assign(run, {
19
+ cwd: (newCwd) => {
20
+ settings.cwd = newCwd ?? shellConfig.cwd;
21
+ return shell;
22
+ },
23
+ env: (newEnv) => {
24
+ settings.env = newEnv === void 0 ? shellConfig.env : {
25
+ ...settings.env,
26
+ ...newEnv
27
+ };
28
+ return shell;
29
+ },
30
+ quiet: (isQuiet = true) => {
31
+ settings.quiet = isQuiet;
32
+ return shell;
33
+ },
34
+ nothrow: () => {
35
+ settings.throws = false;
36
+ return shell;
37
+ },
38
+ throws: (shouldThrow) => {
39
+ settings.throws = shouldThrow;
40
+ return shell;
41
+ },
42
+ echo: (shouldEcho) => {
43
+ settings.echo = shouldEcho;
44
+ return shell;
45
+ },
46
+ bun: (shouldUseBun) => {
47
+ settings.bun = shouldUseBun;
48
+ return shell;
49
+ }
50
+ });
51
+ return shell;
52
+ };
53
+ export { createShell };
54
+
55
+ //# sourceMappingURL=shell.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shell.js","names":[],"sources":["../../src/shell/shell.ts"],"sourcesContent":["import bun from \"bun\"\nimport type { ShellExpression } from \"bun\"\n\nimport type { ShellConfig } from \"#config.ts\"\nimport { ShellCommand } from \"#shell/command.ts\"\nimport type { ShellEnv } from \"#shell/command.ts\"\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 /**\n * Change the default working directory for commands run by this shell.\n *\n * @default process.cwd()\n */\n cwd: (newCwd?: string) => Shell\n /**\n * Add environment variables for commands run by this shell, on top of `process.env` as it was when `bask.ts` loaded\n * and `baskConfig`. Set one to `undefined` to remove it. With no argument, go back to the `baskConfig` variables.\n */\n env: (newEnv?: ShellEnv) => Shell\n /** Configure commands run by this shell to only buffer their output instead of also writing it to stdout and stderr. */\n quiet: (isQuiet?: boolean) => Shell\n /** Configure commands run by this shell to not throw an exception on non-zero exit codes. */\n nothrow: () => Shell\n /**\n * Configure whether commands run by this shell throw an exception on non-zero exit codes.\n *\n * @default true\n */\n throws: (shouldThrow: boolean) => Shell\n /**\n * Configure whether this shell prints each command before running it.\n *\n * @default true\n */\n echo: (shouldEcho: boolean) => Shell\n /**\n * Configure whether this shell runs `node_modules` bins and `node` under Bun (like Bun's `--bun` flag).\n *\n * @default true\n */\n bun: (shouldUseBun: 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 - their default string is what bask prints\n return bun.$.escape(String(expression))\n}\n\n/** Creates a shell that prints each command to stderr before running it, starting from `shellConfig`. */\nexport const createShell = (shellConfig: ShellConfig) => {\n // Use a separate instance so settings on Bun's global `$` don't apply here.\n const bunShell = new bun.$.Shell()\n // Copy `shellConfig` so this shell's changes don't reach other shells.\n const settings: ShellConfig = { ...shellConfig }\n\n const run = (strings: TemplateStringsArray, ...expressions: ShellExpression[]) => {\n // Use `String.raw` because Bun's `$` also reads the raw template strings.\n const commandString = String.raw({ raw: strings.raw }, ...expressions.map(formatExpression)).trim()\n return new ShellCommand(bunShell(strings, ...expressions), commandString, settings)\n }\n\n const shell: Shell = Object.assign(run, {\n cwd: (newCwd?: string) => {\n settings.cwd = newCwd ?? shellConfig.cwd\n return shell\n },\n env: (newEnv?: ShellEnv) => {\n settings.env = newEnv === undefined ? shellConfig.env : { ...settings.env, ...newEnv }\n return shell\n },\n quiet: (isQuiet = true) => {\n settings.quiet = isQuiet\n return shell\n },\n nothrow: () => {\n settings.throws = false\n return shell\n },\n throws: (shouldThrow: boolean) => {\n settings.throws = shouldThrow\n return shell\n },\n echo: (shouldEcho: boolean) => {\n settings.echo = shouldEcho\n return shell\n },\n bun: (shouldUseBun: boolean) => {\n settings.bun = shouldUseBun\n return shell\n },\n })\n\n return shell\n}\n"],"mappings":";;;;AA8CA,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,MAAa,eAAe,gBAA6B;CAEvD,MAAM,WAAW,IAAI,IAAI,EAAE,MAAM;CAEjC,MAAM,WAAwB,EAAE,GAAG,YAAY;CAE/C,MAAM,OAAO,SAA+B,GAAG,gBAAmC;EAEhF,MAAM,gBAAgB,OAAO,IAAI,EAAE,KAAK,QAAQ,IAAI,GAAG,GAAG,YAAY,IAAI,gBAAgB,CAAC,CAAC,CAAC,KAAK;EAClG,OAAO,IAAI,aAAa,SAAS,SAAS,GAAG,WAAW,GAAG,eAAe,QAAQ;CACpF;CAEA,MAAM,QAAe,OAAO,OAAO,KAAK;EACtC,MAAM,WAAoB;GACxB,SAAS,MAAM,UAAU,YAAY;GACrC,OAAO;EACT;EACA,MAAM,WAAsB;GAC1B,SAAS,MAAM,WAAW,KAAA,IAAY,YAAY,MAAM;IAAE,GAAG,SAAS;IAAK,GAAG;GAAO;GACrF,OAAO;EACT;EACA,QAAQ,UAAU,SAAS;GACzB,SAAS,QAAQ;GACjB,OAAO;EACT;EACA,eAAe;GACb,SAAS,SAAS;GAClB,OAAO;EACT;EACA,SAAS,gBAAyB;GAChC,SAAS,SAAS;GAClB,OAAO;EACT;EACA,OAAO,eAAwB;GAC7B,SAAS,OAAO;GAChB,OAAO;EACT;EACA,MAAM,iBAA0B;GAC9B,SAAS,MAAM;GACf,OAAO;EACT;CACF,CAAC;CAED,OAAO;AACT"}
package/dist/task.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { Shell } from "./shell.js";
2
- //#region src/task.d.ts
1
+ import { Shell } from "./shell/shell.js";
3
2
  type AsyncOrSyncReturn = Promise<void> | void;
4
3
  interface TaskOptions {
5
4
  desc?: string;
@@ -22,6 +21,5 @@ export declare const isTask: (value: unknown) => value is Task;
22
21
  /** Registers a task that runs at most once per bask invocation, however many times it's called. */
23
22
  export declare function task(fn: TaskFn): Task;
24
23
  export declare function task(options: TaskOptions, fn: TaskFn): Task;
25
- //#endregion
26
24
  export {};
27
25
  //# sourceMappingURL=task.d.ts.map
@@ -1 +1 @@
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"}
1
+ {"version":3,"file":"task.d.ts","names":[],"sources":["../src/task.ts"],"mappings":";KAKK,oBAAoB;UAEf;EACR;;EAEA;;EAEA;;UAGQ;EACR,IAAI;;KAGD,UAAU,KAAK,gBAAgB;iBAEnB;MACX;EACJ,SAAS;EACT,YAAY,KAAK;EACjB,WAAW,KAAK;;qBAIL,SAAM,mBAAqB,SAAS;;wBAOjC,KAAK,IAAI,SAAS;wBAClB,KAAK,SAAS,aAAa,IAAI,SAAS"}
package/dist/task.js CHANGED
@@ -1,7 +1,8 @@
1
- import { createShell } from "./shell.js";
2
- //#region src/task.ts
3
- const tasks = /* @__PURE__ */ new WeakSet();
4
- const isTask = (value) => typeof value === "function" && tasks.has(value);
1
+ import { getLoadedBaskFile } from "./bask-file/loaded.js";
2
+ import { createShell } from "./shell/shell.js";
3
+ // src/task.ts
4
+ const createdByTask = /* @__PURE__ */ new WeakSet();
5
+ const isTask = (value) => typeof value === "function" && createdByTask.has(value);
5
6
  const failedTasks = /* @__PURE__ */ new WeakMap();
6
7
  /** Returns the innermost task that the error failed, so the CLI can name it. */
7
8
  const getFailedTask = (error) => failedTasks.get(error);
@@ -23,31 +24,34 @@ function task(optionsOrFn, maybeFn) {
23
24
  assertNotStarted();
24
25
  afterFns.push(...fns);
25
26
  };
26
- const runWithShell = async (taskFn) => taskFn({ sh: createShell() });
27
- const runAll = async () => {
27
+ const runAll = async (shellConfig) => {
28
28
  hasStarted = true;
29
+ const runWithShell = async (taskFn) => taskFn({ sh: createShell(shellConfig) });
29
30
  for (const beforeFn of beforeFns) await runWithShell(beforeFn);
30
31
  await runWithShell(fn);
31
32
  for (const afterFn of afterFns) await runWithShell(afterFn);
32
33
  };
33
- const run = async () => {
34
- runPromise ??= runAll();
34
+ const runOnce = async (shellConfig) => {
35
+ runPromise ??= runAll(shellConfig);
35
36
  try {
36
37
  await runPromise;
37
38
  } catch (error) {
38
- if (error instanceof Error && !failedTasks.has(error)) failedTasks.set(error, run);
39
+ if (error instanceof Error && !failedTasks.has(error)) failedTasks.set(error, newTask);
39
40
  throw error;
40
41
  }
41
42
  };
43
+ const run = () => {
44
+ const baskFile = getLoadedBaskFile();
45
+ return runOnce(baskFile.config.shell);
46
+ };
42
47
  const newTask = Object.assign(run, {
43
48
  options,
44
49
  before,
45
50
  after
46
51
  });
47
- tasks.add(newTask);
52
+ createdByTask.add(newTask);
48
53
  return newTask;
49
54
  }
50
- //#endregion
51
55
  export { getFailedTask, isTask, task };
52
56
 
53
57
  //# sourceMappingURL=task.js.map
package/dist/task.js.map CHANGED
@@ -1 +1 @@
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 // Track the start separately because `runAll` calls the task before `runPromise` is assigned.\n let hasStarted = false\n\n const assertNotStarted = () => {\n if (hasStarted) 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 hasStarted = true\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,IAAI,aAAa;CAEjB,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;EACzB,aAAa;EAEb,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"}
1
+ {"version":3,"file":"task.js","names":[],"sources":["../src/task.ts"],"sourcesContent":["import { getLoadedBaskFile } from \"#bask-file/loaded.ts\"\nimport type { ShellConfig } from \"#config.ts\"\nimport { createShell } from \"#shell/shell.ts\"\nimport type { Shell } from \"#shell/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 createdByTask = new WeakSet<object>()\nexport const isTask = (value: unknown): value is Task => typeof value === \"function\" && createdByTask.has(value)\n\nconst failedTasks = new WeakMap<Error, Task>()\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 // Track the start separately because `runAll` calls the task before `runPromise` is assigned.\n let hasStarted = false\n\n const assertNotStarted = () => {\n if (hasStarted) 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 const runAll = async (shellConfig: ShellConfig) => {\n hasStarted = true\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(shellConfig) })\n\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 runOnce = async (shellConfig: ShellConfig) => {\n runPromise ??= runAll(shellConfig)\n try {\n await runPromise\n } catch (error) {\n // Record this task only if no task has yet, because the innermost task's catch runs first.\n // oxlint-disable-next-line no-use-before-define - `runOnce` only runs after `newTask` is assigned\n if (error instanceof Error && !failedTasks.has(error)) failedTasks.set(error, newTask)\n throw error\n }\n }\n\n // oxlint-disable-next-line typescript/promise-function-async - an async function would turn the throw into a rejection\n const run = () => {\n const baskFile = getLoadedBaskFile()\n return runOnce(baskFile.config.shell)\n }\n\n const newTask: Task = Object.assign(run, { options, before, after })\n createdByTask.add(newTask)\n return newTask\n}\n"],"mappings":";;;AA4BA,MAAM,gCAAgB,IAAI,QAAgB;AAC1C,MAAa,UAAU,UAAkC,OAAO,UAAU,cAAc,cAAc,IAAI,KAAK;AAE/G,MAAM,8BAAc,IAAI,QAAqB;;AAE7C,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,IAAI,aAAa;CAEjB,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;CAEA,MAAM,SAAS,OAAO,gBAA6B;EACjD,aAAa;EAEb,MAAM,eAAe,OAAO,WAAmB,OAAO,EAAE,IAAI,YAAY,WAAW,EAAE,CAAC;EAGtF,KAAK,MAAM,YAAY,WAAW,MAAM,aAAa,QAAQ;EAC7D,MAAM,aAAa,EAAE;EAErB,KAAK,MAAM,WAAW,UAAU,MAAM,aAAa,OAAO;CAC5D;CAGA,MAAM,UAAU,OAAO,gBAA6B;EAClD,eAAe,OAAO,WAAW;EACjC,IAAI;GACF,MAAM;EACR,SAAS,OAAO;GAGd,IAAI,iBAAiB,SAAS,CAAC,YAAY,IAAI,KAAK,GAAG,YAAY,IAAI,OAAO,OAAO;GACrF,MAAM;EACR;CACF;CAGA,MAAM,YAAY;EAChB,MAAM,WAAW,kBAAkB;EACnC,OAAO,QAAQ,SAAS,OAAO,KAAK;CACtC;CAEA,MAAM,UAAgB,OAAO,OAAO,KAAK;EAAE;EAAS;EAAQ;CAAM,CAAC;CACnE,cAAc,IAAI,OAAO;CACzB,OAAO;AACT"}
package/dist/utils.js CHANGED
@@ -1,9 +1,8 @@
1
1
  import { styleText } from "node:util";
2
- //#region src/utils.ts
2
+ // src/utils.ts
3
3
  const printError = (message) => {
4
4
  process.stderr.write(`${styleText(["bold", "red"], "error:", { stream: process.stderr })} ${message}\n`);
5
5
  };
6
- //#endregion
7
6
  export { printError };
8
7
 
9
8
  //# sourceMappingURL=utils.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adamhl8/bask",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Write your tasks in TypeScript",
5
5
  "keywords": [
6
6
  "build-tool",
@@ -48,25 +48,34 @@
48
48
  "access": "public"
49
49
  },
50
50
  "scripts": {
51
- "prepare": "tsdown && bun install --ignore-scripts --no-summary && bunx bask prepare"
51
+ "prepare": "tsdown && bun install --ignore-scripts --no-summary && bask prepare"
52
+ },
53
+ "dependencies": {
54
+ "ts-explicit-errors": "^5.0.0"
52
55
  },
53
56
  "devDependencies": {
54
57
  "@adamhl8/bask": "file:.",
55
- "@adamhl8/configs": "^3.0.3",
58
+ "@adamhl8/configs": "3.4.0",
56
59
  "@arethetypeswrong/core": "^0.18.5",
57
60
  "@commitlint/cli": "^21.2.4",
58
61
  "@commitlint/config-conventional": "^21.2.4",
59
- "@types/bun": "^1.4.3",
60
- "execa": "^10.2.0",
61
- "knip": "^6.42.0",
62
+ "@types/bun": "^1.4.4",
63
+ "@vitest/istanbul-lib-coverage": "^2.0.0",
64
+ "@vitest/istanbul-lib-instrument": "^2.0.0",
65
+ "@vitest/istanbul-lib-report": "^2.0.0",
66
+ "@vitest/istanbul-lib-source-maps": "^2.0.0",
67
+ "knip": "^6.43.0",
62
68
  "markdown-toc": "^1.2.0",
63
- "oxfmt": "^0.73.0",
64
- "oxlint": "^1.88.0",
65
- "oxlint-tsgolint": "^7.0.2004",
69
+ "oxfmt": "^0.74.0",
70
+ "oxlint": "^1.89.0",
71
+ "oxlint-tsgolint": "^7.0.2005",
66
72
  "publint": "^0.3.25",
67
73
  "release-it": "^21.1.1",
68
74
  "release-it-git-cliff": "^0.2.0",
69
- "tsdown": "^0.23.1",
70
- "typescript": "^7.0.2"
75
+ "tsdown": "^0.23.2",
76
+ "typescript": "7.1.0-beta"
77
+ },
78
+ "engines": {
79
+ "bun": ">=1.4"
71
80
  }
72
81
  }
@@ -1,5 +0,0 @@
1
- import { Task } from "./task.js";
2
- //#region src/registry.d.ts
3
- export declare const list: Task;
4
- //#endregion
5
- //# sourceMappingURL=registry.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"registry.d.ts","names":[],"sources":["../src/registry.ts"],"mappings":";;qBAgIa,MAAI"}