plantr 2.0.0 → 2.1.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 +23 -20
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +761 -262
- package/dist/src/cli.d.ts +13 -3
- package/dist/src/cli.d.ts.map +1 -0
- package/dist/src/create-seeder-instance.d.ts +55 -78
- package/dist/src/create-seeder-instance.d.ts.map +1 -0
- package/dist/src/define-seeder.d.ts +4 -3
- package/dist/src/define-seeder.d.ts.map +1 -0
- package/dist/src/dependency-resolver.d.ts +2 -1
- package/dist/src/dependency-resolver.d.ts.map +1 -0
- package/dist/src/formatting.d.ts +27 -13
- package/dist/src/formatting.d.ts.map +1 -0
- package/dist/src/lazy-faker.d.ts +12 -0
- package/dist/src/lazy-faker.d.ts.map +1 -0
- package/dist/src/run-seeders.d.ts +39 -22
- package/dist/src/run-seeders.d.ts.map +1 -0
- package/dist/src/seeding-state.d.ts +15 -4
- package/dist/src/seeding-state.d.ts.map +1 -0
- package/dist/src/step.d.ts +79 -22
- package/dist/src/step.d.ts.map +1 -0
- package/dist/src/types.d.ts +117 -34
- package/dist/src/types.d.ts.map +1 -0
- package/dist/state.d.ts +2 -1
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +10 -2
- package/index.ts +56 -23
- package/package.json +2 -1
- package/readme.md +89 -48
- package/src/cli.ts +154 -21
- package/src/create-seeder-instance.ts +225 -110
- package/src/define-seeder.ts +14 -7
- package/src/dependency-resolver.ts +17 -28
- package/src/formatting.ts +121 -36
- package/src/lazy-faker.ts +46 -0
- package/src/run-seeders.ts +336 -115
- package/src/seeding-state.ts +27 -5
- package/src/step.ts +208 -64
- package/src/types.ts +169 -37
- package/state.ts +1 -1
package/src/run-seeders.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { resolveDependencyOrder, UnknownSeederError } from './dependency-resolver'
|
|
1
|
+
import { resolveDependencyOrder, UnknownSeederError } from './dependency-resolver.js'
|
|
2
2
|
import {
|
|
3
|
+
printConcurrentComplete,
|
|
4
|
+
printConcurrentSkipped,
|
|
3
5
|
printErrors,
|
|
4
6
|
printResultsTable,
|
|
5
7
|
printSeederComplete,
|
|
@@ -7,25 +9,55 @@ import {
|
|
|
7
9
|
printSeederSkipped,
|
|
8
10
|
printStartBanner,
|
|
9
11
|
printSummary,
|
|
10
|
-
} from './formatting'
|
|
11
|
-
import
|
|
12
|
-
import type {
|
|
12
|
+
} from './formatting.js'
|
|
13
|
+
import { enterSeeding, exitSeeding } from './seeding-state.js'
|
|
14
|
+
import type { StepRecord } from './step.js'
|
|
15
|
+
import type {
|
|
16
|
+
MaybePromise,
|
|
17
|
+
RunSeedersOptions,
|
|
18
|
+
RunSeedersResult,
|
|
19
|
+
SeederAny,
|
|
20
|
+
SeederResult,
|
|
21
|
+
SeederResultFor,
|
|
22
|
+
} from './types.js'
|
|
23
|
+
|
|
24
|
+
/** Parsed description of a database target URL (without credentials) */
|
|
25
|
+
export type TargetDescription = {
|
|
26
|
+
label: string
|
|
27
|
+
local: boolean
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Per-seeder scope handle returned by `InternalSeederConfig.runSeederScope` */
|
|
31
|
+
export type SeederScopeHandle = {
|
|
32
|
+
record?: StepRecord | undefined
|
|
33
|
+
run: <T>(fn: () => Promise<T>) => Promise<T>
|
|
34
|
+
}
|
|
13
35
|
|
|
14
36
|
/**
|
|
15
37
|
* Internal configuration used by the runner.
|
|
16
|
-
* This is created by createSeederInstance and includes the wrapped context factory.
|
|
38
|
+
* This is created by `createSeederInstance` and includes the wrapped context factory.
|
|
17
39
|
*/
|
|
18
40
|
export type InternalSeederConfig<TContext> = {
|
|
19
41
|
/** Factory function that creates the full context; called once, after onBeforeAll */
|
|
20
|
-
context: () =>
|
|
21
|
-
/** Called
|
|
22
|
-
|
|
42
|
+
context: () => MaybePromise<TContext>
|
|
43
|
+
/** Called once after onBeforeAll to preload step cache entries (when cache.getAll is configured) */
|
|
44
|
+
preloadCache?: (() => MaybePromise<void>) | undefined
|
|
45
|
+
/** Called once before onAfterAll to flush buffered step cache writes (when cache.setMany is configured) */
|
|
46
|
+
flushCache?: (() => MaybePromise<void>) | undefined
|
|
47
|
+
/** Called before each seeder runs (legacy sequential hook); may return the record its steps fill */
|
|
48
|
+
beforeSeeder?: ((name: string, options: { fresh: boolean }) => StepRecord | undefined) | undefined
|
|
49
|
+
/** Concurrency-safe scope wrapper for running a seeder (used by createSeederInstance) */
|
|
50
|
+
createSeederScope?:
|
|
51
|
+
| ((options: { name: string; fresh: boolean; concurrent: boolean }) => SeederScopeHandle)
|
|
52
|
+
| undefined
|
|
53
|
+
/** Banner title (default: `'Seeders'`) */
|
|
54
|
+
bannerName?: string | undefined
|
|
23
55
|
/** Database URL the run writes to; non-local hosts are refused unless allowRemote */
|
|
24
|
-
target?: () => string | undefined
|
|
56
|
+
target?: (() => string | undefined) | undefined
|
|
25
57
|
/** Called before the context is created */
|
|
26
|
-
onBeforeAll?: () => void |
|
|
58
|
+
onBeforeAll?: (() => MaybePromise<void>) | undefined
|
|
27
59
|
/** Called after all seeders complete, also when a seeder or the context factory throws */
|
|
28
|
-
onAfterAll?: () => void |
|
|
60
|
+
onAfterAll?: ((ctx: TContext | undefined) => MaybePromise<void>) | undefined
|
|
29
61
|
}
|
|
30
62
|
|
|
31
63
|
/**
|
|
@@ -62,7 +94,7 @@ const isLocalHost = (hostPort: string): boolean => {
|
|
|
62
94
|
* URL's host, as libpq does. Hand-parsed so driver formats `URL` rejects still work
|
|
63
95
|
* (several hosts, `sqlserver://host;key=value`, sockets with credentials).
|
|
64
96
|
*/
|
|
65
|
-
export function describeTarget(target: string):
|
|
97
|
+
export function describeTarget(target: string): TargetDescription {
|
|
66
98
|
if (/^file:/i.test(target)) return { label: target.replace(/[?#].*$/, ''), local: true }
|
|
67
99
|
const scheme = /^[a-z][\w+.-]*:\/\//i.exec(target)
|
|
68
100
|
if (!scheme) return { label: 'a target that is not a URL', local: false }
|
|
@@ -76,29 +108,36 @@ export function describeTarget(target: string): { label: string; local: boolean
|
|
|
76
108
|
const [authority = '', path = ''] = authorityAndPath.split(/(?=[/;])/, 2)
|
|
77
109
|
let hosts = authority.split(',').filter(Boolean)
|
|
78
110
|
const hostParam = /[?&;]host=([^&;#]*)/i.exec(`${query}${path.startsWith(';') ? path : ''}`)?.[1]
|
|
79
|
-
if (hostParam)
|
|
111
|
+
if (hostParam) {
|
|
112
|
+
try {
|
|
113
|
+
hosts = decodeURIComponent(hostParam).split(',').filter(Boolean)
|
|
114
|
+
} catch {
|
|
115
|
+
return { label: 'a target with an invalid host parameter', local: false }
|
|
116
|
+
}
|
|
117
|
+
}
|
|
80
118
|
|
|
81
119
|
const label = `${hosts.join(',') || 'local socket'}${path.startsWith('/') ? path : ''}`
|
|
82
120
|
return { label, local: hosts.every(isLocalHost) }
|
|
83
121
|
}
|
|
84
122
|
|
|
85
123
|
/**
|
|
86
|
-
* Core seeder runner - executes seeders in dependency order
|
|
124
|
+
* Core seeder runner - executes seeders in dependency order (sequentially or in parallel)
|
|
87
125
|
*
|
|
88
126
|
* @param seeders - Array of seeders to run
|
|
89
127
|
* @param config - Instance configuration (context factory, lifecycle hooks)
|
|
90
|
-
* @param options - Runner options (verbose output, etc.)
|
|
91
|
-
* @returns Object containing results and
|
|
92
|
-
* @throws Dependency errors, `UnknownSeederError
|
|
128
|
+
* @param options - Runner options (verbose output, concurrency, etc.)
|
|
129
|
+
* @returns Object containing `success`, `results`, and `outputs`
|
|
130
|
+
* @throws Dependency errors, `UnknownSeederError`, `SeedTargetError`, or `RangeError` (invalid concurrency), before any hook runs
|
|
93
131
|
*/
|
|
94
|
-
export async function runSeedersCore<
|
|
95
|
-
|
|
132
|
+
export async function runSeedersCore<
|
|
133
|
+
TContext,
|
|
134
|
+
const TSeeders extends readonly SeederAny[] = readonly SeederAny[],
|
|
135
|
+
const TOptions extends RunSeedersOptions = RunSeedersOptions,
|
|
136
|
+
>(
|
|
137
|
+
seeders: TSeeders,
|
|
96
138
|
config: InternalSeederConfig<TContext>,
|
|
97
|
-
options:
|
|
98
|
-
): Promise<{
|
|
99
|
-
results: SeederResult[]
|
|
100
|
-
success: boolean
|
|
101
|
-
}> {
|
|
139
|
+
options: TOptions = {} as TOptions,
|
|
140
|
+
): Promise<RunSeedersResult<TSeeders, TOptions>> {
|
|
102
141
|
const {
|
|
103
142
|
printResults = true,
|
|
104
143
|
verbose = true,
|
|
@@ -106,17 +145,30 @@ export async function runSeedersCore<TContext>(
|
|
|
106
145
|
only,
|
|
107
146
|
fresh = false,
|
|
108
147
|
allowRemote = false,
|
|
148
|
+
concurrency = 1,
|
|
109
149
|
} = options
|
|
110
150
|
|
|
111
|
-
|
|
151
|
+
if (
|
|
152
|
+
Number.isNaN(concurrency) ||
|
|
153
|
+
concurrency < 1 ||
|
|
154
|
+
(Number.isFinite(concurrency) && !Number.isInteger(concurrency))
|
|
155
|
+
) {
|
|
156
|
+
throw new RangeError(
|
|
157
|
+
`Invalid concurrency (${String(concurrency)}): expected a positive integer or Infinity`,
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Validate everything before setting the seeding flag or touching the database
|
|
112
162
|
const orderedNames = resolveDependencyOrder(seeders, only)
|
|
113
163
|
|
|
114
164
|
const seederMap = new Map<string, SeederAny>()
|
|
115
165
|
for (const seeder of seeders) {
|
|
116
166
|
seederMap.set(seeder.name, seeder)
|
|
117
167
|
}
|
|
118
|
-
|
|
119
|
-
|
|
168
|
+
|
|
169
|
+
const freshSet = Array.isArray(fresh) ? new Set<string>(fresh) : undefined
|
|
170
|
+
if (freshSet) {
|
|
171
|
+
for (const name of freshSet) {
|
|
120
172
|
if (!seederMap.has(name)) throw new UnknownSeederError(name, [...seederMap.keys()])
|
|
121
173
|
}
|
|
122
174
|
}
|
|
@@ -127,125 +179,294 @@ export async function runSeedersCore<TContext>(
|
|
|
127
179
|
throw new SeedTargetError(target.label)
|
|
128
180
|
}
|
|
129
181
|
|
|
130
|
-
|
|
131
|
-
printStartBanner(target)
|
|
132
|
-
}
|
|
182
|
+
const isFresh = (name: string): boolean => fresh === true || (freshSet?.has(name) ?? false)
|
|
133
183
|
|
|
134
|
-
// Initialize results
|
|
184
|
+
// Initialize results in deterministic topological order
|
|
135
185
|
const results: SeederResult[] = orderedNames.map((name) => ({
|
|
136
186
|
name,
|
|
137
187
|
status: 'pending',
|
|
138
188
|
}))
|
|
189
|
+
const resultIndex = new Map<string, number>()
|
|
190
|
+
for (let i = 0; i < orderedNames.length; i++) {
|
|
191
|
+
resultIndex.set(orderedNames[i]!, i)
|
|
192
|
+
}
|
|
139
193
|
|
|
140
|
-
// Store outputs for dependency injection
|
|
194
|
+
// Store outputs for dependency injection and return value
|
|
141
195
|
const outputs: Record<string, unknown> = {}
|
|
142
196
|
|
|
143
|
-
// Track failed seeders to skip their dependents
|
|
197
|
+
// Track failed (and skipped) seeders to skip their dependents
|
|
144
198
|
const failedSeeders = new Set<string>()
|
|
199
|
+
const concurrent = concurrency > 1
|
|
145
200
|
|
|
146
|
-
|
|
147
|
-
await config.onBeforeAll()
|
|
148
|
-
}
|
|
149
|
-
|
|
201
|
+
enterSeeding()
|
|
150
202
|
try {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
// TypeScript safety - should never happen since we iterate within bounds
|
|
160
|
-
if (!name || !result) continue
|
|
161
|
-
|
|
162
|
-
const seeder = seederMap.get(name)
|
|
163
|
-
if (!seeder) continue
|
|
164
|
-
|
|
165
|
-
// Check if any dependencies have failed (only relevant when continueOnFailure is true)
|
|
166
|
-
if (continueOnFailure) {
|
|
167
|
-
const hasFailedDep = seeder.dependsOn.some((dep) => failedSeeders.has(dep))
|
|
168
|
-
if (hasFailedDep) {
|
|
169
|
-
result.status = 'skipped'
|
|
170
|
-
failedSeeders.add(name) // Treat skipped as failed for dependency chain
|
|
171
|
-
if (verbose) {
|
|
172
|
-
printSeederHeader(name, i, orderedNames.length, seeder.description)
|
|
173
|
-
printSeederSkipped(seeder.dependsOn.filter((d) => failedSeeders.has(d)))
|
|
174
|
-
}
|
|
175
|
-
continue
|
|
176
|
-
}
|
|
177
|
-
}
|
|
203
|
+
if (verbose) {
|
|
204
|
+
printStartBanner({
|
|
205
|
+
name: config.bannerName,
|
|
206
|
+
count: orderedNames.length,
|
|
207
|
+
target,
|
|
208
|
+
concurrency,
|
|
209
|
+
})
|
|
210
|
+
}
|
|
178
211
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
212
|
+
const runStartTime = performance.now()
|
|
213
|
+
|
|
214
|
+
if (config.onBeforeAll) {
|
|
215
|
+
await config.onBeforeAll()
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
let ctx: TContext | undefined
|
|
219
|
+
try {
|
|
220
|
+
if (config.preloadCache) {
|
|
221
|
+
await config.preloadCache()
|
|
182
222
|
}
|
|
183
223
|
|
|
184
|
-
//
|
|
185
|
-
|
|
224
|
+
// Create context once for all seeders
|
|
225
|
+
ctx = await config.context()
|
|
226
|
+
const activeCtx = ctx
|
|
227
|
+
|
|
228
|
+
const executeSeeder = async (name: string): Promise<boolean> => {
|
|
229
|
+
const idx = resultIndex.get(name)
|
|
230
|
+
const seeder = seederMap.get(name)
|
|
231
|
+
if (idx === undefined || !seeder) return true
|
|
232
|
+
|
|
233
|
+
const scopeHandle: SeederScopeHandle = config.createSeederScope
|
|
234
|
+
? config.createSeederScope({ name, fresh: isFresh(name), concurrent })
|
|
235
|
+
: {
|
|
236
|
+
record: config.beforeSeeder?.(name, { fresh: isFresh(name) }),
|
|
237
|
+
run: (fn) => fn(),
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const deps: Record<string, unknown> = {}
|
|
241
|
+
for (const depName of seeder.dependsOn) {
|
|
242
|
+
deps[depName] = outputs[depName]
|
|
243
|
+
}
|
|
186
244
|
|
|
187
|
-
|
|
188
|
-
fresh: fresh === true || (Array.isArray(fresh) && fresh.includes(name)),
|
|
189
|
-
})
|
|
245
|
+
const startTime = performance.now()
|
|
190
246
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
deps[depName] = outputs[depName]
|
|
195
|
-
}
|
|
247
|
+
try {
|
|
248
|
+
const output = await scopeHandle.run(async () => seeder.run(activeCtx, deps))
|
|
249
|
+
const durationMs = Math.round(performance.now() - startTime)
|
|
196
250
|
|
|
197
|
-
|
|
251
|
+
outputs[name] = output
|
|
252
|
+
results[idx] = {
|
|
253
|
+
name,
|
|
254
|
+
status: 'completed',
|
|
255
|
+
durationMs,
|
|
256
|
+
output,
|
|
257
|
+
cachedSteps: scopeHandle.record?.cachedSteps ?? [],
|
|
258
|
+
}
|
|
198
259
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
260
|
+
if (verbose) {
|
|
261
|
+
if (concurrent) {
|
|
262
|
+
printConcurrentComplete(name, durationMs, true)
|
|
263
|
+
} else {
|
|
264
|
+
printSeederComplete(durationMs, true)
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return true
|
|
268
|
+
} catch (error) {
|
|
269
|
+
const durationMs = Math.round(performance.now() - startTime)
|
|
270
|
+
const err = error instanceof Error ? error : new Error(String(error))
|
|
271
|
+
const failedStep =
|
|
272
|
+
scopeHandle.record?.failed && scopeHandle.record.failed.error === error
|
|
273
|
+
? scopeHandle.record.failed.step
|
|
274
|
+
: undefined
|
|
275
|
+
|
|
276
|
+
results[idx] = {
|
|
277
|
+
name,
|
|
278
|
+
status: 'failed',
|
|
279
|
+
durationMs,
|
|
280
|
+
error: err,
|
|
281
|
+
...(failedStep !== undefined ? { failedStep } : {}),
|
|
282
|
+
cachedSteps: scopeHandle.record?.cachedSteps ?? [],
|
|
283
|
+
}
|
|
284
|
+
failedSeeders.add(name)
|
|
202
285
|
|
|
203
|
-
|
|
204
|
-
|
|
286
|
+
if (verbose) {
|
|
287
|
+
if (concurrent) {
|
|
288
|
+
printConcurrentComplete(name, durationMs, false)
|
|
289
|
+
} else {
|
|
290
|
+
printSeederComplete(durationMs, false)
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return false
|
|
294
|
+
}
|
|
295
|
+
}
|
|
205
296
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
297
|
+
if (!concurrent) {
|
|
298
|
+
// Sequential execution in topological order
|
|
299
|
+
for (let i = 0; i < orderedNames.length; i++) {
|
|
300
|
+
const name = orderedNames[i]
|
|
301
|
+
if (!name) continue
|
|
302
|
+
const seeder = seederMap.get(name)
|
|
303
|
+
if (!seeder) continue
|
|
304
|
+
|
|
305
|
+
if (continueOnFailure) {
|
|
306
|
+
const failedDeps = seeder.dependsOn.filter((dep) => failedSeeders.has(dep))
|
|
307
|
+
if (failedDeps.length > 0) {
|
|
308
|
+
results[i] = { name, status: 'skipped', failedDeps }
|
|
309
|
+
failedSeeders.add(name)
|
|
310
|
+
if (verbose) {
|
|
311
|
+
printSeederHeader(name, i, orderedNames.length, seeder.description)
|
|
312
|
+
printSeederSkipped(failedDeps)
|
|
313
|
+
}
|
|
314
|
+
continue
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (verbose) {
|
|
319
|
+
printSeederHeader(name, i, orderedNames.length, seeder.description)
|
|
320
|
+
}
|
|
211
321
|
|
|
212
|
-
|
|
213
|
-
|
|
322
|
+
const ok = await executeSeeder(name)
|
|
323
|
+
if (!ok && !continueOnFailure) {
|
|
324
|
+
break
|
|
325
|
+
}
|
|
214
326
|
}
|
|
215
|
-
}
|
|
216
|
-
//
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
327
|
+
} else if (orderedNames.length > 0) {
|
|
328
|
+
// Parallel DAG worker pool with critical-path (longest-downstream-chain-first) scheduling
|
|
329
|
+
const remainingDeps = new Map<string, number>()
|
|
330
|
+
const dependents = new Map<string, string[]>()
|
|
331
|
+
|
|
332
|
+
for (const name of orderedNames) {
|
|
333
|
+
remainingDeps.set(name, 0)
|
|
334
|
+
dependents.set(name, [])
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
for (const name of orderedNames) {
|
|
338
|
+
const seeder = seederMap.get(name)
|
|
339
|
+
if (!seeder) continue
|
|
340
|
+
const uniqueDeps = new Set(seeder.dependsOn)
|
|
341
|
+
remainingDeps.set(name, uniqueDeps.size)
|
|
342
|
+
for (const dep of uniqueDeps) {
|
|
343
|
+
dependents.get(dep)?.push(name)
|
|
344
|
+
}
|
|
223
345
|
}
|
|
224
|
-
failedSeeders.add(name)
|
|
225
346
|
|
|
226
|
-
|
|
227
|
-
|
|
347
|
+
// Compute critical-path height (longest chain of downstream dependents) in reverse topo order
|
|
348
|
+
const criticalHeight = new Map<string, number>()
|
|
349
|
+
for (let i = orderedNames.length - 1; i >= 0; i--) {
|
|
350
|
+
const name = orderedNames[i]!
|
|
351
|
+
let maxHeight = 0
|
|
352
|
+
for (const dependent of dependents.get(name) ?? []) {
|
|
353
|
+
const h = (criticalHeight.get(dependent) ?? 0) + 1
|
|
354
|
+
if (h > maxHeight) maxHeight = h
|
|
355
|
+
}
|
|
356
|
+
criticalHeight.set(name, maxHeight)
|
|
228
357
|
}
|
|
229
358
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
359
|
+
const ready: string[] = []
|
|
360
|
+
for (const name of orderedNames) {
|
|
361
|
+
if (remainingDeps.get(name) === 0) {
|
|
362
|
+
ready.push(name)
|
|
363
|
+
}
|
|
233
364
|
}
|
|
365
|
+
|
|
366
|
+
const popHighestPriority = (): string => {
|
|
367
|
+
let bestIdx = 0
|
|
368
|
+
let bestHeight = criticalHeight.get(ready[0]!) ?? 0
|
|
369
|
+
let bestOrder = resultIndex.get(ready[0]!) ?? 0
|
|
370
|
+
for (let i = 1; i < ready.length; i++) {
|
|
371
|
+
const candidate = ready[i]!
|
|
372
|
+
const h = criticalHeight.get(candidate) ?? 0
|
|
373
|
+
const order = resultIndex.get(candidate) ?? 0
|
|
374
|
+
if (h > bestHeight || (h === bestHeight && order < bestOrder)) {
|
|
375
|
+
bestIdx = i
|
|
376
|
+
bestHeight = h
|
|
377
|
+
bestOrder = order
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return ready.splice(bestIdx, 1)[0]!
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
await new Promise<void>((resolve) => {
|
|
384
|
+
let inFlight = 0
|
|
385
|
+
let stopped = false
|
|
386
|
+
|
|
387
|
+
const markSettled = (settledName: string) => {
|
|
388
|
+
const deps = dependents.get(settledName)
|
|
389
|
+
if (!deps) return
|
|
390
|
+
for (const dependent of deps) {
|
|
391
|
+
const next = (remainingDeps.get(dependent) ?? 1) - 1
|
|
392
|
+
remainingDeps.set(dependent, next)
|
|
393
|
+
if (next !== 0 || stopped) continue
|
|
394
|
+
|
|
395
|
+
const seeder = seederMap.get(dependent)
|
|
396
|
+
if (!seeder) continue
|
|
397
|
+
|
|
398
|
+
const failedDeps = seeder.dependsOn.filter((d) => failedSeeders.has(d))
|
|
399
|
+
if (failedDeps.length > 0) {
|
|
400
|
+
const idx = resultIndex.get(dependent)
|
|
401
|
+
if (idx !== undefined) {
|
|
402
|
+
results[idx] = { name: dependent, status: 'skipped', failedDeps }
|
|
403
|
+
}
|
|
404
|
+
failedSeeders.add(dependent)
|
|
405
|
+
if (verbose) {
|
|
406
|
+
printConcurrentSkipped(dependent, failedDeps)
|
|
407
|
+
}
|
|
408
|
+
markSettled(dependent)
|
|
409
|
+
} else {
|
|
410
|
+
ready.push(dependent)
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
const schedule = () => {
|
|
416
|
+
while (!stopped && inFlight < concurrency && ready.length > 0) {
|
|
417
|
+
const name = popHighestPriority()
|
|
418
|
+
inFlight++
|
|
419
|
+
void executeSeeder(name).then((ok) => {
|
|
420
|
+
inFlight--
|
|
421
|
+
if (!ok && !continueOnFailure) {
|
|
422
|
+
stopped = true
|
|
423
|
+
} else {
|
|
424
|
+
markSettled(name)
|
|
425
|
+
}
|
|
426
|
+
schedule()
|
|
427
|
+
})
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
if (inFlight === 0 && (stopped || ready.length === 0)) {
|
|
431
|
+
resolve()
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
schedule()
|
|
436
|
+
})
|
|
234
437
|
}
|
|
235
|
-
}
|
|
236
438
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
439
|
+
if (printResults) {
|
|
440
|
+
printResultsTable(results)
|
|
441
|
+
printErrors(results)
|
|
442
|
+
printSummary(results, performance.now() - runStartTime, concurrency)
|
|
443
|
+
}
|
|
444
|
+
} finally {
|
|
445
|
+
try {
|
|
446
|
+
if (config.flushCache) {
|
|
447
|
+
await config.flushCache()
|
|
448
|
+
}
|
|
449
|
+
} finally {
|
|
450
|
+
if (config.onAfterAll) {
|
|
451
|
+
await config.onAfterAll(ctx)
|
|
452
|
+
}
|
|
453
|
+
}
|
|
242
454
|
}
|
|
243
455
|
} finally {
|
|
244
|
-
|
|
245
|
-
await config.onAfterAll()
|
|
246
|
-
}
|
|
456
|
+
exitSeeding()
|
|
247
457
|
}
|
|
248
458
|
|
|
249
|
-
const
|
|
250
|
-
|
|
459
|
+
const hasFailure = results.some((r) => r.status === 'failed')
|
|
460
|
+
if (hasFailure) {
|
|
461
|
+
return {
|
|
462
|
+
success: false,
|
|
463
|
+
results: results as SeederResultFor<TSeeders>[],
|
|
464
|
+
outputs: outputs as Partial<Record<string, unknown>>,
|
|
465
|
+
} as RunSeedersResult<TSeeders, TOptions>
|
|
466
|
+
}
|
|
467
|
+
return {
|
|
468
|
+
success: true,
|
|
469
|
+
results: results as SeederResultFor<TSeeders>[],
|
|
470
|
+
outputs,
|
|
471
|
+
} as RunSeedersResult<TSeeders, TOptions>
|
|
251
472
|
}
|
package/src/seeding-state.ts
CHANGED
|
@@ -8,12 +8,13 @@
|
|
|
8
8
|
* - Syncing to external services
|
|
9
9
|
*
|
|
10
10
|
* The flag lives on `globalThis`, so `plantr` and `plantr/state` share it even when
|
|
11
|
-
* they are loaded as separate bundles.
|
|
12
|
-
*
|
|
11
|
+
* they are loaded as separate bundles. Overlapping runs are reference-counted so
|
|
12
|
+
* one finishing run does not clear the flag while another is still active.
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
const key = Symbol.for('plantr.seedingActive')
|
|
16
|
-
const
|
|
16
|
+
const countKey = Symbol.for('plantr.seedingCount')
|
|
17
|
+
const state = globalThis as { [key]?: boolean; [countKey]?: number }
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
20
|
* Check if seeding is currently active
|
|
@@ -23,9 +24,30 @@ export function isSeedingActive(): boolean {
|
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
|
-
* Set the seeding state.
|
|
27
|
-
*
|
|
27
|
+
* Set the seeding state directly.
|
|
28
|
+
* Resets the active run count to 1 (true) or 0 (false).
|
|
28
29
|
*/
|
|
29
30
|
export function setSeedingActive(active: boolean): void {
|
|
31
|
+
state[countKey] = active ? 1 : 0
|
|
30
32
|
state[key] = active
|
|
31
33
|
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Increment the active seeding run counter and mark seeding active.
|
|
37
|
+
* Called automatically by `runSeeders` when execution begins.
|
|
38
|
+
*/
|
|
39
|
+
export function enterSeeding(): void {
|
|
40
|
+
const next = (state[countKey] ?? 0) + 1
|
|
41
|
+
state[countKey] = next
|
|
42
|
+
state[key] = true
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Decrement the active seeding run counter and clear the flag when no runs remain.
|
|
47
|
+
* Called automatically by `runSeeders` when execution finishes.
|
|
48
|
+
*/
|
|
49
|
+
export function exitSeeding(): void {
|
|
50
|
+
const next = Math.max(0, (state[countKey] ?? 0) - 1)
|
|
51
|
+
state[countKey] = next
|
|
52
|
+
state[key] = next > 0
|
|
53
|
+
}
|