microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
jan

Branches

Tags

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

Clone

HTTPS

Download ZIP

source/npm/qsharp/generate_docs.js

72lines · modeblame

139a8e70Scott Carda2 years ago1// 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
95956855Bill Ticehurst1 years ago10import { generate_docs } from "./lib/nodejs/qsc_wasm.cjs";
139a8e70Scott Carda2 years ago11
12const scriptDirPath = dirname(fileURLToPath(import.meta.url));
13const docsDirPath = join(scriptDirPath, "docs");
14
15if (!existsSync(docsDirPath)) {
16mkdirSync(docsDirPath);
17}
18
19// 'filename' will be of the format 'namespace/api.md' (except for 'toc.yaml')
291d3b99Scott Carda2 years ago20// 'metadata' will be the metadata that will appear at the top of the file
21// 'contents' will contain the non-metadata markdown expected
139a8e70Scott Carda2 years ago22
291d3b99Scott Carda2 years ago23/** @type {Array<{filename: string; metadata: string; contents: string}>} */
139a8e70Scott Carda2 years ago24const docs = generate_docs();
25
26if (!docs || !docs.length) {
27throw 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();
61a4720cScott Carda1 years ago34var today_str = mm + "/" + dd + "/" + yyyy;
139a8e70Scott Carda2 years ago35
36docs.forEach((doc) => {
37// If the filename contains a /, then we need to create the directory
38const parts = doc.filename.split("/");
39let fullPath = "";
40switch (parts.length) {
41case 1:
61a4720cScott Carda1 years ago42if (doc.filename !== "toc.yml" && doc.filename !== "index.md") {
139a8e70Scott Carda2 years ago43throw new Error(`Invalid filename: ${doc.filename}`);
44} else {
45fullPath = join(docsDirPath, doc.filename);
46}
47break;
48case 2: {
49// Create the directory of the first part
50const dirName = join(docsDirPath, parts[0]);
51if (!existsSync(dirName)) {
52mkdirSync(dirName);
53}
54fullPath = join(dirName, parts[1]);
55break;
56}
57default:
58throw new Error(`Invalid file path: ${doc.filename}`);
59}
61a4720cScott Carda1 years ago60var contents = "";
61if (doc.filename === "toc.yml") {
62contents = doc.contents;
63} else {
64contents =
65doc.metadata.replace("ms.date: {TIMESTAMP}", `ms.date: ${today_str}`) +
66"\n\n" +
67doc.contents;
68}
139a8e70Scott Carda2 years ago69writeFileSync(fullPath, contents);
70});
71
72console.log("Done");