@adamhl8/bask 0.3.0 → 0.5.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 +252 -51
- package/dist/cli.js +18 -8
- package/dist/cli.js.map +1 -1
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/shell.js +1 -1
- package/dist/task.js +3 -1
- package/dist/task.js.map +1 -1
- package/dist/utils.js +9 -0
- package/dist/utils.js.map +1 -0
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -1,28 +1,49 @@
|
|
|
1
|
-
|
|
1
|
+
<h1 align="center"><img align="top" style="color:#36BCF7; width:38px; height:38px;" src="https://raw.githubusercontent.com/adamhl8/bask/refs/heads/main/assets/logo.svg"> bask</h1>
|
|
2
2
|
|
|
3
|
-
A task runner
|
|
3
|
+
A task runner where your tasks are TypeScript functions.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
---
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
<!-- toc -->
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
- [Installation](#installation)
|
|
10
|
+
- [Usage](#usage)
|
|
11
|
+
- [Tasks](#tasks)
|
|
12
|
+
- [Defining tasks](#defining-tasks)
|
|
13
|
+
- [Task options](#task-options)
|
|
14
|
+
- [Task naming](#task-naming)
|
|
15
|
+
- [The default task](#the-default-task)
|
|
16
|
+
- [Task dependencies](#task-dependencies)
|
|
17
|
+
- [Task hooks](#task-hooks)
|
|
18
|
+
- [Running commands](#running-commands)
|
|
19
|
+
- [Shell settings](#shell-settings)
|
|
20
|
+
- [Extending/sharing tasks](#extendingsharing-tasks)
|
|
21
|
+
- [CLI Reference](#cli-reference)
|
|
10
22
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
23
|
+
<!-- tocstop -->
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
bask requires [Bun](https://bun.sh) to be installed. Tasks are built on top of Bun's `$` shell.
|
|
14
28
|
|
|
15
|
-
|
|
29
|
+
- Your project does _not_ need to be Bun-based. You can use bask with any runtime (Node) or package manager (npm, yarn, pnpm, etc.).
|
|
16
30
|
|
|
17
31
|
```sh
|
|
18
|
-
bun add --
|
|
32
|
+
bun add --dev @adamhl8/bask
|
|
19
33
|
```
|
|
20
34
|
|
|
21
|
-
|
|
35
|
+
> [!TIP]
|
|
36
|
+
> To run tasks with a plain `bask` instead of `bunx bask`, also install it globally:
|
|
37
|
+
>
|
|
38
|
+
> ```sh
|
|
39
|
+
> bun add --global @adamhl8/bask
|
|
40
|
+
> ```
|
|
41
|
+
>
|
|
42
|
+
> The global `bask` hands off to the project's copy when there is one, so each project will use its installed version of bask.
|
|
22
43
|
|
|
23
44
|
## Usage
|
|
24
45
|
|
|
25
|
-
|
|
46
|
+
Create a `bask.ts` in your project root and export some tasks:
|
|
26
47
|
|
|
27
48
|
```ts
|
|
28
49
|
import { task } from "@adamhl8/bask"
|
|
@@ -32,7 +53,7 @@ export const lint = task(async ({ sh }) => {
|
|
|
32
53
|
})
|
|
33
54
|
|
|
34
55
|
export const build = task({ desc: "Lint, then build" }, async ({ sh }) => {
|
|
35
|
-
await lint()
|
|
56
|
+
await lint() // calling a task makes it a dependency
|
|
36
57
|
await sh`tsdown`
|
|
37
58
|
})
|
|
38
59
|
|
|
@@ -43,18 +64,23 @@ export const bumpDeps = task(async ({ sh }) => {
|
|
|
43
64
|
export default build
|
|
44
65
|
```
|
|
45
66
|
|
|
46
|
-
Run a task
|
|
67
|
+
Run a task by its export name:
|
|
47
68
|
|
|
48
69
|
```sh
|
|
49
70
|
bask build
|
|
50
|
-
bask bump-deps
|
|
71
|
+
bask bump-deps # runs `bumpDeps`
|
|
51
72
|
```
|
|
52
73
|
|
|
53
|
-
|
|
74
|
+
> [!TIP]
|
|
75
|
+
> In scripts or CI, use `bunx bask` so you don't have to install bask globally.
|
|
54
76
|
|
|
55
|
-
|
|
77
|
+
Run `bask` with no task name to run the default export (`build` above).
|
|
56
78
|
|
|
57
|
-
|
|
79
|
+
```sh
|
|
80
|
+
bask # runs `build`
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Run `bask --list` to list available tasks:
|
|
58
84
|
|
|
59
85
|
```
|
|
60
86
|
Available tasks:
|
|
@@ -63,17 +89,34 @@ Available tasks:
|
|
|
63
89
|
lint
|
|
64
90
|
```
|
|
65
91
|
|
|
66
|
-
|
|
92
|
+
## Tasks
|
|
93
|
+
|
|
94
|
+
### Defining tasks
|
|
95
|
+
|
|
96
|
+
`task` takes a function, and optionally an [options](#task-options) object first. The function receives `sh`, a [shell](#running-commands) for running commands, and it can be async or sync:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
export const build = task({ desc: "Build the project" }, async ({ sh }) => {
|
|
100
|
+
await sh`tsdown`
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
export const hello = task(() => {
|
|
104
|
+
console.log("hello")
|
|
105
|
+
})
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
---
|
|
67
109
|
|
|
68
110
|
### Task options
|
|
69
111
|
|
|
70
|
-
- `desc`:
|
|
71
|
-
- `hidden`:
|
|
72
|
-
- `alias`:
|
|
112
|
+
- `desc`: Shown next to the task in the task list
|
|
113
|
+
- `hidden`: Keeps the task runnable but leaves it out of the task list
|
|
114
|
+
- `alias`: Other names the task runs under, as a string or an array
|
|
115
|
+
- Write them in kebab-case or camelCase, and either spelling runs the task
|
|
73
116
|
|
|
74
117
|
```ts
|
|
75
118
|
export const releaseRun = task({ desc: "Run from CI", hidden: true }, async ({ sh }) => {
|
|
76
|
-
await sh`
|
|
119
|
+
await sh`release-it --ci`
|
|
77
120
|
})
|
|
78
121
|
|
|
79
122
|
export const bumpDeps = task({ alias: ["up", "update-deps"] }, async ({ sh }) => {
|
|
@@ -81,54 +124,199 @@ export const bumpDeps = task({ alias: ["up", "update-deps"] }, async ({ sh }) =>
|
|
|
81
124
|
})
|
|
82
125
|
```
|
|
83
126
|
|
|
84
|
-
Aliases show next to the task in the list:
|
|
127
|
+
Aliases show next to the task in the task list:
|
|
85
128
|
|
|
86
129
|
```
|
|
87
130
|
bump-deps # [aliases: up, update-deps]
|
|
88
131
|
```
|
|
89
132
|
|
|
90
|
-
|
|
133
|
+
---
|
|
91
134
|
|
|
92
|
-
###
|
|
135
|
+
### Task naming
|
|
93
136
|
|
|
94
|
-
|
|
137
|
+
A task exported with a camelCase name can be run by using its equivalent kebab-case name.
|
|
95
138
|
|
|
96
139
|
```ts
|
|
97
|
-
export const
|
|
98
|
-
|
|
99
|
-
|
|
140
|
+
export const bumpDeps = task(async ({ sh }) => {
|
|
141
|
+
// ...
|
|
142
|
+
})
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
These will both run `bumpDeps`:
|
|
146
|
+
|
|
147
|
+
```sh
|
|
148
|
+
bask bumpDeps
|
|
149
|
+
bask bump-deps
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
### The default task
|
|
155
|
+
|
|
156
|
+
Running `bask` on its own runs the default export. If `bask.ts` has no default export, it lists your tasks instead (the same as `bask --list`).
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
import { task } from "@adamhl8/bask"
|
|
160
|
+
|
|
161
|
+
export const build = task(async ({ sh }) => {
|
|
162
|
+
await sh`tsdown`
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
export default build
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
### Task dependencies
|
|
171
|
+
|
|
172
|
+
To depend on another task, call it. Each task runs at most once per invocation, however many tasks call it. If two tasks call it at the same time, they both wait on the same run.
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
export const check = task(async () => {
|
|
176
|
+
await Promise.all([lint(), build()])
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
export const ci = task(async () => {
|
|
180
|
+
await check()
|
|
181
|
+
await lint() // already ran, so this does nothing
|
|
182
|
+
})
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
### Task hooks
|
|
188
|
+
|
|
189
|
+
> [!IMPORTANT]
|
|
190
|
+
> You'll generally only need hooks when [extending tasks](#extendingsharing-tasks) from another bask file. For tasks in your own `bask.ts`, it's simpler to call them directly from the task.
|
|
191
|
+
|
|
192
|
+
On tasks, use `before` and `after` to add hooks:
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
export const clean = task(async ({ sh }) => {
|
|
196
|
+
await sh`rm -rf dist`
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
export const build = task(async ({ sh }) => {
|
|
200
|
+
await sh`tsdown`
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
build.before(clean)
|
|
204
|
+
build.after(async ({ sh }) => {
|
|
205
|
+
await sh`du -sh dist`
|
|
100
206
|
})
|
|
101
207
|
```
|
|
102
208
|
|
|
103
|
-
|
|
209
|
+
- Hooks take tasks or plain functions
|
|
210
|
+
- Hooks run in the order they were added
|
|
211
|
+
- Hooks run once per invocation, along with their task
|
|
212
|
+
- This includes when another task calls the task
|
|
213
|
+
- Each hook gets its own `sh`, separate from the task's
|
|
214
|
+
- Adding a hook to a task that has already started throws an error
|
|
104
215
|
|
|
105
|
-
The
|
|
216
|
+
The built-in `list` task, which prints the task list, takes hooks too:
|
|
106
217
|
|
|
107
|
-
|
|
218
|
+
```ts
|
|
219
|
+
import { list } from "@adamhl8/bask"
|
|
108
220
|
|
|
221
|
+
list.after(() => {
|
|
222
|
+
console.log("\nRun `bask <task>` to run a task.")
|
|
223
|
+
})
|
|
109
224
|
```
|
|
110
|
-
|
|
225
|
+
|
|
226
|
+
## Running commands
|
|
227
|
+
|
|
228
|
+
Every task gets a shell, `sh`, which works like Bun's `$`. One difference is that `sh` prints each command before running it:
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
export const lint = task(async ({ sh }) => {
|
|
232
|
+
await sh`oxlint --fix`
|
|
233
|
+
// prints "oxlint --fix", then runs it
|
|
234
|
+
})
|
|
111
235
|
```
|
|
112
236
|
|
|
113
|
-
|
|
237
|
+
> [!TIP]
|
|
238
|
+
> Like a `package.json` script, `sh` finds installed bins in `node_modules/.bin`, so `oxlint` and `bun oxlint` both work.
|
|
114
239
|
|
|
115
|
-
|
|
240
|
+
To run a command without printing it, use `.echo(false)`:
|
|
241
|
+
|
|
242
|
+
```ts
|
|
243
|
+
export const release = task(async ({ sh }) => {
|
|
244
|
+
const branch = await sh`git branch --show-current`.echo(false).text()
|
|
245
|
+
if (branch.trim() === "main") await sh`gh workflow run release.yml`
|
|
246
|
+
})
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
See the [Bun Shell docs](https://bun.sh/docs/runtime/shell) for the full API.
|
|
250
|
+
|
|
251
|
+
### Shell settings
|
|
252
|
+
|
|
253
|
+
The following methods can be used on the `sh` instance _or_ on each command.
|
|
254
|
+
|
|
255
|
+
- `.cwd()` - Changes the working directory
|
|
256
|
+
- Relative paths resolve from the directory you ran `bask` in, not from the `sh` instance's cwd
|
|
257
|
+
- `.env()` - Sets the environment variables
|
|
258
|
+
- This replaces the whole environment, so spread `process.env` to keep it: `.env({ ...process.env, DEBUG: "1" })`
|
|
259
|
+
- `.nothrow()` - Doesn't throw an error when a command exits with a non-zero exit code
|
|
260
|
+
- `.throws()` - Sets whether a non-zero exit code throws an error
|
|
261
|
+
- `.echo()` - Sets whether the command is printed before it runs
|
|
262
|
+
|
|
263
|
+
Each task gets its own fresh instance of `sh`, so changes to `sh` do not affect other tasks' `sh`. Using the above methods on `sh` will apply to every command after the call in the task.
|
|
116
264
|
|
|
117
265
|
```ts
|
|
118
266
|
export const buildWeb = task(async ({ sh }) => {
|
|
119
267
|
sh.cwd("packages/web")
|
|
120
268
|
await sh`bun install`
|
|
121
269
|
await sh`bun run build`
|
|
270
|
+
// Override `cwd` for just this command
|
|
271
|
+
await sh`cp -r packages/web/dist packages/server/public`.cwd(".")
|
|
122
272
|
})
|
|
123
273
|
```
|
|
124
274
|
|
|
125
|
-
|
|
275
|
+
## Extending/sharing tasks
|
|
276
|
+
|
|
277
|
+
Tasks don't have to live in `bask.ts`. They can live in any other file or package, so many projects can use the same tasks.
|
|
126
278
|
|
|
127
|
-
|
|
279
|
+
```ts
|
|
280
|
+
// shared-tasks.ts
|
|
281
|
+
import { task } from "@adamhl8/bask"
|
|
128
282
|
|
|
129
|
-
|
|
283
|
+
export const lint = task(async ({ sh }) => {
|
|
284
|
+
await sh`oxlint`
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
export const build = task(async ({ sh }) => {
|
|
288
|
+
await lint()
|
|
289
|
+
await sh`tsdown`
|
|
290
|
+
})
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
To make them runnable, re-export them from your `bask.ts`:
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
// bask.ts
|
|
297
|
+
import { task } from "@adamhl8/bask"
|
|
298
|
+
|
|
299
|
+
export * from "./shared-tasks.ts"
|
|
300
|
+
|
|
301
|
+
export const deploy = task(async ({ sh }) => {
|
|
302
|
+
// ...
|
|
303
|
+
})
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
The `lint`, `build`, and `deploy` tasks will all be available.
|
|
307
|
+
|
|
308
|
+
> [!IMPORTANT]
|
|
309
|
+
> `export * from` never re-exports a default. If a shared tasks module has a default export, re-export it by name for it to apply:
|
|
310
|
+
>
|
|
311
|
+
> ```ts
|
|
312
|
+
> export * from "./shared-tasks.ts"
|
|
313
|
+
> export { default } from "./shared-tasks.ts"
|
|
314
|
+
> ```
|
|
315
|
+
|
|
316
|
+
If a project needs to do something before or after a shared task, use [hooks](#task-hooks). A hook you add to an imported task also runs when the shared tasks call it. For example:
|
|
130
317
|
|
|
131
318
|
```ts
|
|
319
|
+
// bask.ts
|
|
132
320
|
import { task } from "@adamhl8/bask"
|
|
133
321
|
|
|
134
322
|
import { build, lint } from "./shared-tasks.ts"
|
|
@@ -136,23 +324,36 @@ import { build, lint } from "./shared-tasks.ts"
|
|
|
136
324
|
export * from "./shared-tasks.ts"
|
|
137
325
|
|
|
138
326
|
export const astroSync = task(async ({ sh }) => {
|
|
139
|
-
await sh`
|
|
327
|
+
await sh`astro sync`
|
|
140
328
|
})
|
|
141
329
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
await sh`bun astro build`
|
|
330
|
+
export const astroBuild = task(async ({ sh }) => {
|
|
331
|
+
await sh`astro build`
|
|
145
332
|
})
|
|
333
|
+
|
|
334
|
+
lint.before(astroSync)
|
|
335
|
+
build.after(astroBuild)
|
|
146
336
|
```
|
|
147
337
|
|
|
148
|
-
|
|
338
|
+
```sh
|
|
339
|
+
bask build
|
|
340
|
+
# runs astroSync -> lint -> build -> astroBuild
|
|
341
|
+
```
|
|
149
342
|
|
|
150
|
-
|
|
343
|
+
This way, each project extends the shared tasks without copying or replacing them.
|
|
151
344
|
|
|
152
|
-
|
|
153
|
-
import { list } from "@adamhl8/bask"
|
|
345
|
+
## CLI Reference
|
|
154
346
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
347
|
+
`bask [options] [task]`
|
|
348
|
+
|
|
349
|
+
- `task`: The task to run, by its kebab-case name, camelCase name, or an alias (default: the default export)
|
|
350
|
+
- `-l`, `--list`: List the available tasks
|
|
351
|
+
|
|
352
|
+
If the task doesn't exist, bask prints an error followed by the task list.
|
|
353
|
+
|
|
354
|
+
**Exit codes:**
|
|
355
|
+
|
|
356
|
+
- `0`: The task succeeded
|
|
357
|
+
- `1`: There's no `bask.ts`, it has a problem (e.g. a task exported twice), or the task doesn't exist
|
|
358
|
+
- `2`: The arguments are invalid
|
|
359
|
+
- Otherwise, the exit code of the command that failed
|
package/dist/cli.js
CHANGED
|
@@ -1,23 +1,32 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
|
+
import { printError } from "./utils.js";
|
|
2
3
|
import { getFailedTask } from "./task.js";
|
|
3
4
|
import { BaskFileError, formatTaskList, getTaskIndex, list, setBaskModule } from "./registry.js";
|
|
4
|
-
import { parseArgs, styleText } from "node:util";
|
|
5
|
-
import bun from "bun";
|
|
6
5
|
import path from "node:path";
|
|
6
|
+
import bun from "bun";
|
|
7
|
+
import { parseArgs } from "node:util";
|
|
7
8
|
//#region src/cli.ts
|
|
9
|
+
const cwd = process.cwd();
|
|
8
10
|
const isModule = (value) => typeof value === "object" && Boolean(value);
|
|
9
|
-
const printError = (message) => {
|
|
10
|
-
process.stderr.write(`${styleText(["bold", "red"], "error:", { stream: process.stderr })} ${message}\n`);
|
|
11
|
-
};
|
|
12
11
|
const isParseArgsError = (error) => error instanceof TypeError && "code" in error && typeof error.code === "string" && error.code.startsWith("ERR_PARSE_ARGS_");
|
|
13
12
|
/** Finds the project's own bask CLI, so a global `bask` runs the version the project pins. */
|
|
14
13
|
const resolveLocalCli = () => {
|
|
15
14
|
try {
|
|
16
|
-
return bun.resolveSync("@adamhl8/bask/cli",
|
|
15
|
+
return bun.resolveSync("@adamhl8/bask/cli", cwd);
|
|
17
16
|
} catch {
|
|
18
17
|
return;
|
|
19
18
|
}
|
|
20
19
|
};
|
|
20
|
+
/** Lists `node_modules/.bin` for `dir` and each of its parents, nearest first. */
|
|
21
|
+
const getBinDirs = (dir) => {
|
|
22
|
+
const binDir = path.join(dir, "node_modules", ".bin");
|
|
23
|
+
const parent = path.dirname(dir);
|
|
24
|
+
return parent === dir ? [binDir] : [binDir, ...getBinDirs(parent)];
|
|
25
|
+
};
|
|
26
|
+
/** Prepends the project's `.bin` folders to `PATH`, like `bun run` does, so tasks can run installed bins by name. */
|
|
27
|
+
const addBinDirsToPath = () => {
|
|
28
|
+
process.env["PATH"] = [...getBinDirs(cwd), process.env["PATH"]].filter(Boolean).join(path.delimiter);
|
|
29
|
+
};
|
|
21
30
|
const parseCliArgs = () => {
|
|
22
31
|
try {
|
|
23
32
|
return parseArgs({
|
|
@@ -55,12 +64,13 @@ const main = async () => {
|
|
|
55
64
|
const args = parseCliArgs();
|
|
56
65
|
if (!args) return;
|
|
57
66
|
const { values, positionals } = args;
|
|
58
|
-
const baskPath = path.
|
|
67
|
+
const baskPath = path.join(cwd, "bask.ts");
|
|
59
68
|
if (!await bun.file(baskPath).exists()) {
|
|
60
|
-
printError(`no bask.ts in ${
|
|
69
|
+
printError(`no bask.ts in ${cwd}`);
|
|
61
70
|
process.exitCode = 1;
|
|
62
71
|
return;
|
|
63
72
|
}
|
|
73
|
+
addBinDirsToPath();
|
|
64
74
|
const baskModule = await import(baskPath);
|
|
65
75
|
if (!isModule(baskModule)) throw new Error("failed to import bask.ts");
|
|
66
76
|
try {
|
package/dist/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","names":[],"sources":["../src/cli.ts"],"sourcesContent":["#!/usr/bin/env bun\n\nimport path from \"node:path\"\nimport { parseArgs
|
|
1
|
+
{"version":3,"file":"cli.js","names":[],"sources":["../src/cli.ts"],"sourcesContent":["#!/usr/bin/env bun\n\nimport path from \"node:path\"\nimport { parseArgs } from \"node:util\"\n\nimport bun from \"bun\"\n\nimport { BaskFileError, formatTaskList, getTaskIndex, list, setBaskModule } from \"#registry.ts\"\nimport { getFailedTask } from \"#task.ts\"\nimport type { Task } from \"#task.ts\"\nimport { printError } from \"#utils.ts\"\n\nconst cwd = process.cwd()\n\nconst isModule = (value: unknown): value is Record<string, unknown> => typeof value === \"object\" && Boolean(value)\n\nconst isParseArgsError = (error: unknown): error is TypeError & { code: string } =>\n error instanceof TypeError &&\n \"code\" in error &&\n typeof error.code === \"string\" &&\n error.code.startsWith(\"ERR_PARSE_ARGS_\")\n\n/** Finds the project's own bask CLI, so a global `bask` runs the version the project pins. */\nconst resolveLocalCli = () => {\n try {\n return bun.resolveSync(\"@adamhl8/bask/cli\", cwd)\n } catch {\n return\n }\n}\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 `PATH`, like `bun run` does, so tasks can run installed bins by name. */\nconst addBinDirsToPath = () => {\n // oxlint-disable-next-line node/no-process-env - commands in `sh` look up bins on this `PATH`\n process.env[\"PATH\"] = [...getBinDirs(cwd), process.env[\"PATH\"]].filter(Boolean).join(path.delimiter)\n}\n\nconst parseCliArgs = () => {\n try {\n return parseArgs({ options: { list: { type: \"boolean\", short: \"l\" } }, allowPositionals: true })\n } catch (error) {\n if (!isParseArgsError(error)) throw error\n printError(error.message)\n process.exitCode = 2\n return\n }\n}\n\n/** Runs a task and reports a failed command in one line, naming the innermost task that failed. */\nconst runTask = async (taskToRun: Task, taskName: string) => {\n try {\n await taskToRun()\n } catch (error) {\n // Rethrow anything else, because a bug in a task file needs its stack trace.\n if (!(error instanceof bun.$.ShellError)) throw error\n\n const failedTask = getFailedTask(error)\n const failedEntry = getTaskIndex()\n .values()\n .find((entry) => entry.task === failedTask)\n // Fall back to the task you ran, because a task that isn't exported has no name.\n const failedName = failedEntry?.name ?? taskName\n printError(`task '${failedName}' failed with exit code ${error.exitCode}`)\n process.exitCode = error.exitCode\n }\n}\n\nconst main = async () => {\n // Import the local CLI and stop, because it runs its own `main` when loaded.\n // The local copy resolves to its own path, so it skips this and runs normally.\n const localCli = resolveLocalCli()\n if (localCli && localCli !== import.meta.path) {\n await import(localCli)\n return\n }\n\n const args = parseCliArgs()\n if (!args) return\n const { values, positionals } = args\n\n const baskPath = path.join(cwd, \"bask.ts\")\n if (!(await bun.file(baskPath).exists())) {\n printError(`no bask.ts in ${cwd}`)\n process.exitCode = 1\n return\n }\n\n addBinDirsToPath()\n const baskModule: unknown = await import(baskPath)\n if (!isModule(baskModule)) throw new Error(\"failed to import bask.ts\")\n try {\n setBaskModule(baskModule)\n } catch (error) {\n if (!(error instanceof BaskFileError)) throw error\n printError(error.message)\n process.exitCode = 1\n return\n }\n\n const [taskName] = positionals\n\n if (values.list || (taskName === undefined && !getTaskIndex().has(\"default\"))) {\n await runTask(list, \"list\")\n return\n }\n\n const selectedEntry = getTaskIndex().get(taskName ?? \"default\")\n\n if (!selectedEntry) {\n printError(`unknown task '${taskName ?? \"default\"}'`)\n process.stderr.write(`\\n${formatTaskList()}\\n`)\n process.exitCode = 1\n return\n }\n\n await runTask(selectedEntry.task, selectedEntry.name)\n}\n\nawait main()\n"],"mappings":";;;;;;;;AAYA,MAAM,MAAM,QAAQ,IAAI;AAExB,MAAM,YAAY,UAAqD,OAAO,UAAU,YAAY,QAAQ,KAAK;AAEjH,MAAM,oBAAoB,UACxB,iBAAiB,aACjB,UAAU,SACV,OAAO,MAAM,SAAS,YACtB,MAAM,KAAK,WAAW,iBAAiB;;AAGzC,MAAM,wBAAwB;CAC5B,IAAI;EACF,OAAO,IAAI,YAAY,qBAAqB,GAAG;CACjD,QAAQ;EACN;CACF;AACF;;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,MAAM,yBAAyB;CAE7B,QAAQ,IAAI,UAAU,CAAC,GAAG,WAAW,GAAG,GAAG,QAAQ,IAAI,OAAO,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,KAAK,KAAK,SAAS;AACrG;AAEA,MAAM,qBAAqB;CACzB,IAAI;EACF,OAAO,UAAU;GAAE,SAAS,EAAE,MAAM;IAAE,MAAM;IAAW,OAAO;GAAI,EAAE;GAAG,kBAAkB;EAAK,CAAC;CACjG,SAAS,OAAO;EACd,IAAI,CAAC,iBAAiB,KAAK,GAAG,MAAM;EACpC,WAAW,MAAM,OAAO;EACxB,QAAQ,WAAW;EACnB;CACF;AACF;;AAGA,MAAM,UAAU,OAAO,WAAiB,aAAqB;CAC3D,IAAI;EACF,MAAM,UAAU;CAClB,SAAS,OAAO;EAEd,IAAI,EAAE,iBAAiB,IAAI,EAAE,aAAa,MAAM;EAEhD,MAAM,aAAa,cAAc,KAAK;EAKtC,MAAM,aAJc,aAAa,CAAC,CAC/B,OAAO,CAAC,CACR,MAAM,UAAU,MAAM,SAAS,UAEL,CAAC,EAAE,QAAQ;EACxC,WAAW,SAAS,WAAW,0BAA0B,MAAM,UAAU;EACzE,QAAQ,WAAW,MAAM;CAC3B;AACF;AAEA,MAAM,OAAO,YAAY;CAGvB,MAAM,WAAW,gBAAgB;CACjC,IAAI,YAAY,aAAa,YAAY,MAAM;EAC7C,MAAM,OAAO;EACb;CACF;CAEA,MAAM,OAAO,aAAa;CAC1B,IAAI,CAAC,MAAM;CACX,MAAM,EAAE,QAAQ,gBAAgB;CAEhC,MAAM,WAAW,KAAK,KAAK,KAAK,SAAS;CACzC,IAAI,CAAE,MAAM,IAAI,KAAK,QAAQ,CAAC,CAAC,OAAO,GAAI;EACxC,WAAW,iBAAiB,KAAK;EACjC,QAAQ,WAAW;EACnB;CACF;CAEA,iBAAiB;CACjB,MAAM,aAAsB,MAAM,OAAO;CACzC,IAAI,CAAC,SAAS,UAAU,GAAG,MAAM,IAAI,MAAM,0BAA0B;CACrE,IAAI;EACF,cAAc,UAAU;CAC1B,SAAS,OAAO;EACd,IAAI,EAAE,iBAAiB,gBAAgB,MAAM;EAC7C,WAAW,MAAM,OAAO;EACxB,QAAQ,WAAW;EACnB;CACF;CAEA,MAAM,CAAC,YAAY;CAEnB,IAAI,OAAO,QAAS,aAAa,KAAA,KAAa,CAAC,aAAa,CAAC,CAAC,IAAI,SAAS,GAAI;EAC7E,MAAM,QAAQ,MAAM,MAAM;EAC1B;CACF;CAEA,MAAM,gBAAgB,aAAa,CAAC,CAAC,IAAI,YAAY,SAAS;CAE9D,IAAI,CAAC,eAAe;EAClB,WAAW,iBAAiB,YAAY,UAAU,EAAE;EACpD,QAAQ,OAAO,MAAM,KAAK,eAAe,EAAE,GAAG;EAC9C,QAAQ,WAAW;EACnB;CACF;CAEA,MAAM,QAAQ,cAAc,MAAM,cAAc,IAAI;AACtD;AAEA,MAAM,KAAK"}
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
import { printError } from "./utils.js";
|
|
1
2
|
import { isTask, task } from "./task.js";
|
|
2
3
|
import { list } from "./registry.js";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import bun from "bun";
|
|
6
|
+
//#region src/index.ts
|
|
7
|
+
if (path.basename(bun.main) === "bask.ts") {
|
|
8
|
+
const command = ["bask", ...process.argv.slice(2)].join(" ");
|
|
9
|
+
printError(`bask.ts can't be run directly. Use \`bunx\` instead of \`bun\`.\nEither run \`bunx ${command}\` or run \`${command}\` directly (if installed globally).`);
|
|
10
|
+
process.exit(1);
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
3
13
|
export { isTask, list, task };
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import path from \"node:path\"\n\nimport bun from \"bun\"\n\nimport { printError } from \"#utils.ts\"\n\n// Stop a `bun bask` run because Bun runs bask.ts itself instead of the bask CLI.\nif (path.basename(bun.main) === \"bask.ts\") {\n const command = [\"bask\", ...process.argv.slice(2)].join(\" \")\n printError(\n `bask.ts can't be run directly. Use \\`bunx\\` instead of \\`bun\\`.\\nEither run \\`bunx ${command}\\` or run \\`${command}\\` directly (if installed globally).`,\n )\n process.exit(1)\n}\n\nexport { list } from \"#registry.ts\"\nexport { isTask, task } from \"#task.ts\"\nexport type { Task } from \"#task.ts\"\n"],"mappings":";;;;;;AAOA,IAAI,KAAK,SAAS,IAAI,IAAI,MAAM,WAAW;CACzC,MAAM,UAAU,CAAC,QAAQ,GAAG,QAAQ,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG;CAC3D,WACE,sFAAsF,QAAQ,cAAc,QAAQ,qCACtH;CACA,QAAQ,KAAK,CAAC;AAChB"}
|
package/dist/shell.js
CHANGED
package/dist/task.js
CHANGED
|
@@ -11,8 +11,9 @@ function task(optionsOrFn, maybeFn) {
|
|
|
11
11
|
const beforeFns = [];
|
|
12
12
|
const afterFns = [];
|
|
13
13
|
let runPromise;
|
|
14
|
+
let hasStarted = false;
|
|
14
15
|
const assertNotStarted = () => {
|
|
15
|
-
if (
|
|
16
|
+
if (hasStarted) throw new Error("Can't add a hook to a task that has already started");
|
|
16
17
|
};
|
|
17
18
|
const before = (...fns) => {
|
|
18
19
|
assertNotStarted();
|
|
@@ -24,6 +25,7 @@ function task(optionsOrFn, maybeFn) {
|
|
|
24
25
|
};
|
|
25
26
|
const runWithShell = async (taskFn) => taskFn({ sh: createShell() });
|
|
26
27
|
const runAll = async () => {
|
|
28
|
+
hasStarted = true;
|
|
27
29
|
for (const beforeFn of beforeFns) await runWithShell(beforeFn);
|
|
28
30
|
await runWithShell(fn);
|
|
29
31
|
for (const afterFn of afterFns) await runWithShell(afterFn);
|
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\n const assertNotStarted = () => {\n if (
|
|
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"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { styleText } from "node:util";
|
|
2
|
+
//#region src/utils.ts
|
|
3
|
+
const printError = (message) => {
|
|
4
|
+
process.stderr.write(`${styleText(["bold", "red"], "error:", { stream: process.stderr })} ${message}\n`);
|
|
5
|
+
};
|
|
6
|
+
//#endregion
|
|
7
|
+
export { printError };
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","names":[],"sources":["../src/utils.ts"],"sourcesContent":["import { styleText } from \"node:util\"\n\n// Write to `process.stderr` directly because Bun colors all `console.error` output red in a terminal.\nexport const printError = (message: string) => {\n process.stderr.write(`${styleText([\"bold\", \"red\"], \"error:\", { stream: process.stderr })} ${message}\\n`)\n}\n"],"mappings":";;AAGA,MAAa,cAAc,YAAoB;CAC7C,QAAQ,OAAO,MAAM,GAAG,UAAU,CAAC,QAAQ,KAAK,GAAG,UAAU,EAAE,QAAQ,QAAQ,OAAO,CAAC,EAAE,GAAG,QAAQ,GAAG;AACzG"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adamhl8/bask",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Write your tasks in TypeScript",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"build-tool",
|
|
@@ -52,12 +52,13 @@
|
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@adamhl8/bask": "file:.",
|
|
55
|
-
"@adamhl8/configs": "^3.0.
|
|
55
|
+
"@adamhl8/configs": "^3.0.3",
|
|
56
56
|
"@arethetypeswrong/core": "^0.18.5",
|
|
57
|
-
"@commitlint/cli": "^21.2.
|
|
58
|
-
"@commitlint/config-conventional": "^21.2.
|
|
57
|
+
"@commitlint/cli": "^21.2.4",
|
|
58
|
+
"@commitlint/config-conventional": "^21.2.4",
|
|
59
59
|
"@types/bun": "^1.4.3",
|
|
60
|
-
"
|
|
60
|
+
"execa": "^10.2.0",
|
|
61
|
+
"knip": "^6.42.0",
|
|
61
62
|
"markdown-toc": "^1.2.0",
|
|
62
63
|
"oxfmt": "^0.73.0",
|
|
63
64
|
"oxlint": "^1.88.0",
|