plantr 1.0.1 → 2.0.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/LICENSE +21 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +365 -193
- package/dist/src/cli.d.ts +13 -0
- package/dist/src/create-seeder-instance.d.ts +29 -16
- package/dist/src/dependency-resolver.d.ts +16 -1
- package/dist/src/formatting.d.ts +7 -2
- package/dist/src/run-seeders.d.ts +28 -5
- package/dist/src/seeding-state.d.ts +5 -1
- package/dist/src/step.d.ts +27 -2
- package/dist/src/types.d.ts +20 -0
- package/dist/state.d.ts +11 -0
- package/dist/state.js +12 -0
- package/index.ts +6 -3
- package/package.json +19 -10
- package/readme.md +105 -26
- package/src/cli.ts +118 -0
- package/src/create-seeder-instance.ts +77 -111
- package/src/dependency-resolver.ts +42 -2
- package/src/formatting.ts +30 -12
- package/src/run-seeders.ts +172 -103
- package/src/seeding-state.ts +9 -4
- package/src/step.ts +73 -16
- package/src/types.ts +20 -0
- package/state.ts +11 -0
- package/dist/cli.d.ts +0 -17
- package/dist/cli.js +0 -59
- package/dist/index-a1s5r901.js +0 -4
package/src/cli.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { parseArgs } from 'node:util'
|
|
2
|
+
import { resolveDependencyOrder } from './dependency-resolver'
|
|
3
|
+
import { colorize } from './formatting'
|
|
4
|
+
import type { RunSeedersOptions, SeederAny } from './types'
|
|
5
|
+
|
|
6
|
+
type RunSeeders = (
|
|
7
|
+
seeders: readonly SeederAny[],
|
|
8
|
+
options?: RunSeedersOptions,
|
|
9
|
+
) => Promise<{ success: boolean }>
|
|
10
|
+
|
|
11
|
+
/** Errors whose message says everything; others are printed with their stack */
|
|
12
|
+
const USAGE_ERRORS = new Set([
|
|
13
|
+
'CircularDependencyError',
|
|
14
|
+
'MissingDependencyError',
|
|
15
|
+
'DuplicateSeederError',
|
|
16
|
+
'UnknownSeederError',
|
|
17
|
+
'SeedTargetError',
|
|
18
|
+
])
|
|
19
|
+
|
|
20
|
+
const helpText = (count: number) => `
|
|
21
|
+
${colorize('Usage:', 'cyan', 'bold')} bun run seed [seeder...] [options]
|
|
22
|
+
|
|
23
|
+
${colorize('Arguments:', 'white', 'bold')}
|
|
24
|
+
seeder... Run only these seeders and the seeders they depend on
|
|
25
|
+
|
|
26
|
+
${colorize('Options:', 'white', 'bold')}
|
|
27
|
+
--only a,b Same as naming the seeders
|
|
28
|
+
--fresh Ignore and overwrite cached steps of the named seeders
|
|
29
|
+
(of every seeder when none are named)
|
|
30
|
+
--continue-on-failure Keep running seeders that do not depend on a failed one
|
|
31
|
+
--allow-remote Seed a database that is not local
|
|
32
|
+
--dry-run Show execution order without running seeders
|
|
33
|
+
--help, -h Show this help message
|
|
34
|
+
|
|
35
|
+
${count} seeders registered.
|
|
36
|
+
`
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Parse CLI arguments, run the seeders and return the exit code.
|
|
40
|
+
* Unknown flags and unknown seeder names return 1.
|
|
41
|
+
*/
|
|
42
|
+
export async function runCli(
|
|
43
|
+
seeders: readonly SeederAny[],
|
|
44
|
+
runSeeders: RunSeeders,
|
|
45
|
+
{ name, argv }: { name: string; argv: readonly string[] },
|
|
46
|
+
): Promise<number> {
|
|
47
|
+
let parsed: ReturnType<typeof parse>
|
|
48
|
+
try {
|
|
49
|
+
parsed = parse(argv)
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error(colorize(error instanceof Error ? error.message : String(error), 'red'))
|
|
52
|
+
console.log(helpText(seeders.length))
|
|
53
|
+
return 1
|
|
54
|
+
}
|
|
55
|
+
const { values, positionals } = parsed
|
|
56
|
+
|
|
57
|
+
if (values.help) {
|
|
58
|
+
console.log(helpText(seeders.length))
|
|
59
|
+
return 0
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const names = [...positionals, ...(values.only ?? []).flatMap((value) => value.split(','))]
|
|
63
|
+
.map((value) => value.trim())
|
|
64
|
+
.filter(Boolean)
|
|
65
|
+
const only = names.length > 0 ? names : undefined
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
const order = resolveDependencyOrder(seeders, only)
|
|
69
|
+
|
|
70
|
+
if (values['dry-run']) {
|
|
71
|
+
console.log(
|
|
72
|
+
`\n${colorize('🌱 Dry Run', 'cyan', 'bold')} ${colorize(`(${order.length} seeders)`, 'dim')}\n`,
|
|
73
|
+
)
|
|
74
|
+
console.log(colorize('Execution order:', 'white', 'bold'))
|
|
75
|
+
order.forEach((seederName, i) => {
|
|
76
|
+
const deps = seeders.find((seeder) => seeder.name === seederName)?.dependsOn ?? []
|
|
77
|
+
const depsStr =
|
|
78
|
+
deps.length > 0 ? ` ${colorize(`(depends on: ${deps.join(', ')})`, 'dim')}` : ''
|
|
79
|
+
console.log(` ${i + 1}. ${seederName}${depsStr}`)
|
|
80
|
+
})
|
|
81
|
+
console.log('')
|
|
82
|
+
return 0
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
console.log(
|
|
86
|
+
`\n${colorize(`🌱 Running ${name}`, 'cyan', 'bold')} ${colorize(`(${order.length} seeders)`, 'dim')}\n`,
|
|
87
|
+
)
|
|
88
|
+
const { success } = await runSeeders(seeders, {
|
|
89
|
+
printResults: true,
|
|
90
|
+
verbose: true,
|
|
91
|
+
only,
|
|
92
|
+
fresh: values.fresh ? (only ?? true) : false,
|
|
93
|
+
continueOnFailure: values['continue-on-failure'],
|
|
94
|
+
allowRemote: values['allow-remote'],
|
|
95
|
+
})
|
|
96
|
+
return success ? 0 : 1
|
|
97
|
+
} catch (error) {
|
|
98
|
+
const usage = error instanceof Error && USAGE_ERRORS.has(error.name)
|
|
99
|
+
console.error(colorize(usage ? (error as Error).message : Bun.inspect(error), 'red'))
|
|
100
|
+
return 1
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function parse(argv: readonly string[]) {
|
|
105
|
+
return parseArgs({
|
|
106
|
+
args: [...argv],
|
|
107
|
+
allowPositionals: true,
|
|
108
|
+
strict: true,
|
|
109
|
+
options: {
|
|
110
|
+
help: { type: 'boolean', short: 'h' },
|
|
111
|
+
'dry-run': { type: 'boolean' },
|
|
112
|
+
only: { type: 'string', multiple: true },
|
|
113
|
+
fresh: { type: 'boolean' },
|
|
114
|
+
'continue-on-failure': { type: 'boolean' },
|
|
115
|
+
'allow-remote': { type: 'boolean' },
|
|
116
|
+
},
|
|
117
|
+
})
|
|
118
|
+
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { expect } from 'bun:test'
|
|
2
2
|
import { faker } from '@faker-js/faker'
|
|
3
|
+
import { runCli } from './cli'
|
|
3
4
|
import { createDefineSeeder } from './define-seeder'
|
|
4
|
-
import { resolveDependencyOrder } from './dependency-resolver'
|
|
5
|
-
import { colorize } from './formatting'
|
|
6
5
|
import { runSeedersCore } from './run-seeders'
|
|
7
6
|
import { isSeedingActive, setSeedingActive } from './seeding-state'
|
|
8
7
|
import { createStepRunner, type SeederCache } from './step'
|
|
@@ -47,7 +46,8 @@ export type SeederInstanceConfig<TContext extends BaseSeederContext> = {
|
|
|
47
46
|
* return cached?.output as T | undefined
|
|
48
47
|
* },
|
|
49
48
|
* set: async (key, value) => {
|
|
50
|
-
*
|
|
49
|
+
* const output = value as Prisma.InputJsonValue
|
|
50
|
+
* await prisma.seedCache.upsert({ where: { key }, create: { key, output }, update: { output } })
|
|
51
51
|
* }
|
|
52
52
|
* }
|
|
53
53
|
* ```
|
|
@@ -55,8 +55,23 @@ export type SeederInstanceConfig<TContext extends BaseSeederContext> = {
|
|
|
55
55
|
cache?: SeederCache
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
58
|
+
* Optional: the database URL this instance writes to, e.g. `() => process.env.DATABASE_URL`.
|
|
59
|
+
* The banner prints its host and database (never credentials), and a run refuses a
|
|
60
|
+
* host that is not local unless it passes `allowRemote` (CLI: `--allow-remote`).
|
|
61
|
+
* Return undefined to skip the check.
|
|
62
|
+
*/
|
|
63
|
+
target?: () => string | undefined
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Optional: seed faker before each seeder with this number plus a hash of the
|
|
67
|
+
* seeder's name, so every run generates the same data and adding a seeder does
|
|
68
|
+
* not shift the others'.
|
|
69
|
+
*/
|
|
70
|
+
fakerSeed?: number
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Called before all seeders run, before the context factory.
|
|
74
|
+
* Use for setup like connecting to databases, or to refuse a run.
|
|
60
75
|
*
|
|
61
76
|
* @example
|
|
62
77
|
* ```typescript
|
|
@@ -68,7 +83,8 @@ export type SeederInstanceConfig<TContext extends BaseSeederContext> = {
|
|
|
68
83
|
onBeforeAll?: () => void | Promise<void>
|
|
69
84
|
|
|
70
85
|
/**
|
|
71
|
-
* Called after all seeders complete (success or failure)
|
|
86
|
+
* Called after all seeders complete (success or failure), also when the context
|
|
87
|
+
* factory throws.
|
|
72
88
|
* Use for cleanup like disconnecting from databases.
|
|
73
89
|
*
|
|
74
90
|
* @example
|
|
@@ -115,7 +131,9 @@ export type SeederInstance<TContext extends BaseSeederContext> = {
|
|
|
115
131
|
*
|
|
116
132
|
* @param seeders - Array of seeders to run
|
|
117
133
|
* @param options - Optional runner options
|
|
118
|
-
* @returns Object containing results and success status
|
|
134
|
+
* @returns Object containing results and success status; `success` is false when a seeder failed
|
|
135
|
+
* @throws Before anything runs, on an invalid registry (duplicate name, missing
|
|
136
|
+
* dependency, cycle), an unknown name in `only`/`fresh`, or a refused `target`
|
|
119
137
|
*/
|
|
120
138
|
runSeeders: (
|
|
121
139
|
seeders: readonly SeederAny[],
|
|
@@ -123,8 +141,10 @@ export type SeederInstance<TContext extends BaseSeederContext> = {
|
|
|
123
141
|
) => Promise<{ results: SeederResult[]; success: boolean }>
|
|
124
142
|
|
|
125
143
|
/**
|
|
126
|
-
* Run seeders as a CLI command.
|
|
127
|
-
*
|
|
144
|
+
* Run seeders as a CLI command: parses the arguments, runs, and exits with 0 or 1.
|
|
145
|
+
* Seeder names (positional or `--only a,b`) select seeders plus their dependencies;
|
|
146
|
+
* `--fresh`, `--continue-on-failure`, `--allow-remote`, `--dry-run` and `--help` map
|
|
147
|
+
* to the runner options. Unknown flags and names exit 1.
|
|
128
148
|
*
|
|
129
149
|
* @example
|
|
130
150
|
* ```typescript
|
|
@@ -134,7 +154,14 @@ export type SeederInstance<TContext extends BaseSeederContext> = {
|
|
|
134
154
|
* }
|
|
135
155
|
* ```
|
|
136
156
|
*/
|
|
137
|
-
runSeedersCli: (
|
|
157
|
+
runSeedersCli: (
|
|
158
|
+
seeders: readonly SeederAny[],
|
|
159
|
+
options?: {
|
|
160
|
+
name?: string
|
|
161
|
+
/** Arguments to parse (default: `process.argv.slice(2)`) */
|
|
162
|
+
argv?: readonly string[]
|
|
163
|
+
},
|
|
164
|
+
) => Promise<void>
|
|
138
165
|
|
|
139
166
|
/**
|
|
140
167
|
* Check if seeding is currently active.
|
|
@@ -172,15 +199,7 @@ export type SeederInstance<TContext extends BaseSeederContext> = {
|
|
|
172
199
|
* ...base,
|
|
173
200
|
* prisma,
|
|
174
201
|
* }),
|
|
175
|
-
*
|
|
176
|
-
* get: async (key) => {
|
|
177
|
-
* const cached = await prisma.seedCache.findUnique({ where: { key } })
|
|
178
|
-
* return cached?.output
|
|
179
|
-
* },
|
|
180
|
-
* set: async (key, value) => {
|
|
181
|
-
* await prisma.seedCache.create({ data: { key, output: value } })
|
|
182
|
-
* }
|
|
183
|
-
* },
|
|
202
|
+
* target: () => process.env.DATABASE_URL,
|
|
184
203
|
* })
|
|
185
204
|
*
|
|
186
205
|
* export { defineSeeder, isSeedingActive }
|
|
@@ -204,106 +223,43 @@ export function createSeederInstance<TContext extends BaseSeederContext = BaseSe
|
|
|
204
223
|
seeders: readonly SeederAny[],
|
|
205
224
|
options?: RunSeedersOptions,
|
|
206
225
|
): Promise<{ results: SeederResult[]; success: boolean }> => {
|
|
207
|
-
//
|
|
208
|
-
const { step, setSeederName } = createStepRunner({
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
214
|
-
const ctx = await config.context(baseContext)
|
|
226
|
+
// One step runner per run, so cache keys and duplicate checks are per run
|
|
227
|
+
const { step, setSeederName } = createStepRunner({
|
|
228
|
+
cache: config.cache,
|
|
229
|
+
log: options?.verbose ?? true,
|
|
230
|
+
})
|
|
231
|
+
const { fakerSeed } = config
|
|
215
232
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
+
setSeedingActive(true)
|
|
234
|
+
try {
|
|
235
|
+
return await runSeedersCore(
|
|
236
|
+
seeders,
|
|
237
|
+
{
|
|
238
|
+
context: () => config.context({ faker, step, expect }),
|
|
239
|
+
beforeSeeder: (name, { fresh }) => {
|
|
240
|
+
if (fakerSeed !== undefined) faker.seed(fakerSeed + hashName(name))
|
|
241
|
+
return setSeederName(name, { fresh })
|
|
242
|
+
},
|
|
243
|
+
target: config.target,
|
|
244
|
+
onBeforeAll: config.onBeforeAll,
|
|
245
|
+
onAfterAll: config.onAfterAll,
|
|
246
|
+
},
|
|
247
|
+
options,
|
|
248
|
+
)
|
|
249
|
+
} finally {
|
|
250
|
+
setSeedingActive(false)
|
|
233
251
|
}
|
|
234
|
-
|
|
235
|
-
return runSeedersCore(seeders, internalConfig, options)
|
|
236
252
|
}
|
|
237
253
|
|
|
238
|
-
/**
|
|
239
|
-
* Run seeders as a CLI command.
|
|
240
|
-
* Handles help flag, dry-run flag, prints banner with seeder count, runs seeders, and exits with proper code.
|
|
241
|
-
*
|
|
242
|
-
* @param seeders - Array of seeders to run
|
|
243
|
-
* @param options - Optional CLI options
|
|
244
|
-
*/
|
|
245
254
|
const runSeedersCli = async (
|
|
246
255
|
seeders: readonly SeederAny[],
|
|
247
|
-
options?: { name?: string },
|
|
256
|
+
options?: { name?: string; argv?: readonly string[] },
|
|
248
257
|
): Promise<void> => {
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
// Show help if requested
|
|
253
|
-
if (args.includes('--help') || args.includes('-h')) {
|
|
254
|
-
console.log(`
|
|
255
|
-
${colorize('Usage:', 'cyan', 'bold')} bun run seed [options]
|
|
256
|
-
|
|
257
|
-
${colorize('Options:', 'white', 'bold')}
|
|
258
|
-
--help, -h Show this help message
|
|
259
|
-
--dry-run Show execution order without running seeders
|
|
260
|
-
|
|
261
|
-
Runs all ${seeders.length} seeders in dependency order.
|
|
262
|
-
`)
|
|
263
|
-
process.exit(0)
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
// Dry run: show execution order without running
|
|
267
|
-
if (args.includes('--dry-run')) {
|
|
268
|
-
console.log(
|
|
269
|
-
`\n${colorize('🌱 Dry Run', 'cyan', 'bold')} ${colorize(`(${seeders.length} seeders)`, 'dim')}\n`,
|
|
270
|
-
)
|
|
271
|
-
|
|
272
|
-
// Create a map for quick seeder lookup
|
|
273
|
-
const seederMap = new Map<string, SeederAny>()
|
|
274
|
-
for (const seeder of seeders) {
|
|
275
|
-
seederMap.set(seeder.name, seeder)
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
try {
|
|
279
|
-
const order = resolveDependencyOrder(seeders)
|
|
280
|
-
console.log(colorize('Execution order:', 'white', 'bold'))
|
|
281
|
-
for (let i = 0; i < order.length; i++) {
|
|
282
|
-
const seederName = order[i]
|
|
283
|
-
if (!seederName) continue
|
|
284
|
-
const seeder = seederMap.get(seederName)
|
|
285
|
-
const deps = seeder?.dependsOn ?? []
|
|
286
|
-
const depsStr =
|
|
287
|
-
deps.length > 0 ? ` ${colorize(`(depends on: ${deps.join(', ')})`, 'dim')}` : ''
|
|
288
|
-
console.log(` ${i + 1}. ${seederName}${depsStr}`)
|
|
289
|
-
}
|
|
290
|
-
console.log('')
|
|
291
|
-
process.exit(0)
|
|
292
|
-
} catch (error) {
|
|
293
|
-
console.error(colorize('Failed to resolve dependencies:', 'red'))
|
|
294
|
-
console.error(` ${error instanceof Error ? error.message : String(error)}`)
|
|
295
|
-
process.exit(1)
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
console.log(
|
|
300
|
-
`\n${colorize(`🌱 Running ${name}`, 'cyan', 'bold')} ${colorize(`(${seeders.length} seeders)`, 'dim')}\n`,
|
|
301
|
-
)
|
|
302
|
-
const { success } = await runSeeders(seeders, {
|
|
303
|
-
printResults: true,
|
|
304
|
-
verbose: true,
|
|
258
|
+
const code = await runCli(seeders, runSeeders, {
|
|
259
|
+
name: options?.name ?? 'Seeders',
|
|
260
|
+
argv: options?.argv ?? process.argv.slice(2),
|
|
305
261
|
})
|
|
306
|
-
process.exit(
|
|
262
|
+
process.exit(code)
|
|
307
263
|
}
|
|
308
264
|
|
|
309
265
|
return {
|
|
@@ -313,3 +269,13 @@ Runs all ${seeders.length} seeders in dependency order.
|
|
|
313
269
|
isSeedingActive,
|
|
314
270
|
}
|
|
315
271
|
}
|
|
272
|
+
|
|
273
|
+
/** Small stable string hash (FNV-1a), so a seeder's faker seed depends only on its name */
|
|
274
|
+
function hashName(name: string): number {
|
|
275
|
+
let hash = 0x811c9dc5
|
|
276
|
+
for (let i = 0; i < name.length; i++) {
|
|
277
|
+
hash ^= name.charCodeAt(i)
|
|
278
|
+
hash = Math.imul(hash, 0x01000193)
|
|
279
|
+
}
|
|
280
|
+
return hash >>> 0
|
|
281
|
+
}
|
|
@@ -20,19 +20,46 @@ export class MissingDependencyError extends Error {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Error thrown when two seeders share a name
|
|
25
|
+
*/
|
|
26
|
+
export class DuplicateSeederError extends Error {
|
|
27
|
+
constructor(name: string) {
|
|
28
|
+
super(`Two seeders are named "${name}"; seeder names must be unique`)
|
|
29
|
+
this.name = 'DuplicateSeederError'
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Error thrown when a seeder is selected by a name that is not registered
|
|
35
|
+
*/
|
|
36
|
+
export class UnknownSeederError extends Error {
|
|
37
|
+
constructor(name: string, known: readonly string[]) {
|
|
38
|
+
super(`Unknown seeder "${name}". Registered seeders: ${known.join(', ') || '(none)'}`)
|
|
39
|
+
this.name = 'UnknownSeederError'
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
23
43
|
/**
|
|
24
44
|
* Resolves the execution order of seeders based on their dependencies
|
|
25
45
|
* using Kahn's algorithm (topological sort)
|
|
26
46
|
*
|
|
27
47
|
* @param seeders - Array of seeders to sort
|
|
48
|
+
* @param only - Optional names to select: returns them and everything they depend on
|
|
28
49
|
* @returns Array of seeder names in execution order
|
|
50
|
+
* @throws {DuplicateSeederError} If two seeders share a name
|
|
51
|
+
* @throws {UnknownSeederError} If a name in `only` is not registered
|
|
29
52
|
* @throws {CircularDependencyError} If circular dependencies are detected
|
|
30
53
|
* @throws {MissingDependencyError} If a seeder depends on a non-existent seeder
|
|
31
54
|
*/
|
|
32
|
-
export function resolveDependencyOrder(
|
|
55
|
+
export function resolveDependencyOrder(
|
|
56
|
+
seeders: readonly SeederAny[],
|
|
57
|
+
only?: readonly string[],
|
|
58
|
+
): string[] {
|
|
33
59
|
// Build a map of seeder names for quick lookup
|
|
34
60
|
const seederMap = new Map<string, SeederAny>()
|
|
35
61
|
for (const seeder of seeders) {
|
|
62
|
+
if (seederMap.has(seeder.name)) throw new DuplicateSeederError(seeder.name)
|
|
36
63
|
seederMap.set(seeder.name, seeder)
|
|
37
64
|
}
|
|
38
65
|
|
|
@@ -99,7 +126,20 @@ export function resolveDependencyOrder(seeders: readonly SeederAny[]): string[]
|
|
|
99
126
|
throw new CircularDependencyError(cycle)
|
|
100
127
|
}
|
|
101
128
|
|
|
102
|
-
return result
|
|
129
|
+
if (!only) return result
|
|
130
|
+
|
|
131
|
+
// Keep the selected seeders and their transitive dependencies, in execution order
|
|
132
|
+
const selected = new Set<string>()
|
|
133
|
+
const visit = (name: string) => {
|
|
134
|
+
if (selected.has(name)) return
|
|
135
|
+
selected.add(name)
|
|
136
|
+
for (const dep of seederMap.get(name)?.dependsOn ?? []) visit(dep)
|
|
137
|
+
}
|
|
138
|
+
for (const name of only) {
|
|
139
|
+
if (!seederMap.has(name)) throw new UnknownSeederError(name, [...seederMap.keys()])
|
|
140
|
+
visit(name)
|
|
141
|
+
}
|
|
142
|
+
return result.filter((name) => selected.has(name))
|
|
103
143
|
}
|
|
104
144
|
|
|
105
145
|
/**
|
package/src/formatting.ts
CHANGED
|
@@ -110,22 +110,38 @@ export function printResultsTable(results: SeederResult[]): void {
|
|
|
110
110
|
console.log('')
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
+
const escapeAnnotation = (text: string) =>
|
|
114
|
+
text.replace(/%/g, '%25').replace(/\r/g, '%0D').replace(/\n/g, '%0A')
|
|
115
|
+
|
|
113
116
|
/**
|
|
114
|
-
* Print error details for failed seeders
|
|
117
|
+
* Print error details for failed seeders: the failing step, the full error (message,
|
|
118
|
+
* stack, cause and own fields via Bun.inspect) and a hint when cached steps ran first.
|
|
119
|
+
* Under GitHub Actions, also prints one `::error` annotation per failed seeder.
|
|
115
120
|
*/
|
|
116
121
|
export function printErrors(results: SeederResult[]): void {
|
|
117
122
|
const failed = results.filter((r) => r.status === 'failed')
|
|
118
123
|
|
|
119
124
|
for (const result of failed) {
|
|
120
|
-
|
|
125
|
+
const where = result.failedStep ? ` in step "${result.failedStep}"` : ''
|
|
126
|
+
console.log(colorize(`❌ Seeder "${result.name}" failed${where}:`, 'red'))
|
|
121
127
|
if (result.error) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
128
|
+
const details = Bun.inspect(result.error, { colors: Boolean(process.stdout.isTTY) })
|
|
129
|
+
for (const line of details.split('\n')) console.log(` ${line}`)
|
|
130
|
+
}
|
|
131
|
+
const cached = result.cachedSteps ?? []
|
|
132
|
+
if (cached.length > 0) {
|
|
133
|
+
console.log(
|
|
134
|
+
colorize(
|
|
135
|
+
` ${cached.length} earlier step(s) came from the cache: ${cached.join(', ')}. If they are stale, run this seeder fresh (CLI: --only ${result.name} --fresh).`,
|
|
136
|
+
'yellow',
|
|
137
|
+
),
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
if (process.env.GITHUB_ACTIONS === 'true') {
|
|
141
|
+
const title = `Seeder ${result.name} failed${where}`.replace(/[:,]/g, ' ')
|
|
142
|
+
console.log(
|
|
143
|
+
`::error title=${escapeAnnotation(title)}::${escapeAnnotation(result.error?.message ?? 'failed')}`,
|
|
144
|
+
)
|
|
129
145
|
}
|
|
130
146
|
console.log('')
|
|
131
147
|
}
|
|
@@ -165,9 +181,11 @@ export function printSeederComplete(durationMs: number, success: boolean): void
|
|
|
165
181
|
/**
|
|
166
182
|
* Print the initial banner when seeding starts
|
|
167
183
|
*/
|
|
168
|
-
export function printStartBanner(
|
|
169
|
-
const
|
|
170
|
-
|
|
184
|
+
export function printStartBanner(target?: { label: string; local: boolean }): void {
|
|
185
|
+
const targetText = target
|
|
186
|
+
? ` ${colorize(`→ ${target.label}`, 'dim')}${target.local ? '' : ` ${colorize('(remote, allowed)', 'yellow')}`}`
|
|
187
|
+
: ''
|
|
188
|
+
console.log(`\n${colorize('🌱 Running Seeders', 'cyan', 'bold')}${targetText}\n`)
|
|
171
189
|
}
|
|
172
190
|
|
|
173
191
|
/**
|