-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackageBuild.ts
268 lines (249 loc) · 7.81 KB
/
packageBuild.ts
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env node
/**
*
* Credits: Dirtbikercj
*
*/
const fs = require("fs-extra");
const glob = require("glob");
const zip = require("bestzip");
const path = require("path");
const chalk = require("chalk");
const processHost = require("child_process");
const { author, name: packageName, version } = require("./package.json");
const configText = fs.readFileSync("./build.json");
const config = JSON.parse(configText);
const currentDir = __dirname;
const modName = `${packageName.replace(/[^a-z0-9]/gi, "")}`;
const currentDate = new Date();
const epoch = `${currentDate}`;
console.log(`[SPT build System] ${epoch}`);
if (config.GitStatus === true) {
if (fs.existsSync(`${currentDir}/.git`)) {
const gitStatus = processHost.execSync("git status -uno", {
encoding: "utf-8",
});
console.log(gitStatus);
} else
console.log(
chalk.yellow(
"[SPT Build System] GitStatus is enabled in build.json, but this isn't a Git repository."
)
);
}
if (config.ShowBuildOptions) {
console.log("[SPT Build System] Build options:");
console.log(`[SPT Build System] ContainPlugin: ${config.ContainPlugin}`);
console.log(`[SPT Build System] CopyBundles: ${config.CopyBundles}`);
console.log(`[SPT Build System] BuildZip: ${config.BuildZip}`);
if (config.ContainPlugin) {
console.log(
config.PluginFileName !== ""
? `[SPT Build System] PluginFileName: ${config.PluginFileName}`
: chalk.red("[SPT Build System] PluginFileName: Not configured.")
);
console.log(
config.PluginPath !== ""
? `[SPT Build System] PluginPath: ${config.PluginPath}`
: chalk.red("[SPT Build System] PluginPath: Not configured.")
);
}
console.log(`[SPT Build System] BuildDir: ${config.BuildDir}`);
console.log(`[SPT Build System] CopyToGame: ${config.CopyToGame}`);
console.log(`[SPT Build System] PathToGame: ${config.PathToGame}`);
console.log(`[SPT Build System] StartServer: ${config.StartServer}`);
console.log(`[SPT Build System] StartDelay: ${config.StartDelay}`);
}
console.log(`[SPT Build System] Generated package name: ${modName}`);
if (config.FirstRun) {
// First run on new build system, remove dist folder.
fs.rmSync(`${currentDir}\\dist`, { force: true, recursive: true });
console.log(chalk.yellow("[SPT Build System] First time running npm build."));
console.log(
chalk.yellow("[SPT Build System] Please configure the build.json file.")
);
console.log(
chalk.yellow(
'[SPT Build System] Disable this warning by disabling FirstRun in "build.json".'
)
);
process.exit(1);
}
fs.rmSync(`${currentDir}\\${config.BuildDir}`, {
force: true,
recursive: true,
});
fs.rmSync(`${currentDir}\\${config.BuildDir}`, {
force: true,
recursive: true,
});
console.log(chalk.green("[SPT Build System] Previous build files deleted."));
let exclude;
if (config.CopyBundles === true) {
exclude = glob.sync(`{${config.IgnoreList.join(",")}}`, {
realpath: true,
dot: true,
});
} else {
config.IgnoreList.push("bundles/");
exclude = glob.sync(`{${config.IgnoreList.join(",")}}`, {
realpath: true,
dot: true,
});
}
fs.copySync(currentDir, path.normalize(`${currentDir}/../~${modName}`), {
filter: (src) => {
return !exclude.includes(src);
},
});
//temp folder path (only used with BuildZip enabled in build.json)
const tempPath = path.normalize(`${currentDir}/temp`);
const serverModPath = path.normalize(
`${currentDir}/${config.BuildDir}/user/mods/${modName}`
);
if (config.BuildZip) {
//Creating a temp folder with the mod
fs.moveSync(path.normalize(`${currentDir}/../~${modName}`), tempPath, {
overwrite: true,
});
//Copying temp folder to output (build) folder with the proper structure
fs.copySync(tempPath, serverModPath);
} else {
//No need to create a temp folder
fs.moveSync(path.normalize(`${currentDir}/../~${modName}`), serverModPath, {
overwrite: true,
});
}
if (config.ContainPlugin) {
const pluginFolder =
config.PluginPath !== ""
? path.normalize(`${config.PluginPath}`)
: "Not configured.";
if (fs.existsSync(pluginFolder) && config.PluginPath !== "")
fs.copySync(
path.normalize(`${pluginFolder}/${config.PluginFileName}`),
path.normalize(
`${currentDir}/${config.BuildDir}/BepInEx/plugins/${config.PluginFileName}`
)
);
else
console.log(
chalk.red(
`[SPT Build System] Couldn't find your configured plugin folder: ${pluginFolder}`
)
);
}
console.log(
chalk.green(
`[SPT Build System] Server files built. ${config.BuildDir} directory contains your newly built files.`
)
);
if (config.CopyToGame) {
//const gamePath = path.normalize(`${config.PathToGame}`)
if (fs.existsSync(config.PathToGame)) {
// Delete the existing build before we copy the new build over.
fs.rmSync(`${config.PathToGame}/user/mods/${modName}`, {
force: true,
recursive: true,
});
console.log(
chalk.green("[SPT Build System] Files deleted in game directory.")
);
// Copy files to the game directory
fs.copySync(
path.normalize(`${currentDir}/${config.BuildDir}`),
config.PathToGame
);
console.log(
chalk.green(
`[SPT Build System] Files copied to game path: "${config.PathToGame}".`
)
);
} else
console.log(
chalk.red(
`[SPT Build System] Copy failed, couldn't find your game folder at: "${config.PathToGame}".`
)
);
}
if (config.BuildZip) {
// Create the temporary user/mod path
const relativeZipPath = path.normalize(
path.join(`${tempPath}/`, "user/", "mods/")
);
fs.mkdir(relativeZipPath, { recursive: true }, (err) => {
if (err) {
console.error(
chalk.red(
`[SPT Build System] A error has occurred creating directory "${relativeZipPath}": `,
err.stack
),
err
);
}
});
if (!fs.existsSync(path.normalize(`${currentDir}\\${config.BuildDir}`)))
fs.mkdirSync(path.normalize(`${currentDir}\\${config.BuildDir}`));
zip({
source: "*",
destination: `${currentDir}\\${config.BuildDir}\\${modName}-${
version ?? ""
}.zip`,
cwd: `${currentDir}/${config.BuildDir}`,
})
.catch(function (err) {
console.error(
chalk.red(
"[SPT Build System] A bestzip error has occurred: ",
err.stack
)
);
})
.then(function () {
// remove the temp directories
fs.rmSync(tempPath, { force: true, recursive: true });
console.log(
chalk.green(
`[SPT Build System] Compressed mod package to: ${config.BuildDir}\\${modName}.zip`
)
);
});
}
if (config.StartServer === true && config.PathToGame !== "") {
if (fs.existsSync(config.PathToGame)) {
console.log(
chalk.green(
`[SPT Build System] Server starting in ${config.StartDelay} seconds.`
)
);
setTimeout(() => {
try {
const command = "Aki.Server.Exe";
const output = processHost.execSync(command, {
cwd: config.PathToGame,
stdio: "inherit", // This allows the output to be displayed in the terminal
});
console.log("Command executed successfully:", output.toString());
} catch (error) {
console.error("Error executing command:", error.message);
}
}, config.StartDelay * 1000);
} else {
console.log(
chalk.red(
`[SPT Build System] Server start failed, couldn't find your game folder at: "${config.PathToGame}".`
)
);
}
} else {
if (config.StartServer === false)
console.log(
chalk.yellow("[SPT Build System] Server start is Disabled in config.")
);
else if (config.PathToGame === "")
console.log(
chalk.red(
'[SPT Build System] Server start was ignored because "PathToGame" in "build.json" isn\'t configured.'
)
);
}