Skip to content

Commit 42c49d3

Browse files
committed
basic stuff
1 parent dd61750 commit 42c49d3

File tree

11 files changed

+665
-0
lines changed

11 files changed

+665
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/*
2+
*/node_modules/*
3+
4+
# BlocksCompiler
5+
BlocksCompiler/_last_compilation.js

BlocksCompiler/config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"real_blocks_path": "C:\\Users\\User\\Documents\\_LocalPenguinMod\\PenguinMod-Blocks",
3+
"vm_blocks_path": "C:\\Users\\User\\Documents\\_LocalPenguinMod\\PenguinMod-Vm\\node_modules\\scratch-blocks",
4+
"gui_blocks_path": "C:\\Users\\User\\Documents\\_LocalPenguinMod\\penguinmod.github.io\\node_modules\\scratch-blocks",
5+
"gui_blocks_media_path": "C:\\Users\\User\\Documents\\_LocalPenguinMod\\penguinmod.github.io\\node_modules\\scratch-blocks\\media",
6+
"vm_blocks_media_path": "C:\\Users\\User\\Documents\\_LocalPenguinMod\\PenguinMod-Vm\\node_modules\\scratch-blocks\\media",
7+
"shouldRecompile": true
8+
}

BlocksCompiler/index.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
const childProcess = require("child_process");
2+
const fs = require("fs");
3+
const path = require("path");
4+
5+
const config = require("./config.json");
6+
7+
if (!config.gui_blocks_path) {
8+
throw new Error(`Define "gui_blocks_path" in the config.json file. Use the full path, not a relative path.`);
9+
}
10+
if (!config.vm_blocks_path) {
11+
throw new Error(`Define "vm_blocks_path" in the config.json file. Use the full path, not a relative path.`);
12+
}
13+
if (!config.real_blocks_path) {
14+
throw new Error(`Define "real_blocks_path" in the config.json file. Use the full path, not a relative path.`);
15+
}
16+
if (!config.gui_blocks_media_path) {
17+
throw new Error(`Define "gui_blocks_media_path" in the config.json file. Use the full path, not a relative path.`);
18+
}
19+
if (!config.vm_blocks_media_path) {
20+
throw new Error(`Define "vm_blocks_media_path" in the config.json file. Use the full path, not a relative path.`);
21+
}
22+
23+
const getTemplateKey = (name) => {
24+
return `{{{{!!__TEMPLATE_${name}__!!}}}}`;
25+
};
26+
const getAllTemplateKeys = (data) => {
27+
const keys = String(data).match(/(?<={{{{!!__TEMPLATE_)[^\.]+/gm).map(match => {
28+
return String(match) + '.js';
29+
});
30+
return keys;
31+
};
32+
33+
const escapeText = (text) => {
34+
const stringified = JSON.stringify(String(text));
35+
return stringified.substring(1, stringified.length - 1);
36+
};
37+
38+
// no error checking because wtf would we do if an error happened, replace the files anyways?
39+
const template = fs.readFileSync("./template.js", "utf8");
40+
41+
const handleAfterCompilation = () => {
42+
let finalizedVertical = template;
43+
const keys = getAllTemplateKeys(finalizedVertical);
44+
const allReadingPaths = keys.map(key => ({
45+
path: path.join(config.real_blocks_path, key),
46+
key: key
47+
}));
48+
49+
console.log("replacing template");
50+
for (const readingPath of allReadingPaths) {
51+
console.log("reading", readingPath.key);
52+
const compiled = escapeText(fs.readFileSync(readingPath.path, "utf8"));
53+
finalizedVertical = finalizedVertical.replace(getTemplateKey(readingPath.key), compiled);
54+
console.log("replaced", getTemplateKey(readingPath.key), "in the compilation");
55+
}
56+
57+
// for the user to see ig
58+
fs.writeFileSync(
59+
"./_last_compilation.js",
60+
'/* Generated by penguinmod-blocks-updater. Editing this file will do nothing. */'
61+
+ '/* This is purely for viewing the generated output. */'
62+
+ `\n/* ${JSON.stringify(allReadingPaths, null, 4).replaceAll("*/", "*-/")} */`
63+
+ finalizedVertical,
64+
"utf8"
65+
);
66+
// return;
67+
68+
console.log("replacing packages");
69+
console.log("replacing dist folder");
70+
71+
const guiPath = {
72+
nor: path.join(config.gui_blocks_path, "/dist/vertical.js"),
73+
web: path.join(config.gui_blocks_path, "/dist/web/vertical.js")
74+
};
75+
const vmPath = {
76+
nor: path.join(config.vm_blocks_path, "/dist/vertical.js"),
77+
web: path.join(config.vm_blocks_path, "/dist/web/vertical.js")
78+
};
79+
80+
const paths = [guiPath, vmPath];
81+
82+
console.log(paths, "replacing these paths with new package");
83+
84+
for (const path of paths) {
85+
fs.writeFileSync(path.nor, finalizedVertical);
86+
fs.writeFileSync(path.web, finalizedVertical);
87+
}
88+
89+
console.log("replaced dist folder");
90+
91+
console.log("replacing media folder...");
92+
const source = path.join(config.real_blocks_path, "/media");
93+
94+
fs.rmSync(config.gui_blocks_media_path, { recursive: true, force: true });
95+
fs.rmSync(config.vm_blocks_media_path, { recursive: true, force: true });
96+
97+
fs.cpSync(source, config.gui_blocks_media_path, {
98+
errorOnExist: false,
99+
force: true,
100+
recursive: true
101+
});
102+
fs.cpSync(source, config.vm_blocks_media_path, {
103+
errorOnExist: false,
104+
force: true,
105+
recursive: true
106+
});
107+
108+
console.log("replaced media folder");
109+
console.log("operations completed, gg");
110+
};
111+
112+
if (config.shouldRecompile) {
113+
console.log("Recompiling, please wait...");
114+
const enterPath = JSON.stringify(`${config.real_blocks_path}`);
115+
116+
// enter the path & then run the npm command that builds the files (may take a while)
117+
childProcess.exec(`cd ${enterPath} && npm run prepublish`, (err, stdout, stderr) => {
118+
if (err) {
119+
throw new Error(err);
120+
}
121+
if (stderr) {
122+
// these dont seem to be fatal
123+
console.error("stderr:", stderr);
124+
}
125+
126+
console.log("stdout:", stdout);
127+
128+
// compilation is now finished so we can use fs to read files from that dir now
129+
handleAfterCompilation();
130+
});
131+
} else {
132+
// we just assume that we are already compiled
133+
console.log('skipping compilation...');
134+
handleAfterCompilation();
135+
}

BlocksCompiler/package-lock.json

Lines changed: 85 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BlocksCompiler/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "penguinmod-blocks-downloader",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"path": "^0.12.7",
13+
"scratch-blocks": "github:PenguinMod/PenguinMod-Blocks#develop-builds"
14+
}
15+
}

0 commit comments

Comments
 (0)