microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
npm/generate_samples_content.js
31lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | // @ts-check |
| 5 | |
| 6 | import { readFileSync, writeFileSync } from "node:fs"; |
| 7 | import { dirname, join } from "node:path"; |
| 8 | import { fileURLToPath } from "node:url"; |
| 9 | |
| 10 | import samples from "../samples/samples.mjs"; |
| 11 | |
| 12 | const thisDir = dirname(fileURLToPath(import.meta.url)); |
| 13 | const sampleDir = join(thisDir, "..", "samples"); |
| 14 | const sampleGeneratedDir = join(thisDir, "src"); |
| 15 | |
| 16 | const result = samples.map((sample) => { |
| 17 | const samplePath = join(sampleDir, sample.file); |
| 18 | const sampleText = readFileSync(samplePath, "utf8"); |
| 19 | return { |
| 20 | title: sample.title, |
| 21 | shots: sample.shots, |
| 22 | code: sampleText, |
| 23 | }; |
| 24 | }); |
| 25 | |
| 26 | const contentPath = join(sampleGeneratedDir, "samples.generated.ts"); |
| 27 | writeFileSync( |
| 28 | contentPath, |
| 29 | `export default ${JSON.stringify(result, undefined, 2)}`, |
| 30 | "utf-8" |
| 31 | ); |
| 32 | |