keroosha.cybergulag.today/quartz/processors/emit.ts

46 lines
1.5 KiB
TypeScript
Raw Normal View History

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