@adamhl8/bask 0.5.0 → 0.6.1

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 (46) 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.js +32 -0
  7. package/dist/bask-file/list.js.map +1 -0
  8. package/dist/bask-file/loaded.js +18 -0
  9. package/dist/bask-file/loaded.js.map +1 -0
  10. package/dist/bask-file/task-index.d.ts +2 -0
  11. package/dist/bask-file/task-index.js +69 -0
  12. package/dist/bask-file/task-index.js.map +1 -0
  13. package/dist/cli.js +15 -83
  14. package/dist/cli.js.map +1 -1
  15. package/dist/config.d.ts +46 -0
  16. package/dist/config.js +26 -0
  17. package/dist/config.js.map +1 -0
  18. package/dist/index.d.ts +3 -2
  19. package/dist/index.js +4 -4
  20. package/dist/index.js.map +1 -1
  21. package/dist/run-bask.js +47 -0
  22. package/dist/run-bask.js.map +1 -0
  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.js +159 -0
  27. package/dist/shell/command.js.map +1 -0
  28. package/dist/shell/node-shim.js +47 -0
  29. package/dist/shell/node-shim.js.map +1 -0
  30. package/dist/shell/shell.d.ts +41 -0
  31. package/dist/shell/shell.js +55 -0
  32. package/dist/shell/shell.js.map +1 -0
  33. package/dist/task.d.ts +1 -3
  34. package/dist/task.js +15 -11
  35. package/dist/task.js.map +1 -1
  36. package/dist/utils.js +1 -2
  37. package/package.json +18 -13
  38. package/dist/registry.d.ts +0 -5
  39. package/dist/registry.d.ts.map +0 -1
  40. package/dist/registry.js +0 -96
  41. package/dist/registry.js.map +0 -1
  42. package/dist/shell.d.ts +0 -57
  43. package/dist/shell.d.ts.map +0 -1
  44. package/dist/shell.js +0 -142
  45. package/dist/shell.js.map +0 -1
  46. package/dist/task.d.ts.map +0 -1
