-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild-browser.js
84 lines (55 loc) · 1.8 KB
/
build-browser.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
const fs = require("fs");
const path = require("path");
function endsWith(file, ...fileExtensions) {
for (const ext of fileExtensions) {
if (file.endsWith(ext)) {
return true;
}
}
return false;
}
function copyFiles(srcFolder, outFolder, ...exts) {
const files = fs.readdirSync(srcFolder);
for (const file of files) {
const from = path.join(srcFolder, file);
const to = path.join(outFolder, file);
if (fs.statSync(from).isDirectory()) {
fs.mkdirSync(to);
copyFiles(from, to, ...exts);
} else if (endsWith(file, ...exts) && file !== "index.js") {
fs.copyFileSync(from, to);
}
}
}
function processJSFiles(fromFolder, toFolder) {
const files = fs.readdirSync(fromFolder);
for (const file of files) {
const from = path.join(fromFolder, file);
const to = path.join(toFolder, file);
if (fs.statSync(from).isDirectory()) {
processJSFiles(from, to);
} else if (endsWith(file, ".js") && file !== "index.js") {
let content = fs.readFileSync(from, "utf8");
const importRegex = /from "([^"]+)"/g;
content = content.replace(importRegex, (match, p1) => {
if (p1 === '@phaserjs/editor-scripts-base') {
return `from "../../phaserjs_editor_scripts_base/index.js"`;
}
return `from "${p1}.js"`;
});
//console.log(`Writing ${to}`);
fs.writeFileSync(to, content);
}
}
}
const packageData = JSON.parse(fs.readFileSync("package.json"));
const name = packageData.name
.replaceAll("@", "")
.replaceAll("/", "_")
.replaceAll("-", "_");
const version = packageData.version;
fs.rmSync(`browser/${name}`, {recursive: true, force: true});
fs.mkdirSync(`browser/${name}`, { recursive: true });
fs.writeFileSync(`browser/${name}/library.txt`, version);
copyFiles("src", `browser/${name}/`, ".scene", ".components");
processJSFiles("out", `browser/${name}/`);