-
Notifications
You must be signed in to change notification settings - Fork 0
/
packageBuild.ts
100 lines (90 loc) · 3.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
#!/usr/bin/env node
// This is a simple script used to build a mod package. The script will copy necessary files to the build directory
// and compress the build directory into a zip file that can be easily shared.
const fs = require("fs-extra");
const glob = require("glob");
const zip = require("bestzip");
const path = require("path");
// Load the package.json file to get some information about the package so we can name things appropriately. This is
// atypical, and you would never do this in a production environment, but this script is only used for development so
// it's fine in this case. Some of these values are stored in environment variables, but those differ between node
// versions; the 'author' value is not available after node v14.
const { author, name: packageName, version } = require("./package.json");
// Generate the name of the package, stripping out all non-alphanumeric characters in the 'author' and 'name'.
const modName = `${author.replace(/[^a-z0-9]/gi, "")}-${packageName.replace(
/[^a-z0-9]/gi,
""
)}-${version}`;
console.log(`Generated package name: ${modName}`);
// Delete the old build directory and compressed package file.
fs.rmSync(`${__dirname}/dist`, { force: true, recursive: true });
console.log("Previous build files deleted.");
// Generate a list of files that should not be copied over into the distribution directory. This is a blacklist to ensure
// we always copy over additional files and directories that authors may have added to their project. This may need to be
// expanded upon by the mod author to allow for node modules that are used within the mod; example commented out below.
const ignoreList = [
"node_modules/",
// "node_modules/!(weighted|glob)", // Instead of excluding the entire node_modules directory, allow two node modules.
"src/**/*.js",
"types/",
".git/",
".gitea/",
".eslintignore",
".eslintrc.json",
".gitignore",
".DS_Store",
"packageBuild.ts",
"mod.code-workspace",
"package-lock.json",
"tsconfig.json",
"deploy.bat",
];
const exclude = glob.sync(`{${ignoreList.join(",")}}`, {
realpath: true,
dot: true,
});
// For some reason these basic-bitch functions won't allow us to copy a directory into itself, so we have to resort to
// using a temporary directory, like an idiot. Excuse the normalize spam; some modules cross-platform, some don't...
fs.copySync(__dirname, path.normalize(`${__dirname}/../~${modName}`), {
filter: (filePath) => {
return !exclude.includes(filePath);
},
});
fs.moveSync(
path.normalize(`${__dirname}/../~${modName}`),
path.normalize(`${__dirname}/${modName}`),
{ overwrite: true }
);
fs.copySync(
path.normalize(`${__dirname}/${modName}`),
path.normalize(`${__dirname}/dist`)
);
console.log("Build files copied.");
// Compress the files for easy distribution. The compressed file is saved into the dist directory. When uncompressed we
// need to be sure that it includes a directory that the user can easily copy into their game mods directory.
zip({
source: modName,
destination: `dist/${modName}.zip`,
cwd: __dirname,
})
.catch(function (err) {
console.error("A bestzip error has occurred: ", err.stack);
})
.then(function () {
console.log(`Compressed mod package to: /dist/${modName}.zip`);
// Now that we're done with the compression we can delete the temporary build directory.
fs.rmSync(`${__dirname}/${modName}`, { force: true, recursive: true });
console.log(
"Build successful! your zip file has been created and is ready to be uploaded to hub.sp-tarkov.com/files/"
);
// Run deploy.bat now for testing
const exec = require("child_process").exec;
exec("deploy.bat", (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
console.log("deploy.bat has been run successfully!");
});
});