|
| 1 | +import { getProjectRootDir } from "docs-generator/utils/getProjectRootDir"; |
| 2 | +import { log } from "docs-generator/utils/log"; |
| 3 | +import { readFileSync, readdirSync, statSync } from "node:fs"; |
| 4 | +import { writeFile } from "node:fs/promises"; |
| 5 | +import { basename, join } from "node:path"; |
| 6 | +import { format } from "prettier"; |
| 7 | + |
| 8 | +import packageJson from "../package.json" with { type: "json" }; |
| 9 | +import { |
| 10 | + GENERATED_FILE_BANNER, |
| 11 | + GENERATED_STACKBLITZ_FILE, |
| 12 | +} from "./constants.js"; |
| 13 | +import { ensureGeneratedDir } from "./ensureGeneratedDir.js"; |
| 14 | + |
| 15 | +function getAllFiles(dir: string): readonly string[] { |
| 16 | + const files: string[] = []; |
| 17 | + const dirFiles = readdirSync(dir); |
| 18 | + dirFiles.forEach((file) => { |
| 19 | + if ( |
| 20 | + /node_modules|(\.git$)|RootLayout|MainNavigation|README|CHANGELOG|(\.(ico|png))/.test( |
| 21 | + file |
| 22 | + ) |
| 23 | + ) { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + const fullPath = join(dir, file); |
| 28 | + if (statSync(fullPath).isDirectory()) { |
| 29 | + files.push(...getAllFiles(fullPath)); |
| 30 | + } else { |
| 31 | + files.push(fullPath); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + return files; |
| 36 | +} |
| 37 | + |
| 38 | +interface TemplateHiddenInputProps { |
| 39 | + name: string; |
| 40 | + value: string; |
| 41 | +} |
| 42 | + |
| 43 | +function toTemplateHiddenInputProps( |
| 44 | + fullFilePath: string |
| 45 | +): TemplateHiddenInputProps { |
| 46 | + const srcPath = fullFilePath.replace(/^.+vite-(t|j)s\//, ""); |
| 47 | + const name = basename(srcPath); |
| 48 | + |
| 49 | + let contents = readFileSync(fullFilePath, "utf8"); |
| 50 | + if (name === "package.json") { |
| 51 | + const parsed = JSON.parse(contents); |
| 52 | + // sass-embedded doesn't work here for some reason |
| 53 | + parsed.devDependencies.sass = parsed.devDependencies["sass-embedded"]; |
| 54 | + delete parsed.devDependencies["sass-embedded"]; |
| 55 | + |
| 56 | + contents = JSON.stringify(parsed, null, 2); |
| 57 | + } else if (name === "index.html") { |
| 58 | + contents = contents.replace( |
| 59 | + /id="root"/, |
| 60 | + `id="root" class="{{CLASS_NAME}}"` |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + return { |
| 65 | + name: `project[files][${srcPath}]`, |
| 66 | + value: contents, |
| 67 | + }; |
| 68 | +} |
| 69 | + |
| 70 | +async function run(): Promise<void> { |
| 71 | + await ensureGeneratedDir(); |
| 72 | + |
| 73 | + const examplesRoot = join(getProjectRootDir(), "examples"); |
| 74 | + const typescriptFiles = getAllFiles(join(examplesRoot, "vite-ts")); |
| 75 | + const javascriptFiles = getAllFiles(join(examplesRoot, "vite-js")); |
| 76 | + |
| 77 | + const contents = `${GENERATED_FILE_BANNER} |
| 78 | +
|
| 79 | +export const JS_STACKBLITZ_TEMPLATE = ${JSON.stringify(javascriptFiles.map((file) => toTemplateHiddenInputProps(file)))} |
| 80 | +export const TS_STACKBLITZ_TEMPLATE = ${JSON.stringify(typescriptFiles.map((file) => toTemplateHiddenInputProps(file)))} |
| 81 | +
|
| 82 | +export const STACKBLITZ_DEPENDENCIES: Record<string, string> = ${JSON.stringify( |
| 83 | + { |
| 84 | + ...packageJson.dependencies, |
| 85 | + ...packageJson.devDependencies, |
| 86 | + } |
| 87 | + )} |
| 88 | +`; |
| 89 | + |
| 90 | + await writeFile( |
| 91 | + GENERATED_STACKBLITZ_FILE, |
| 92 | + await format(contents, { filepath: GENERATED_STACKBLITZ_FILE }), |
| 93 | + "utf8" |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +await log(run(), "", `Created ${GENERATED_STACKBLITZ_FILE}`); |
0 commit comments