-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (113 loc) · 4.61 KB
/
index.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const childProcess = require("child_process");
const fs = require("fs");
const path = require("path");
const config = require("./config.json");
if (!config.gui_blocks_path) {
throw new Error(`Define "gui_blocks_path" in the config.json file. Use the full path, not a relative path.`);
}
if (!config.vm_blocks_path) {
throw new Error(`Define "vm_blocks_path" in the config.json file. Use the full path, not a relative path.`);
}
if (!config.real_blocks_path) {
throw new Error(`Define "real_blocks_path" in the config.json file. Use the full path, not a relative path.`);
}
if (!config.gui_blocks_media_path) {
throw new Error(`Define "gui_blocks_media_path" in the config.json file. Use the full path, not a relative path.`);
}
if (!config.vm_blocks_media_path) {
throw new Error(`Define "vm_blocks_media_path" in the config.json file. Use the full path, not a relative path.`);
}
const getTemplateKey = (name) => {
return `{{{{!!__TEMPLATE_${name}__!!}}}}`;
};
const getAllTemplateKeys = (data) => {
const keys = String(data).match(/(?<={{{{!!__TEMPLATE_)[^\.]+/gm).map(match => {
return String(match) + '.js';
});
return keys;
};
const escapeText = (text) => {
const stringified = JSON.stringify(String(text));
return stringified.substring(1, stringified.length - 1);
};
// no error checking because wtf would we do if an error happened, replace the files anyways?
const template = fs.readFileSync("./template.js", "utf8");
const handleAfterCompilation = () => {
let finalizedVertical = template;
const keys = getAllTemplateKeys(finalizedVertical);
const allReadingPaths = keys.map(key => ({
path: path.join(config.real_blocks_path, key),
key: key
}));
console.log("replacing template");
for (const readingPath of allReadingPaths) {
console.log("reading", readingPath.key);
const compiled = escapeText(fs.readFileSync(readingPath.path, "utf8"));
finalizedVertical = finalizedVertical.replace(getTemplateKey(readingPath.key), compiled);
console.log("replaced", getTemplateKey(readingPath.key), "in the compilation");
}
// for the user to see ig
fs.writeFileSync(
"./_last_compilation.js",
'/* Generated by penguinmod-blocks-updater. Editing this file will do nothing. */'
+ '/* This is purely for viewing the generated output. */'
+ `\n/* ${JSON.stringify(allReadingPaths, null, 4).replaceAll("*/", "*-/")} */`
+ finalizedVertical,
"utf8"
);
// return;
console.log("replacing packages");
console.log("replacing dist folder");
const guiPath = {
nor: path.join(config.gui_blocks_path, "/dist/vertical.js"),
web: path.join(config.gui_blocks_path, "/dist/web/vertical.js")
};
const vmPath = {
nor: path.join(config.vm_blocks_path, "/dist/vertical.js"),
web: path.join(config.vm_blocks_path, "/dist/web/vertical.js")
};
const paths = [guiPath, vmPath];
console.log(paths, "replacing these paths with new package");
for (const path of paths) {
fs.writeFileSync(path.nor, finalizedVertical);
fs.writeFileSync(path.web, finalizedVertical);
}
console.log("replaced dist folder");
console.log("replacing media folder...");
const source = path.join(config.real_blocks_path, "/media");
fs.rmSync(config.gui_blocks_media_path, { recursive: true, force: true });
fs.rmSync(config.vm_blocks_media_path, { recursive: true, force: true });
fs.cpSync(source, config.gui_blocks_media_path, {
errorOnExist: false,
force: true,
recursive: true
});
fs.cpSync(source, config.vm_blocks_media_path, {
errorOnExist: false,
force: true,
recursive: true
});
console.log("replaced media folder");
console.log("operations completed, gg");
};
if (config.shouldRecompile) {
console.log("Recompiling, please wait...");
const enterPath = JSON.stringify(`${config.real_blocks_path}`);
// enter the path & then run the npm command that builds the files (may take a while)
childProcess.exec(`cd ${enterPath} && npm run prepublish`, (err, stdout, stderr) => {
if (err) {
throw new Error(err);
}
if (stderr) {
// these dont seem to be fatal
console.error("stderr:", stderr);
}
console.log("stdout:", stdout);
// compilation is now finished so we can use fs to read files from that dir now
handleAfterCompilation();
});
} else {
// we just assume that we are already compiled
console.log('skipping compilation...');
handleAfterCompilation();
}