-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-build.js
163 lines (138 loc) · 5.79 KB
/
test-build.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
161
162
163
import fs from "fs";
import path from "path";
import { spawn } from "child_process";
import os from "os";
import fse from "fs-extra";
import { glob } from "./node_modules/glob/dist/esm/index.js";
import { promisify } from "util";
import { exec as execCallback } from "child_process";
// Promisify the exec function
const exec = promisify(execCallback);
// Array to store all spawned child processes
const spawnedProcesses = [];
// Function to get the latest "bedrock-server-*" directory
const getLatestBedrockServerDir = () => glob.sync("bedrock-server-*")[0];
const checkAndBuild = async () => {
// Clean up the 'build/' directory
const cleanBuildDir = "build";
if (fs.existsSync(cleanBuildDir)) {
fse.removeSync(cleanBuildDir);
console.log("> Cleaned up the 'build/' directory...\n");
}
// Use the function to get the latest directory
let bedrockServerDir = getLatestBedrockServerDir();
if (!bedrockServerDir) {
console.error("> Bedrock server directory not found...\n");
// Execute your BDS script here
const bdsProcess = spawn("node", ["bds.js"], {
stdio: "inherit",
});
spawnedProcesses.push(bdsProcess); // Add to the array
await new Promise((resolve) => {
bdsProcess.on("close", (code) => {
if (code === 0) {
bedrockServerDir = glob.sync("bedrock-server-*")[0];
console.log("\n> Bedrock server set up successfully...\n");
resolve();
} else {
console.error(" - Error while setting up the Bedrock server.");
process.exit(code);
}
});
});
}
if (!bedrockServerDir) {
console.error("> Bedrock server directory not found...\n");
return;
}
// Remove the ".zip" extension from the directory name if it exists
bedrockServerDir = bedrockServerDir.replace(/\.zip$/, "");
// Check if the 'worlds' folder exists, and if not, create it
const worldsDir = path.join(bedrockServerDir, "worlds");
if (!fs.existsSync(worldsDir)) {
fs.mkdirSync(worldsDir, { recursive: true });
}
// Check if the 'Bedrock level' subfolder exists in 'worlds', and if not, create it
const testWorldDir = path.join(worldsDir, "Bedrock level");
if (!fs.existsSync(testWorldDir)) {
fs.mkdirSync(testWorldDir);
}
// Copy 'new-world-beta-api'
fse.copySync("new-world-beta-api", testWorldDir);
// Determine the OS type and execute the appropriate build command
if (os.type() === "Linux") {
await exec("npm run build");
} else if (os.type() === "Windows_NT") {
await exec("npm run build_win");
} else {
console.error("Unsupported OS: " + os.type());
return;
}
// Copy the build contents to the 'bedrock-server-*/worlds/Bedrock level/behavior_packs/paradox' directory
const buildDir = "build";
const paradoxDir = path.join(testWorldDir, "behavior_packs", "paradox");
fse.copySync(buildDir, paradoxDir);
console.log(`> Copied build contents to '${bedrockServerDir}/worlds/Bedrock level/behavior_packs/paradox'...\n`);
// Read and parse manifest.json
const manifestPath = path.join(paradoxDir, "manifest.json");
if (fs.existsSync(manifestPath)) {
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
// Update world_behavior_packs.json
const worldBehaviorPacksPath = path.join(testWorldDir, "world_behavior_packs.json");
if (fs.existsSync(worldBehaviorPacksPath)) {
const worldBehaviorPacks = [
{
pack_id: manifest.header.uuid,
version: manifest.header.version,
},
];
// Write the updated JSON back to the file
fs.writeFileSync(worldBehaviorPacksPath, JSON.stringify(worldBehaviorPacks, null, 2));
}
}
console.log("> Test build completed...\n");
// Determine and execute the server based on the OS
const serverPath = path.resolve(bedrockServerDir, "bedrock_server");
if (os.type() === "Linux") {
// Execute on Linux
const sudoCommand = `LD_LIBRARY_PATH=. ${serverPath}`;
const chmodProcess = spawn("chmod", ["+x", serverPath], {
cwd: bedrockServerDir,
});
chmodProcess.on("close", (chmodCode) => {
if (chmodCode === 0) {
const serverProcess = spawn("sh", ["-c", `sudo ${sudoCommand}`], {
stdio: "inherit",
cwd: bedrockServerDir,
});
spawnedProcesses.push(serverProcess); // Add to the array
serverProcess.on("exit", (code) => {
console.log(`\n - Server exited with code ${code}. Killing all spawned processes...`);
spawnedProcesses.forEach((child) => {
child.kill();
});
process.exit(1);
});
} else {
console.error(" - Error setting execute permission for bedrock_server.");
}
});
} else if (os.type() === "Windows_NT") {
// Execute on Windows
const serverProcess = spawn("cmd", ["/c", serverPath], {
stdio: "inherit",
cwd: bedrockServerDir,
});
spawnedProcesses.push(serverProcess); // Add to the array
serverProcess.on("exit", (code) => {
console.log(`\n - Server exited with code ${code}. Killing all spawned processes...`);
spawnedProcesses.forEach((child) => {
child.kill();
});
process.exit(1);
});
} else {
console.error(" - Unsupported OS: " + os.type());
}
};
checkAndBuild();