microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/npm/qsharp/generate_docs.js
72lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | // @ts-check |
| 5 | |
| 6 | import { existsSync, mkdirSync, writeFileSync } from "node:fs"; |
| 7 | import { dirname, join } from "node:path"; |
| 8 | import { fileURLToPath } from "node:url"; |
| 9 | |
| 10 | import { generate_docs } from "./lib/nodejs/qsc_wasm.cjs"; |
| 11 | |
| 12 | const scriptDirPath = dirname(fileURLToPath(import.meta.url)); |
| 13 | const docsDirPath = join(scriptDirPath, "docs"); |
| 14 | |
| 15 | if (!existsSync(docsDirPath)) { |
| 16 | mkdirSync(docsDirPath); |
| 17 | } |
| 18 | |
| 19 | // 'filename' will be of the format 'namespace/api.md' (except for 'toc.yaml') |
| 20 | // 'metadata' will be the metadata that will appear at the top of the file |
| 21 | // 'contents' will contain the non-metadata markdown expected |
| 22 | |
| 23 | /** @type {Array<{filename: string; metadata: string; contents: string}>} */ |
| 24 | const docs = generate_docs(); |
| 25 | |
| 26 | if (!docs || !docs.length) { |
| 27 | throw new Error("No docs generated"); |
| 28 | } |
| 29 | |
| 30 | var today = new Date(); |
| 31 | var dd = String(today.getDate()).padStart(2, "0"); |
| 32 | var mm = String(today.getMonth() + 1).padStart(2, "0"); //January is 0! |
| 33 | var yyyy = today.getFullYear(); |
| 34 | var today_str = mm + "/" + dd + "/" + yyyy; |
| 35 | |
| 36 | docs.forEach((doc) => { |
| 37 | // If the filename contains a /, then we need to create the directory |
| 38 | const parts = doc.filename.split("/"); |
| 39 | let fullPath = ""; |
| 40 | switch (parts.length) { |
| 41 | case 1: |
| 42 | if (doc.filename !== "toc.yml" && doc.filename !== "index.md") { |
| 43 | throw new Error(`Invalid filename: ${doc.filename}`); |
| 44 | } else { |
| 45 | fullPath = join(docsDirPath, doc.filename); |
| 46 | } |
| 47 | break; |
| 48 | case 2: { |
| 49 | // Create the directory of the first part |
| 50 | const dirName = join(docsDirPath, parts[0]); |
| 51 | if (!existsSync(dirName)) { |
| 52 | mkdirSync(dirName); |
| 53 | } |
| 54 | fullPath = join(dirName, parts[1]); |
| 55 | break; |
| 56 | } |
| 57 | default: |
| 58 | throw new Error(`Invalid file path: ${doc.filename}`); |
| 59 | } |
| 60 | var contents = ""; |
| 61 | if (doc.filename === "toc.yml") { |
| 62 | contents = doc.contents; |
| 63 | } else { |
| 64 | contents = |
| 65 | doc.metadata.replace("ms.date: {TIMESTAMP}", `ms.date: ${today_str}`) + |
| 66 | "\n\n" + |
| 67 | doc.contents; |
| 68 | } |
| 69 | writeFileSync(fullPath, contents); |
| 70 | }); |
| 71 | |
| 72 | console.log("Done"); |
| 73 | |