microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
joaoboechat/test-verbose-gpu

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/npm/qsharp/generate_docs.js

77lines · modecode

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