|
| 1 | +import fs from "fs/promises"; |
| 2 | +import path from "path"; |
| 3 | +import { fileURLToPath } from "url"; |
| 4 | +import { execSync } from "child_process"; |
| 5 | + |
| 6 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 7 | + |
| 8 | +const root = path.resolve(__dirname, "../node_modules"); |
| 9 | +const prismPath = path.join(root, "prismjs"); |
| 10 | + |
| 11 | +let sourcePath, destPath; |
| 12 | + |
| 13 | +// --- Cloning & Installing Prism --- |
| 14 | +console.log("[postinstall] Cloning Prism..."); |
| 15 | +// Ensure we work with a fresh copy |
| 16 | +await fs.rm(prismPath, { recursive: true, force: true }); |
| 17 | +// FIXME: Remove “--branch v2” when Prism v2 is released to fallback to the default branch |
| 18 | +execSync("git clone --branch v2 https://github.com/PrismJS/prism.git prismjs", { |
| 19 | + cwd: root, |
| 20 | + stdio: "inherit", |
| 21 | +}); |
| 22 | + |
| 23 | +console.log("[postinstall] Installing Prism dependencies..."); |
| 24 | +execSync("npm install", { |
| 25 | + cwd: prismPath, |
| 26 | + stdio: "inherit", |
| 27 | +}); |
| 28 | + |
| 29 | +console.log("[postinstall] Building Prism..."); |
| 30 | +execSync("npm run build", { |
| 31 | + cwd: prismPath, |
| 32 | + stdio: "inherit", |
| 33 | +}); |
| 34 | + |
| 35 | +// --- Working with plugins --- |
| 36 | +sourcePath = path.join(prismPath, "src/plugins"); |
| 37 | +destPath = path.resolve(__dirname, "../plugins"); |
| 38 | + |
| 39 | +async function copy () { |
| 40 | + // We need { recursive: true } so the script doesn't fail if the folder already exists |
| 41 | + await fs.mkdir(destPath, { recursive: true }); |
| 42 | + |
| 43 | + let plugins = await fs.readdir(sourcePath, { withFileTypes: true }); |
| 44 | + for (let plugin of plugins) { |
| 45 | + if (!plugin.isDirectory()) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + let source = path.join(sourcePath, plugin.name); |
| 50 | + let dest = path.join(destPath, plugin.name); |
| 51 | + await fs.mkdir(dest, { recursive: true }); |
| 52 | + |
| 53 | + let files = await fs.readdir(source, { withFileTypes: true }); |
| 54 | + for (let file of files) { |
| 55 | + if (!file.isFile()) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + let name = path.parse(file.name).name; |
| 60 | + // Copy only the README.md and demo.* files |
| 61 | + if (["README", "demo"].includes(name)) { |
| 62 | + await fs.copyFile(path.join(source, file.name), path.join(dest, file.name)); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +console.log("[postinstall] Copying Prism plugins docs..."); |
| 69 | +try { |
| 70 | + await copy(); |
| 71 | +} |
| 72 | +catch (error) { |
| 73 | + console.error(`[postinstall] Failed to copy Prism plugins docs: ${error.message}`); |
| 74 | +} |
| 75 | + |
| 76 | +// Create plugins.json in the plugins folder with global data |
| 77 | +console.log("[postinstall] Creating plugins.json..."); |
| 78 | +let json = { |
| 79 | + permalink: "{{ page.filePathStem.replace('README', '/index') }}.html", |
| 80 | + tags: ["plugin"], |
| 81 | +}; |
| 82 | + |
| 83 | +await fs.writeFile(path.join(destPath, "plugins.json"), JSON.stringify(json, null, "\t")); |
| 84 | + |
| 85 | +// --- Copying other files (components.json, file-sizes.json, etc.) --- |
| 86 | +sourcePath = path.join(prismPath, "dist"); |
| 87 | +destPath = path.resolve(__dirname, ".."); |
| 88 | + |
| 89 | +let filenames = ["components.json", "file-sizes.json"]; |
| 90 | +for (let file of filenames) { |
| 91 | + console.log(`[postinstall] Copying ${file}...`); |
| 92 | + try { |
| 93 | + await fs.copyFile(path.join(sourcePath, file), path.join(destPath, file)); |
| 94 | + } |
| 95 | + catch (error) { |
| 96 | + console.error(`[postinstall] Failed to copy ${file}: ${error.message}`); |
| 97 | + } |
| 98 | +} |
0 commit comments