-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
161 lines (148 loc) · 7.07 KB
/
gulpfile.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import del from 'del';
import child_process from 'child_process';
import yargs from 'yargs'
import fs from 'fs';
import { hideBin } from 'yargs/helpers'
/**
* Cleanup function - delete some files that are not needed and print some messages
*/
function cleanup(cb, language) {
console.log("Deleting some files...");
del(['dist/base/index.html']);
console.log("Finished!");
if (language == "full-offline") {
console.log("If you want to set up a single-file distributable web server, use dist/base/redbean.com and check the icp-create-server repository");
}
console.log(`If you want to use the library and deploy to a server, all you need is the generated dist/base/${language}.iife.js\n`);
cb();
}
/**
* Main task - build the project for the specified language
*/
export default (cb) => {
// Check the language argument
const args = yargs(hideBin(process.argv)).argv;
if (args["language"] == undefined) {
// If the language argument is not specified, print the help message
console.log("Please specify a language to build the project for. For example, to build the project for python, use the following command:");
console.log("\tnpm run build -- --language python\n");
console.log("To build the project for all languages, use the following command:");
console.log("\tnpm run build -- --language full\n");
console.log("If you want to run python or java in offline mode, use the following command:");
console.log("\tnpm run build -- --language ${language}-offline\n");
console.log("The other languages instead already work in offline mode even when built normally.\n");
process.exit(1);
}
const language = args["language"] == "p5" || args["language"] == "processing" ? "p5-and-processing" : args["language"];
console.log(`Building the project for '${language}' - this may take a while...`);
child_process.exec(`vite build -c=build-config/vite.${language}.js`, (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] building the project");
cb(err);
}
// Replace "use strict"; with "use strict";\nglobalThis.process = globalThis.process;\n
// This is because the p5-and-processing library uses process, which is undefined in the browser
// This is a hacky way to fix it
console.log("Replacing process with globalThis.process");
child_process.exec(`sed -i 's/\"use strict\";/\"use strict\";\\nglobalThis.process = globalThis.process;\\n/g' dist/base/${language}.iife.js`, (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] replacing process with globalThis.process");
cb(err);
}
});
console.log("Build completed!");
if (language == "full-offline") {
console.log("Setting up redbean.com for full offline mode.")
child_process.exec("cd dist/base/ && chmod +x ../../bin/zip.com && ../../bin/zip.com -r redbean.com utils full-offline.iife.js reveal.js reveal.css blood.css white.css custom-style.css && ../../bin/zip.com -0 redbean.com index.html && chmod +x redbean.com", (err, stdout, stderr) => {
if (err) {
// Try to construct the path for windows systems
child_process.exec("cd dist\\base\\ && ..\\..\\bin\\zip.com -r redbean.com full-offline.iife.js reveal.js reveal.css blood.css white.css custom-style.css && ..\\..\\bin\\zip.com -r -0 redbean.com utils index.html", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] zipping dist into redbean.com");
cb(err);
}
cleanup(cb, language);
});
} else {
cleanup(cb, language);
}
})
} else {
cleanup(cb, language);
}
});
}
// Create a custom task 'prepare-cpp'
export function prepareCpp(cb) {
console.log("Preparing C++ files...");
child_process.exec("vite build -c=build-config/vite.prepare-cpp.js", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] preparing C++ files");
cb(err);
}
// Get the list of files and directories in the public folder
const files = fs.readdirSync("public");
// Remove all these files from src/modules/workers/cpp
files.forEach(file => {
console.log(`Removing ${file} from src/modules/workers/cpp`);
del([`src/modules/workers/cpp/${file}`]);
});
child_process.exec("cp src/modules/workers/cpp/cppWorkerBundle.iife.js public/utils/cpp/cppWorkerBundle.iife.js", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] copying worker to public folder");
cb(err);
}
console.log("Finished preparing C++ files!");
cb();
});
});
}
// Create a custom task 'prepare-python'
export function preparePython(cb) {
console.log("Preparing Python files...");
child_process.exec("vite build -c=build-config/vite.prepare-python.js", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] preparing Python files");
cb(err);
}
// Get the list of files and directories in the public folder
const files = fs.readdirSync("public");
// Remove all these files from src/modules/workers
files.forEach(file => {
console.log(`Removing ${file} from src/modules/workers`);
del([`src/modules/workers/${file}`]);
});
child_process.exec("cp src/modules/workers/pythonWorkerBundle.iife.js public/utils/python/pyodide/pythonWorkerBundle.iife.js", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] copying worker to public folder");
cb(err);
}
console.log("Finished preparing Python files!");
cb();
});
});
}
// Create a custom task 'prepare-java'
export function prepareJava(cb) {
console.log("Preparing Java files...");
child_process.exec("cp src/modules/workers/java/javaWorker.js public/utils/java/javaWorker.js", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] moving javaWorker.js to javaWorker.js");
cb(err);
}
child_process.exec("cp src/modules/workers/java/javaTeaWorkerOffline.js public/utils/java/javaTeaWorker.js", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] moving javaTeaWorkerOffline.js to javaTeaWorker.js");
cb(err);
}
child_process.exec("cp src/modules/workers/java/javaRunWorker.js public/utils/java/javaRunWorker.js", (err, stdout, stderr) => {
if (err) {
console.log("[ERROR] moving javaRunWorker.js to javaRunWorker.js");
cb(err);
}
console.log("Finished preparing Java files!");
cb();
});
});
});
}