package/dist/registry.js DELETED
@@ -1,96 +0,0 @@
1
- import { isTask, task } from "./task.js";
2
- //#region src/registry.ts
3
- /** Reports a mistake in `bask.ts` that the CLI prints without a stack trace. */
4
- var BaskFileError = class extends Error {
5
- name = "BaskFileError";
6
- };
7
- let taskIndex;
8
- const toKebabCase = (name) => name.replaceAll(/[A-Z]/gv, (match) => `-${match.toLowerCase()}`);
9
- const toCamelCase = (name) => name.replaceAll(/-[a-z\d]/gv, (match) => match.slice(1).toUpperCase());
10
- /** Formats one problem as a single line, or several under a count with one per line. */
11
- const formatProblems = (problems) => {
12
- if (problems.length === 1) return problems.join("\n");
13
- return [`bask.ts has ${problems.length} problems:`, ...problems.map((problem) => ` ${problem}`)].join("\n");
14
- };
15
- /** Indexes the loaded `bask.ts` module's tasks under both spellings of their export names and aliases. */
16
- const setBaskModule = (baskModule) => {
17
- const defaultTask = baskModule["default"];
18
- const problems = [];
19
- if ("default" in baskModule && !isTask(defaultTask)) problems.push("the default export isn't a task. Wrap it in task().");
20
- const taskExports = Object.entries(baskModule).filter((entry) => entry[0] !== "default" && isTask(entry[1]));
21
- const defaultHasName = taskExports.some(([, exportedTask]) => exportedTask === defaultTask);
22
- if (isTask(defaultTask) && !defaultHasName) taskExports.push(["default", defaultTask]);
23
- const index = /* @__PURE__ */ new Map();
24
- for (const [exportName, exportedTask] of taskExports) {
25
- const existingEntry = index.values().find((entry) => entry.task === exportedTask);
26
- if (existingEntry) {
27
- problems.push(`task '${exportName}' is also exported as '${existingEntry.exportName}'. Use the alias option instead.`);
28
- continue;
29
- }
30
- const kebabName = toKebabCase(exportName);
31
- const entry = {
32
- exportName,
33
- name: kebabName,
34
- aliases: [],
35
- task: exportedTask
36
- };
37
- index.set(exportName, entry);
38
- index.set(kebabName, entry);
39
- }
40
- for (const entry of new Set(index.values())) {
41
- const aliases = [entry.task.options.alias ?? []].flat();
42
- for (const alias of aliases) {
43
- const kebabAlias = toKebabCase(alias);
44
- if (index.get(kebabAlias) === entry) continue;
45
- const keys = [
46
- alias,
47
- kebabAlias,
48
- toCamelCase(alias)
49
- ];
50
- const owner = keys.map((key) => index.get(key)).find(Boolean);
51
- if (kebabAlias === "default") problems.push(`task '${entry.exportName}' can't use 'default' as an alias. Export it as the default instead.`);
52
- else if (owner) problems.push(`task '${entry.exportName}' has alias '${alias}', which task '${owner.exportName}' already uses`);
53
- else {
54
- entry.aliases.push(kebabAlias);
55
- for (const key of keys) index.set(key, entry);
56
- }
57
- }
58
- }
59
- if (problems.length > 0) throw new BaskFileError(formatProblems(problems));
60
- const defaultEntry = index.values().find((entry) => entry.task === defaultTask);
61
- if (defaultEntry) index.set("default", defaultEntry);
62
- taskIndex = index;
63
- };
64
- const getTaskIndex = () => {
65
- if (!taskIndex) throw new Error("Tasks were read before the CLI loaded bask.ts");
66
- return taskIndex;
67
- };
68
- const formatAliases = (aliases) => {
69
- if (aliases.length === 0) return;
70
- return `[${aliases.length === 1 ? "alias" : "aliases"}: ${aliases.join(", ")}]`;
71
- };
72
- const formatTaskList = () => {
73
- const index = getTaskIndex();
74
- const defaultEntry = index.get("default");
75
- const rows = [...new Set(index.values())].filter((entry) => !entry.task.options.hidden).map((entry) => {
76
- const defaultMarker = entry === defaultEntry && entry.name !== "default" ? "[default]" : void 0;
77
- const comment = [
78
- entry.task.options.desc,
79
- defaultMarker,
80
- formatAliases(entry.aliases)
81
- ].filter(Boolean).join(" ");
82
- return {
83
- name: entry.name,
84
- comment
85
- };
86
- }).toSorted((a, b) => a.name.localeCompare(b.name));
87
- const nameWidth = Math.max(...rows.map((row) => row.name.length));
88
- return ["Available tasks:", ...rows.map(({ name, comment }) => comment ? ` ${name.padEnd(nameWidth)} # ${comment}` : ` ${name}`)].join("\n");
89
- };
90
- const list = task(() => {
91
- console.log(formatTaskList());
92
- });
93
- //#endregion
94
- export { BaskFileError, formatTaskList, getTaskIndex, list, setBaskModule };
95
-
96
- //# sourceMappingURL=registry.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"registry.js","names":[],"sources":["../src/registry.ts"],"sourcesContent":["import { isTask, task } from \"#task.ts\"\nimport type { Task } from \"#task.ts\"\n\ninterface TaskEntry {\n /** Export name as written in `bask.ts`, for error messages. */\n exportName: string\n /** Kebab-case export name, or `default` for a task that's only the default export. */\n name: string\n /** Kebab-case aliases from the task's `alias` option. */\n aliases: string[]\n task: Task\n}\n\ntype TaskIndex = Map<string, TaskEntry>\n\n/** Reports a mistake in `bask.ts` that the CLI prints without a stack trace. */\nexport class BaskFileError extends Error {\n public override name = \"BaskFileError\"\n}\n\nlet taskIndex: TaskIndex | undefined\n\nconst toKebabCase = (name: string) => name.replaceAll(/[A-Z]/gv, (match) => `-${match.toLowerCase()}`)\nconst toCamelCase = (name: string) => name.replaceAll(/-[a-z\\d]/gv, (match) => match.slice(1).toUpperCase())\n\n/** Formats one problem as a single line, or several under a count with one per line. */\nconst formatProblems = (problems: string[]) => {\n if (problems.length === 1) return problems.join(\"\\n\")\n return [`bask.ts has ${problems.length} problems:`, ...problems.map((problem) => ` ${problem}`)].join(\"\\n\")\n}\n\n/** Indexes the loaded `bask.ts` module's tasks under both spellings of their export names and aliases. */\nexport const setBaskModule = (baskModule: Record<string, unknown>) => {\n const defaultTask = baskModule[\"default\"]\n // Collect every problem before throwing, so one run reports all of them.\n const problems: string[] = []\n\n // Reject a non-task default because bask runs it when no task is named, so it's always a mistake.\n if (\"default\" in baskModule && !isTask(defaultTask))\n problems.push(\"the default export isn't a task. Wrap it in task().\")\n\n // Collect named task exports, then add the default under `default` only when it has no other name.\n const taskExports = Object.entries(baskModule).filter(\n (entry): entry is [string, Task] => entry[0] !== \"default\" && isTask(entry[1]),\n )\n const defaultHasName = taskExports.some(([, exportedTask]) => exportedTask === defaultTask)\n if (isTask(defaultTask) && !defaultHasName) taskExports.push([\"default\", defaultTask])\n\n // Index each task under its export name and kebab-case name, collecting tasks exported more than once.\n const index: TaskIndex = new Map()\n for (const [exportName, exportedTask] of taskExports) {\n const existingEntry = index.values().find((entry) => entry.task === exportedTask)\n if (existingEntry) {\n problems.push(\n `task '${exportName}' is also exported as '${existingEntry.exportName}'. Use the alias option instead.`,\n )\n continue\n }\n\n const kebabName = toKebabCase(exportName)\n const entry: TaskEntry = { exportName, name: kebabName, aliases: [], task: exportedTask }\n index.set(exportName, entry)\n index.set(kebabName, entry)\n }\n\n // Index each alias under every spelling, collecting aliases that are `default` or already taken.\n // Every export name is already indexed, so a clash with a name is reported against the alias.\n // Collect the entries into a `Set` first because the loop adds alias keys to `index`.\n for (const entry of new Set(index.values())) {\n const aliases = [entry.task.options.alias ?? []].flat()\n\n for (const alias of aliases) {\n const kebabAlias = toKebabCase(alias)\n // Skip an alias that repeats the task's own name or another of its aliases.\n if (index.get(kebabAlias) === entry) continue\n\n const keys = [alias, kebabAlias, toCamelCase(alias)]\n const owner = keys.map((key) => index.get(key)).find(Boolean)\n if (kebabAlias === \"default\")\n problems.push(`task '${entry.exportName}' can't use 'default' as an alias. Export it as the default instead.`)\n else if (owner)\n problems.push(`task '${entry.exportName}' has alias '${alias}', which task '${owner.exportName}' already uses`)\n else {\n entry.aliases.push(kebabAlias)\n for (const key of keys) index.set(key, entry)\n }\n }\n }\n\n if (problems.length > 0) throw new BaskFileError(formatProblems(problems))\n\n const defaultEntry = index.values().find((entry) => entry.task === defaultTask)\n if (defaultEntry) index.set(\"default\", defaultEntry)\n\n taskIndex = index\n}\n\nexport const getTaskIndex = () => {\n if (!taskIndex) throw new Error(\"Tasks were read before the CLI loaded bask.ts\")\n return taskIndex\n}\n\nconst formatAliases = (aliases: string[]) => {\n if (aliases.length === 0) return\n return `[${aliases.length === 1 ? \"alias\" : \"aliases\"}: ${aliases.join(\", \")}]`\n}\n\nexport const formatTaskList = () => {\n const index = getTaskIndex()\n const defaultEntry = index.get(\"default\")\n\n // Collect the entries into a `Set` because every name of a task shares one entry.\n const rows = [...new Set(index.values())]\n .filter((entry) => !entry.task.options.hidden)\n .map((entry) => {\n const defaultMarker = entry === defaultEntry && entry.name !== \"default\" ? \"[default]\" : undefined\n const comment = [entry.task.options.desc, defaultMarker, formatAliases(entry.aliases)].filter(Boolean).join(\" \")\n return { name: entry.name, comment }\n })\n .toSorted((a, b) => a.name.localeCompare(b.name))\n\n const nameWidth = Math.max(...rows.map((row) => row.name.length))\n const lines = rows.map(({ name, comment }) =>\n comment ? ` ${name.padEnd(nameWidth)} # ${comment}` : ` ${name}`,\n )\n return [\"Available tasks:\", ...lines].join(\"\\n\")\n}\n\nexport const list = task(() => {\n console.log(formatTaskList())\n})\n"],"mappings":";;;AAgBA,IAAa,gBAAb,cAAmC,MAAM;CACvC,OAAuB;AACzB;AAEA,IAAI;AAEJ,MAAM,eAAe,SAAiB,KAAK,WAAW,YAAY,UAAU,IAAI,MAAM,YAAY,GAAG;AACrG,MAAM,eAAe,SAAiB,KAAK,WAAW,eAAe,UAAU,MAAM,MAAM,CAAC,CAAC,CAAC,YAAY,CAAC;;AAG3G,MAAM,kBAAkB,aAAuB;CAC7C,IAAI,SAAS,WAAW,GAAG,OAAO,SAAS,KAAK,IAAI;CACpD,OAAO,CAAC,eAAe,SAAS,OAAO,aAAa,GAAG,SAAS,KAAK,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,IAAI;AAC7G;;AAGA,MAAa,iBAAiB,eAAwC;CACpE,MAAM,cAAc,WAAW;CAE/B,MAAM,WAAqB,CAAC;CAG5B,IAAI,aAAa,cAAc,CAAC,OAAO,WAAW,GAChD,SAAS,KAAK,qDAAqD;CAGrE,MAAM,cAAc,OAAO,QAAQ,UAAU,CAAC,CAAC,QAC5C,UAAmC,MAAM,OAAO,aAAa,OAAO,MAAM,EAAE,CAC/E;CACA,MAAM,iBAAiB,YAAY,MAAM,GAAG,kBAAkB,iBAAiB,WAAW;CAC1F,IAAI,OAAO,WAAW,KAAK,CAAC,gBAAgB,YAAY,KAAK,CAAC,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ellEnv = 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
- }
55
- //#endregion
56
- export {};
57
- //# sourceMappingURL=shell.d.ts.map
@@ -1 +0,0 @@
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 DELETED
@@ -1,142 +0,0 @@
1
- import bun from "bun";
2
- import { styleText } from "node:util";
3
- //#region src/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
- /** 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;
138
- };
139
- //#endregion
140
- export { createShell };
141
-
142
- //# sourceMappingURL=shell.js.map
package/dist/shell.js.map DELETED
@@ -1 +0,0 @@
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,SAA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