From 17535d45d710814a706f1e982b26e9488b98222d Mon Sep 17 00:00:00 2001 From: evelyn masso Date: Wed, 28 Feb 2024 17:16:48 -0800 Subject: [PATCH] Update build-contribute.ts --- src/scripts/build-contribute.ts | 115 +++++++++++++++++++++++++++++--- 1 file changed, 106 insertions(+), 9 deletions(-) diff --git a/src/scripts/build-contribute.ts b/src/scripts/build-contribute.ts index 55249da32f..46a02a85ff 100644 --- a/src/scripts/build-contribute.ts +++ b/src/scripts/build-contribute.ts @@ -1,24 +1,121 @@ -import fs from "fs/promises"; +import { readdir, cp } from "fs/promises"; import path from "path"; -import { cloneLibraryRepo } from "./utils"; +import { fileURLToPath } from "url"; +import YAML from "yaml"; +import { cloneLibraryRepo, readFile, writeFile } from "./utils"; +import type { Dirent } from "fs"; -const localPath = "in/p5.js/"; +/// The absolute path to the folder this file is in +const __dirname = path.dirname(fileURLToPath(import.meta.url)); + +/// Absolute path to the root of this project repo +const repoRootPath = path.join(__dirname, "../../"); + +const localPath = path.join(__dirname, "../../in/p5.js/"); const repoUrl = "https://github.com/processing/p5.js.git"; /// Where to find the docs in the cloned repo -const sourceDirectory = path.join( - __dirname, - localPath, - "src/contributor_docs/", -); +const sourceDirectory = path.join(localPath, "contributor_docs/"); /// Where the docs will be output for the website const outputDirectory = path.join(__dirname, "../content/contributor-docs/"); +/// Directories that are translations +// TODO: tie this to supported languages in astro config +const langDirs = ["ar", "es", "hi", "ko", "pt-br", "sk", "zh"]; +const assetsSubFolder = "images"; + +const convertMdtoMdx = async ( + sourceFile: string, + destinationFolder: string, + frontmatterObject?: { [key: string]: string }, +) => { + const { name, ext } = path.parse(sourceFile); + + if (ext !== ".md") { + console.warn( + `${sourceFile} is not an .md file, and will not be converted to .mdx`, + ); + return; + } + + const contents = await readFile(sourceFile); + + // this means the read file failed for some reason + if (contents === undefined) return; + + const newFilePath = path.join(destinationFolder, `${name}.mdx`); + const newContents = ` + --- + ${YAML.stringify(frontmatterObject)} + --- + ${contents} + `; + await writeFile(newFilePath, newContents); +}; + +const moveAssetsFolder = async (dirPath: string) => { + await cp(dirPath, path.join(repoRootPath, "public/contributor-docs"), { + recursive: true, + }); +}; + +/** + * Moves a list of files or a folder of files to a new location, + * converting all .md files into .mdx + * + * Does not recurse into subfolders! + * + * @param files + */ +const moveContentFiles = async ( + files: Dirent | Array, + destinationFolder: string, +) => { + // TODO: Make this little transform easier to read + const filepathsToMove = Array.isArray(files) + ? files + : files.isFile() + ? [files.path] + : await readdir(files.path); + for (const fp of filepathsToMove) { + const { ext, base } = path.parse(fp); + if (ext === ".md") { + await convertMdtoMdx(fp, destinationFolder); + } else { + await cp(fp, path.join(destinationFolder, base)); + } + } +}; + const run = async () => { console.log("hello from the contribute builder script"); await cloneLibraryRepo(localPath, repoUrl); - await fs.copyFile(sourceDirectory, outputDirectory); + + // get all the files and folders within the docs folder + const topLevelFiles = await readdir(sourceDirectory, { withFileTypes: true }); + for (const tlf of topLevelFiles) { + const { ext, base } = path.parse(tlf.path); + if (tlf.isDirectory()) { + if (base === assetsSubFolder) { + console.log("moving images folder"); + await moveAssetsFolder(tlf.path); + } else if (langDirs.includes(base)) { + console.log(`moving lang folder (${tlf.name})`); + await moveContentFiles(tlf, path.join(outputDirectory, tlf.name)); + } else { + console.log(`moving regular folder into 'en' (${tlf.name})`); + await moveContentFiles(tlf, path.join(outputDirectory, "en", tlf.name)); + } + } else if (ext === ".md") { + console.log(`moving markdown file into 'en' (${tlf.name})`); + await convertMdtoMdx(tlf.path, path.join(outputDirectory, "en")); + } else if (ext === ".mdx") { + console.log(`copy mdx file into 'en' (${tlf.name})`); + await cp(tlf.path, path.join(outputDirectory, "en", base)); + } + } + console.log("DONE???"); }; run();