2023-05-31 21:01:23 +00:00
|
|
|
import path from "path"
|
|
|
|
import fs from "fs"
|
|
|
|
import { PerfTimer } from "../perf"
|
2023-07-24 01:20:43 +00:00
|
|
|
import { getStaticResourcesFromPlugins } from "../plugins"
|
2023-05-31 21:01:23 +00:00
|
|
|
import { EmitCallback } from "../plugins/types"
|
|
|
|
import { ProcessedContent } from "../plugins/vfile"
|
2023-08-03 05:10:13 +00:00
|
|
|
import { FilePath, joinSegments } from "../path"
|
2023-07-04 17:08:32 +00:00
|
|
|
import { QuartzLogger } from "../log"
|
2023-07-16 06:02:12 +00:00
|
|
|
import { trace } from "../trace"
|
2023-07-24 00:07:19 +00:00
|
|
|
import { BuildCtx } from "../ctx"
|
2023-07-02 20:08:29 +00:00
|
|
|
|
2023-07-24 00:09:12 +00:00
|
|
|
export async function emitContent(ctx: BuildCtx, content: ProcessedContent[]) {
|
|
|
|
const { argv, cfg } = ctx
|
2023-05-31 21:01:23 +00:00
|
|
|
const perf = new PerfTimer()
|
2023-07-24 00:07:19 +00:00
|
|
|
const log = new QuartzLogger(ctx.argv.verbose)
|
2023-07-04 17:08:32 +00:00
|
|
|
|
|
|
|
log.start(`Emitting output files`)
|
2023-05-31 21:01:23 +00:00
|
|
|
const emit: EmitCallback = async ({ slug, ext, content }) => {
|
2023-08-03 05:10:13 +00:00
|
|
|
const pathToPage = joinSegments(argv.output, slug + ext) as FilePath
|
2023-05-31 21:01:23 +00:00
|
|
|
const dir = path.dirname(pathToPage)
|
|
|
|
await fs.promises.mkdir(dir, { recursive: true })
|
|
|
|
await fs.promises.writeFile(pathToPage, content)
|
|
|
|
return pathToPage
|
|
|
|
}
|
|
|
|
|
2023-07-22 23:06:36 +00:00
|
|
|
let emittedFiles = 0
|
2023-07-24 07:04:01 +00:00
|
|
|
const staticResources = getStaticResourcesFromPlugins(ctx)
|
2023-05-31 21:01:23 +00:00
|
|
|
for (const emitter of cfg.plugins.emitters) {
|
2023-06-04 17:37:43 +00:00
|
|
|
try {
|
2023-07-24 00:09:12 +00:00
|
|
|
const emitted = await emitter.emit(ctx, content, staticResources, emit)
|
2023-06-04 17:37:43 +00:00
|
|
|
emittedFiles += emitted.length
|
2023-05-31 21:01:23 +00:00
|
|
|
|
2023-07-24 00:07:19 +00:00
|
|
|
if (ctx.argv.verbose) {
|
2023-06-04 17:37:43 +00:00
|
|
|
for (const file of emitted) {
|
|
|
|
console.log(`[emit:${emitter.name}] ${file}`)
|
|
|
|
}
|
2023-05-31 21:01:23 +00:00
|
|
|
}
|
2023-06-04 17:37:43 +00:00
|
|
|
} catch (err) {
|
2023-07-16 06:02:12 +00:00
|
|
|
trace(`Failed to emit from plugin \`${emitter.name}\``, err as Error)
|
2023-05-31 21:01:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 00:07:19 +00:00
|
|
|
log.end(`Emitted ${emittedFiles} files to \`${argv.output}\` in ${perf.timeSince()}`)
|
2023-05-31 21:01:23 +00:00
|
|
|
}
|