2023-08-16 22:04:15 -07:00
|
|
|
import { PerfTimer } from "../util/perf"
|
2023-07-23 18:20:43 -07:00
|
|
|
import { getStaticResourcesFromPlugins } from "../plugins"
|
2023-05-31 17:01:23 -04:00
|
|
|
import { ProcessedContent } from "../plugins/vfile"
|
2023-08-16 22:04:15 -07:00
|
|
|
import { QuartzLogger } from "../util/log"
|
|
|
|
import { trace } from "../util/trace"
|
|
|
|
import { BuildCtx } from "../util/ctx"
|
2025-03-13 10:27:46 -07:00
|
|
|
import chalk from "chalk"
|
2023-07-02 13:08:29 -07:00
|
|
|
|
2023-07-23 17:09:12 -07:00
|
|
|
export async function emitContent(ctx: BuildCtx, content: ProcessedContent[]) {
|
|
|
|
const { argv, cfg } = ctx
|
2023-05-31 17:01:23 -04:00
|
|
|
const perf = new PerfTimer()
|
2023-07-23 17:07:19 -07:00
|
|
|
const log = new QuartzLogger(ctx.argv.verbose)
|
2023-07-04 10:08:32 -07:00
|
|
|
|
2025-03-16 14:17:31 -07:00
|
|
|
log.start(`Emitting files`)
|
2023-05-31 17:01:23 -04:00
|
|
|
|
2023-07-22 16:06:36 -07:00
|
|
|
let emittedFiles = 0
|
2023-07-24 00:04:01 -07:00
|
|
|
const staticResources = getStaticResourcesFromPlugins(ctx)
|
2025-03-13 10:27:46 -07:00
|
|
|
await Promise.all(
|
|
|
|
cfg.plugins.emitters.map(async (emitter) => {
|
|
|
|
try {
|
|
|
|
const emitted = await emitter.emit(ctx, content, staticResources)
|
|
|
|
if (Symbol.asyncIterator in emitted) {
|
|
|
|
// Async generator case
|
|
|
|
for await (const file of emitted) {
|
|
|
|
emittedFiles++
|
|
|
|
if (ctx.argv.verbose) {
|
|
|
|
console.log(`[emit:${emitter.name}] ${file}`)
|
|
|
|
} else {
|
2025-03-16 14:17:31 -07:00
|
|
|
log.updateText(`${emitter.name} -> ${chalk.gray(file)}`)
|
2025-03-13 10:27:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Array case
|
|
|
|
emittedFiles += emitted.length
|
|
|
|
for (const file of emitted) {
|
|
|
|
if (ctx.argv.verbose) {
|
|
|
|
console.log(`[emit:${emitter.name}] ${file}`)
|
|
|
|
} else {
|
2025-03-16 14:17:31 -07:00
|
|
|
log.updateText(`${emitter.name} -> ${chalk.gray(file)}`)
|
2025-03-13 10:27:46 -07:00
|
|
|
}
|
|
|
|
}
|
2023-06-04 13:37:43 -04:00
|
|
|
}
|
2025-03-13 10:27:46 -07:00
|
|
|
} catch (err) {
|
|
|
|
trace(`Failed to emit from plugin \`${emitter.name}\``, err as Error)
|
2023-05-31 17:01:23 -04:00
|
|
|
}
|
2025-03-13 10:27:46 -07:00
|
|
|
}),
|
|
|
|
)
|
2023-05-31 17:01:23 -04:00
|
|
|
|
2023-07-23 17:07:19 -07:00
|
|
|
log.end(`Emitted ${emittedFiles} files to \`${argv.output}\` in ${perf.timeSince()}`)
|
2023-05-31 17:01:23 -04:00
|
|
|
}
|