| 2 | import type { PandaContext } from './create-context' |
| 3 | |
| 4 | export async function codegen(ctx: PandaContext, ids?: ArtifactId[]) { |
| 5 | const { default: pLimit } = await import('p-limit') |
| 6 | const limit = pLimit(20) |
| 7 | if (ctx.config.clean) ctx.output.empty() |
| 8 | |
| 9 | let artifacts = ctx.getArtifacts(ids) |
| 10 | if (ctx.hooks['codegen:prepare']) { |
| 11 | const results = await ctx.hooks['codegen:prepare']?.({ changed: ids, artifacts }) |
| 12 | if (results) artifacts = results |
| 13 | } |
| 14 | |
| 15 | // limit concurrency since we might output a lot of files |
| 16 | const promises = artifacts.map((artifact) => limit(() => ctx.output.write(artifact))) |
| 17 | await Promise.allSettled(promises) |
| 18 | |
| 19 | await ctx.hooks['codegen:done']?.({ changed: ids }) |
| 20 | |
| 21 | return { |
| 22 | box: ctx.initMessage(), |
| 23 | msg: ctx.messages.artifactsGenerated(), |
| 24 | } |
| 25 | } |