(context: Context)
| 9 | import { generateClientBundle } from './client'; |
| 10 | |
| 11 | export async function generateOutput(context: Context): Promise<{ fileCount: number }> { |
| 12 | const outputPath = path.resolve(context.config.output.path); |
| 13 | |
| 14 | if (context.config.output.clean) { |
| 15 | if ( |
| 16 | await fsPromises |
| 17 | .access(outputPath) |
| 18 | .then(() => true) |
| 19 | .catch(() => false) |
| 20 | ) { |
| 21 | await fsPromises.rm(outputPath, { force: true, recursive: true }); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | const config = getTypedConfig(context); |
| 26 | |
| 27 | const client = getClientPlugin(config); |
| 28 | if ('bundle' in client.config && client.config.bundle && !config.dryRun) { |
| 29 | // not proud of this one |
| 30 | // @ts-expect-error |
| 31 | config._FRAGILE_CLIENT_BUNDLE_RENAMED = generateClientBundle({ |
| 32 | header: config.output.header, |
| 33 | outputPath, |
| 34 | // @ts-expect-error |
| 35 | plugin: client, |
| 36 | project: context.gen, |
| 37 | }); |
| 38 | } |
| 39 | |
| 40 | for (const plugin of context.registerPlugins()) { |
| 41 | await plugin.run(); |
| 42 | } |
| 43 | |
| 44 | context.gen.plan(); |
| 45 | |
| 46 | const ctx = new IntentContext(context.spec); |
| 47 | for (const intent of context.intents) { |
| 48 | await intent.run(ctx); |
| 49 | } |
| 50 | |
| 51 | let fileCount = 0; |
| 52 | const writes: Promise<void>[] = []; |
| 53 | for (const file of context.gen.render()) { |
| 54 | const filePath = path.resolve(outputPath, file.path); |
| 55 | const dir = path.dirname(filePath); |
| 56 | if (!context.config.dryRun) { |
| 57 | writes.push( |
| 58 | fsPromises |
| 59 | .mkdir(dir, { recursive: true }) |
| 60 | .then(() => fsPromises.writeFile(filePath, file.content, { encoding: 'utf8' })), |
| 61 | ); |
| 62 | } |
| 63 | fileCount++; |
| 64 | } |
| 65 | await Promise.all(writes); |
| 66 | |
| 67 | const { source } = context.config.output; |
| 68 | if (source.enabled) { |
no test coverage detected