microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billti/qdk_package

Branches

Tags

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

Clone

HTTPS

Download ZIP

source/npm/qsharp/generate_docs.js

72lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4// @ts-check
5
6import { existsSync, mkdirSync, writeFileSync } from "node:fs";
7import { dirname, join } from "node:path";
8import { fileURLToPath } from "node:url";
9
10import { generate_docs } from "./lib/nodejs/qsc_wasm.cjs";
11
12const scriptDirPath = dirname(fileURLToPath(import.meta.url));
13const docsDirPath = join(scriptDirPath, "docs");
14
15if (!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}>} */
24const docs = generate_docs();
25
26if (!docs || !docs.length) {
27 throw new Error("No docs generated");
28}
29
30var today = new Date();
31var dd = String(today.getDate()).padStart(2, "0");
32var mm = String(today.getMonth() + 1).padStart(2, "0"); //January is 0!
33var yyyy = today.getFullYear();
34var today_str = mm + "/" + dd + "/" + yyyy;
35
36docs.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
72console.log("Done");
73