mirror of
https://github.com/langgenius/dify.git
synced 2026-07-14 17:07:03 +08:00
Signed-off-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: 盐粒 Yanli <mail@yanli.one> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: zyssyz123 <916125788@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: 盐粒 Yanli <yanli@dify.ai> Co-authored-by: 林玮 (Jade Lin) <linw1995@icloud.com>
94 lines
2.0 KiB
TypeScript
94 lines
2.0 KiB
TypeScript
import { mkdir, writeFile } from 'node:fs/promises'
|
|
import path from 'node:path'
|
|
import { e2eDir } from '../scripts/common'
|
|
|
|
export type SeedStatus = 'blocked' | 'created' | 'skipped' | 'updated' | 'verified'
|
|
|
|
export type SeedResource = {
|
|
id?: string
|
|
kind: string
|
|
name: string
|
|
}
|
|
|
|
export type SeedResult = {
|
|
reason?: string
|
|
resource?: SeedResource
|
|
status: SeedStatus
|
|
title: string
|
|
}
|
|
|
|
export type SeedContext = {
|
|
dryRun: boolean
|
|
resources: Map<string, SeedResource>
|
|
}
|
|
|
|
export type SeedTask = {
|
|
id: string
|
|
run: (context: SeedContext) => Promise<SeedResult>
|
|
title: string
|
|
}
|
|
|
|
const reportDir = path.join(e2eDir, 'seed-report')
|
|
|
|
export const created = (title: string, resource?: SeedResource): SeedResult => ({
|
|
resource,
|
|
status: 'created',
|
|
title,
|
|
})
|
|
|
|
export const updated = (title: string, resource?: SeedResource): SeedResult => ({
|
|
resource,
|
|
status: 'updated',
|
|
title,
|
|
})
|
|
|
|
export const verified = (title: string, resource?: SeedResource): SeedResult => ({
|
|
resource,
|
|
status: 'verified',
|
|
title,
|
|
})
|
|
|
|
export const skipped = (title: string, reason: string): SeedResult => ({
|
|
reason,
|
|
status: 'skipped',
|
|
title,
|
|
})
|
|
|
|
export const blocked = (title: string, reason: string): SeedResult => ({
|
|
reason,
|
|
status: 'blocked',
|
|
title,
|
|
})
|
|
|
|
export async function runSeedTasks(tasks: SeedTask[], context: SeedContext) {
|
|
const results: SeedResult[] = []
|
|
|
|
for (const task of tasks) {
|
|
const result = await task.run(context)
|
|
results.push(result)
|
|
if (result.resource)
|
|
context.resources.set(task.id, result.resource)
|
|
|
|
const suffix = result.reason ? `: ${result.reason}` : ''
|
|
console.warn(`[seed] ${result.status.padEnd(8)} ${result.title}${suffix}`)
|
|
}
|
|
|
|
return results
|
|
}
|
|
|
|
export async function writeSeedReport(pack: string, results: SeedResult[]) {
|
|
await mkdir(reportDir, { recursive: true })
|
|
const reportPath = path.join(reportDir, `${pack}.json`)
|
|
await writeFile(
|
|
reportPath,
|
|
`${JSON.stringify({
|
|
generated_at: new Date().toISOString(),
|
|
pack,
|
|
results,
|
|
}, null, 2)}\n`,
|
|
'utf8',
|
|
)
|
|
|
|
return reportPath
|
|
}
|