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_samples_content.js

65lines · modeblame

02831734Bill Ticehurst3 years ago1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4// @ts-check
5
6import { readFileSync, writeFileSync } from "node:fs";
7import { dirname, join } from "node:path";
8import { fileURLToPath } from "node:url";
9
9da4acf2Scott Carda1 years ago10import qSharpSampleList from "../../../samples/samples.mjs";
11import openQasmSampleList from "../../../samples/OpenQASM/samples.mjs";
02831734Bill Ticehurst3 years ago12
13const thisDir = dirname(fileURLToPath(import.meta.url));
9da4acf2Scott Carda1 years ago14const qSharpSampleDir = join(thisDir, "..", "..", "..", "samples");
15const openQasmSampleDir = join(
16thisDir,
17"..",
18"..",
19"..",
20"samples",
21"OpenQASM",
22);
f11770aaDmitryVasilevsky1 years ago23
24const tsDir = join(thisDir, "src");
25const qSharpGeneratedTsPath = join(tsDir, "samples.generated.ts");
26const openQasmGeneratedTsPath = join(tsDir, "openqasm-samples.generated.ts");
27
28embedSampleContentsInTsFile(
29qSharpSampleList,
30qSharpSampleDir,
31qSharpGeneratedTsPath,
32);
33embedSampleContentsInTsFile(
34openQasmSampleList,
35openQasmSampleDir,
36openQasmGeneratedTsPath,
02831734Bill Ticehurst3 years ago37);
f11770aaDmitryVasilevsky1 years ago38
39/**
40* @param {any[]} sampleList
41* @param {string} sampleDir
42* @param {import("fs").PathOrFileDescriptor} generatedTsFileName
43*/
44function embedSampleContentsInTsFile(
45sampleList,
46sampleDir,
47generatedTsFileName,
48) {
49const result = sampleList.map((sample) => {
50const samplePath = join(sampleDir, sample.file);
51const sampleText = readFileSync(samplePath, "utf8");
52return {
53title: sample.title,
54shots: sample.shots,
55code: sampleText,
56omitFromTests: sample.omitFromTests,
57};
58});
59
60writeFileSync(
61generatedTsFileName,
62`export default ${JSON.stringify(result, undefined, 2)}`,
63"utf-8",
64);
65}