-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (71 loc) · 2.42 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
const glob = require("glob");
const fs = require("fs");
const fse = require("fs-extra");
const defSettings = {
package_path: ".",
exclude_modules: [
"@types/mojang-minecraft",
"@types/mojang-gametest",
],
silent: true,
resolve_imports: true,
}
const settings = Object.assign(
defSettings,
process.argv[2] ? JSON.parse(process.argv[2]) : {}
);
const included_modules = []
let package;
try {
package = JSON.parse(fs.readFileSync(`../../${settings.package_path}/package-lock.json`).toString());
} catch (e) {
console.warn("can't find package-lock.json at project root!");
return;
}
// add dependencies to output dir
for (const module in package.dependencies) {
if (defSettings.exclude_modules.includes(module)) {
continue;
}
// cp node_modules/[module_name]/[exports] -> [target]/[exports]
const module_path = `../../${settings.package_path}/node_modules/${module}`
let config;
try {
config = JSON.parse(fs.readFileSync(`${module_path}/minecraft-module.config.json`).toString());
} catch (err) {
// minecraft-module.config.json not found
if (!settings.silent) {
console.warn(`unable to resolve module "${module}", ${module}/minecraft-module.config.json not found`);
}
continue;
}
// copy module files to target
for (const key in config.exports) {
fse.copySync(`${module_path}/${key}`, config.exports[key]);
}
included_modules.push(module);
if (!settings.silent) {
console.log(`resolved module "${module}"`);
}
}
console.log(included_modules.join("|"));
/**
* replace all module imports with correct relative imports
* @param {Error} err
* @param {string[]} files_names
*/
function reImportFile(err, files_names) {
const depth_offset = 2;
for (const file_name of files_names) {
const file = fs.readFileSync(file_name);
const depth = file_name.split("/").length - depth_offset;
const changed_file = file.toString().replace(
new RegExp(`(import|export) ((?:.|\n|\r)*?) from ["'\`](${included_modules.join("|")})["'\`]`, "g"),
`$1 $2 from "${depth === 0 ? "./" : "../".repeat(depth)}scripts/modules/$3/index.js"`
)
fs.writeFileSync(file_name, changed_file);
}
}
if (settings.resolve_imports) {
glob("BP/scripts/**/*.ts", { ignore: ["BP/scripts/server/**/*", "BP/scripts/client/**/*"] }, reImportFile);
}