microsoft/typespec
Publicmirrored from https://github.com/microsoft/typespecAvailable
packages/html-program-viewer/src/emitter.ts
17lines · modecode
| 1 | import { Program, resolvePath } from "@cadl-lang/compiler"; |
| 2 | import { readFile } from "fs/promises"; |
| 3 | import { dirname, resolve } from "path"; |
| 4 | import { fileURLToPath } from "url"; |
| 5 | import { renderProgram } from "./ui.js"; |
| 6 | |
| 7 | export async function $onEmit(program: Program) { |
| 8 | const html = renderProgram(program); |
| 9 | const htmlPath = resolvePath(program.compilerOptions.outputPath!, "cadl-program.html"); |
| 10 | const cssPath = resolvePath(program.compilerOptions.outputPath!, "style.css"); |
| 11 | await program.host.writeFile( |
| 12 | htmlPath, |
| 13 | `<!DOCTYPE html><html lang="en"><link rel="stylesheet" href="style.css"><body>${html}</body></html>` |
| 14 | ); |
| 15 | const cssFile = resolve(dirname(fileURLToPath(import.meta.url)), "../../src/style.css"); |
| 16 | await program.host.writeFile(cssPath, await (await readFile(cssFile)).toString()); |
| 17 | } |
| 18 | |