plantr 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +383 -221
- package/dist/src/cli.d.ts.map +1 -1
- package/dist/src/create-seeder-instance.d.ts +76 -10
- package/dist/src/create-seeder-instance.d.ts.map +1 -1
- package/dist/src/dependency-resolver.d.ts +5 -4
- package/dist/src/dependency-resolver.d.ts.map +1 -1
- package/dist/src/errors.d.ts +18 -0
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/formatting.d.ts +16 -0
- package/dist/src/formatting.d.ts.map +1 -1
- package/dist/src/map-with-limit.d.ts +19 -0
- package/dist/src/map-with-limit.d.ts.map +1 -0
- package/dist/src/run-seeders.d.ts +39 -8
- package/dist/src/run-seeders.d.ts.map +1 -1
- package/dist/src/signals.d.ts +9 -0
- package/dist/src/signals.d.ts.map +1 -0
- package/dist/src/step.d.ts +15 -0
- package/dist/src/step.d.ts.map +1 -1
- package/dist/src/types.d.ts +7 -0
- package/dist/src/types.d.ts.map +1 -1
- package/index.ts +5 -0
- package/package.json +1 -1
- package/readme.md +161 -21
- package/src/cli.ts +4 -13
- package/src/create-seeder-instance.ts +119 -21
- package/src/dependency-resolver.ts +5 -4
- package/src/errors.ts +29 -0
- package/src/formatting.ts +32 -1
- package/src/map-with-limit.ts +48 -0
- package/src/run-seeders.ts +304 -205
- package/src/signals.ts +54 -0
- package/src/step.ts +30 -4
- package/src/types.ts +7 -0
package/src/formatting.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { inspect as nodeInspect } from 'node:util'
|
|
2
|
+
import { isSeedUsageError } from './errors.js'
|
|
2
3
|
import type { SeederResult } from './types.js'
|
|
3
4
|
|
|
4
5
|
type BunInspectGlobal = {
|
|
@@ -19,6 +20,14 @@ export function inspectValue(value: unknown, options?: { colors?: boolean }): st
|
|
|
19
20
|
return nodeInspect(value, { colors: options?.colors ?? false })
|
|
20
21
|
}
|
|
21
22
|
|
|
23
|
+
/**
|
|
24
|
+
* An error as the person running the seed should see it: a usage error by its message
|
|
25
|
+
* alone, anything else in full (message, stack, `cause` and extra fields).
|
|
26
|
+
*/
|
|
27
|
+
export function formatError(error: unknown, options?: { colors?: boolean }): string {
|
|
28
|
+
return isSeedUsageError(error) ? error.message : inspectValue(error, options)
|
|
29
|
+
}
|
|
30
|
+
|
|
22
31
|
/**
|
|
23
32
|
* ANSI color codes for terminal output
|
|
24
33
|
*/
|
|
@@ -154,7 +163,7 @@ export function printErrors(results: readonly SeederResult[]): void {
|
|
|
154
163
|
for (const result of failed) {
|
|
155
164
|
const where = result.failedStep ? ` in step "${result.failedStep}"` : ''
|
|
156
165
|
console.log(colorize(`❌ Seeder "${result.name}" failed${where}:`, 'red'))
|
|
157
|
-
const details =
|
|
166
|
+
const details = formatError(result.error, { colors: supportsColor() })
|
|
158
167
|
for (const line of details.split('\n')) console.log(` ${line}`)
|
|
159
168
|
if (result.cachedSteps.length > 0) {
|
|
160
169
|
console.log(
|
|
@@ -174,6 +183,28 @@ export function printErrors(results: readonly SeederResult[]): void {
|
|
|
174
183
|
}
|
|
175
184
|
}
|
|
176
185
|
|
|
186
|
+
/**
|
|
187
|
+
* Print errors thrown while cleaning up (cache flush, onAfterAll, teardown, deferred
|
|
188
|
+
* cleanups) that are not rethrown, so a second failure is never silently lost.
|
|
189
|
+
*/
|
|
190
|
+
export function printCleanupErrors(errors: readonly unknown[]): void {
|
|
191
|
+
for (const error of errors) {
|
|
192
|
+
console.error(colorize('Cleanup after the run also failed:', 'red'))
|
|
193
|
+
for (const line of formatError(error, { colors: supportsColor() }).split('\n')) {
|
|
194
|
+
console.error(` ${line}`)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Print that `onFresh` is about to run, before the seeders
|
|
201
|
+
*/
|
|
202
|
+
export function printFreshReset(seeders: readonly string[], all: boolean): void {
|
|
203
|
+
const which = all ? 'every seeder runs fresh' : `fresh: ${seeders.join(', ')}`
|
|
204
|
+
console.log(colorize(`↺ onFresh (${which})`, 'yellow'))
|
|
205
|
+
console.log('')
|
|
206
|
+
}
|
|
207
|
+
|
|
177
208
|
/**
|
|
178
209
|
* Print seeder header before execution
|
|
179
210
|
*/
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { MaybePromise } from './types.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Run `fn` over `items` with at most `limit` calls in flight, and resolve to the results
|
|
5
|
+
* in the order of `items`, whatever order the calls finish in.
|
|
6
|
+
*
|
|
7
|
+
* Fails fast: after the first call throws or rejects, no further item starts. It waits for
|
|
8
|
+
* the calls already in flight to settle, so nothing still runs once it rejects (with the
|
|
9
|
+
* first error; later errors are dropped).
|
|
10
|
+
*
|
|
11
|
+
* @param limit - A positive integer, or `Infinity` for no limit
|
|
12
|
+
* @throws {RangeError} When `limit` is not a positive integer or `Infinity`
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const products = await mapWithLimit(bodies, 8, (body) => api.post('/products', body))
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export async function mapWithLimit<Item, Result>(
|
|
20
|
+
items: Iterable<Item>,
|
|
21
|
+
limit: number,
|
|
22
|
+
fn: (item: Item, index: number) => MaybePromise<Result>,
|
|
23
|
+
): Promise<Result[]> {
|
|
24
|
+
if (Number.isNaN(limit) || limit < 1 || (Number.isFinite(limit) && !Number.isInteger(limit))) {
|
|
25
|
+
throw new RangeError(
|
|
26
|
+
`Invalid limit (${String(limit)}): expected a positive integer or Infinity`,
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
const list = Array.from(items)
|
|
30
|
+
const results = new Array<Result>(list.length)
|
|
31
|
+
let next = 0
|
|
32
|
+
let failure: { error: unknown } | undefined
|
|
33
|
+
|
|
34
|
+
const worker = async () => {
|
|
35
|
+
while (!failure && next < list.length) {
|
|
36
|
+
const index = next++
|
|
37
|
+
try {
|
|
38
|
+
results[index] = await fn(list[index] as Item, index)
|
|
39
|
+
} catch (error) {
|
|
40
|
+
failure ??= { error }
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
await Promise.all(Array.from({ length: Math.min(limit, list.length) }, worker))
|
|
45
|
+
|
|
46
|
+
if (failure) throw failure.error
|
|
47
|
+
return results
|
|
48
|
+
}
|