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