Skip to content

Commit ff364c8

Browse files
committed
NPM Compression Scripts
Adds two NPM scripts to handle compressing and decompressing the locations data in a reproducible way. Needs to be tested on Windows.
1 parent 464fa27 commit ff364c8

File tree

5 files changed

+118
-3
lines changed

5 files changed

+118
-3
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ yarn.lock
3232
## windows
3333
desktop.ini
3434

35+
## OSX
36+
.DS_Store
37+
3538
## parcel
3639
.parcel-cache/
3740

project/package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"node": "20.11.1"
1212
},
1313
"scripts": {
14+
"postinstall": "node scripts/fix-7za-permissions.js",
1415
"check:circular": "madge --circular --ts-config tsconfig.json --extensions ts ./src/",
1516
"lint": "npx @biomejs/biome lint ./",
1617
"lint:fix": "npx @biomejs/biome lint --write ./",
@@ -31,7 +32,9 @@
3132
"gen:types": "tsc -p tsconfig.typedef.json --resolveJsonModule",
3233
"gen:docs": "typedoc --options ./typedoc.json --entryPointStrategy expand ./src",
3334
"gen:items": "ts-node -r tsconfig-paths/register ./src/tools/ItemTplGenerator/ItemTplGeneratorProgram.ts",
34-
"gen:productionquests": "ts-node -r tsconfig-paths/register ./src/tools/ProductionQuestsGen/ProductionQuestsGenProgram.ts"
35+
"gen:productionquests": "ts-node -r tsconfig-paths/register ./src/tools/ProductionQuestsGen/ProductionQuestsGenProgram.ts",
36+
"database:compress": "node scripts/databaseCompress.js",
37+
"database:decompress": "node scripts/databaseDecompress.js"
3538
},
3639
"dependencies": {
3740
"atomically": "~1.7",
@@ -68,6 +71,7 @@
6871
"@vitest/ui": "~2",
6972
"@yao-pkg/pkg": "5.12",
7073
"@yao-pkg/pkg-fetch": "3.5.9",
74+
"7zip-bin": "^5.2.0",
7175
"cross-env": "~7.0",
7276
"fs-extra": "~11.2",
7377
"gulp": "~5.0",
@@ -77,11 +81,11 @@
7781
"gulp-rename": "~2.0",
7882
"madge": "~7",
7983
"minimist": "~1.2",
84+
"node-7z": "^3.0.0",
8085
"resedit": "~2.0",
8186
"ts-node-dev": "~2.0",
8287
"tsconfig-paths": "~4.2",
83-
"typedoc": "~0.26",
84-
"typemoq": "~2.1"
88+
"typedoc": "~0.26"
8589
},
8690
"targets": {
8791
"default": {

project/scripts/databaseCompress.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// This script compresses the locations database into a 7z archive.
2+
3+
const Seven = require("node-7z");
4+
const path = require("node:path");
5+
const { path7za } = require("7zip-bin");
6+
7+
const archivePath = path.resolve(__dirname, "../assets/compressed/locations.7z");
8+
const locationsDir = path.resolve(__dirname, "../assets/database/locations/*");
9+
10+
let hadError = false;
11+
12+
console.log(archivePath);
13+
console.log(locationsDir);
14+
15+
const myStream = Seven.add(archivePath, locationsDir, {
16+
recursive: true,
17+
$bin: path7za,
18+
method: ["0=LZMA2"],
19+
compressionLevel: 9,
20+
});
21+
22+
myStream.on("end", () => {
23+
if (!hadError) {
24+
console.log("Compression completed successfully.");
25+
}
26+
});
27+
28+
myStream.on("error", (err) => {
29+
hadError = true;
30+
console.error(`Error compressing locations: ${err}`);
31+
});

project/scripts/databaseDecompress.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// This script removes the contents of the locations directory and then decompresses
2+
// the locations database from a 7z archive.
3+
4+
const Seven = require("node-7z");
5+
const path = require("node:path");
6+
const fs = require("fs-extra");
7+
const { path7za } = require("7zip-bin");
8+
9+
const archivePath = path.resolve(__dirname, "../assets/compressed/locations.7z");
10+
const databaseDir = path.resolve(__dirname, "../assets/database/locations");
11+
12+
(async () => {
13+
try {
14+
const archiveExists = await fs.pathExists(archivePath);
15+
if (!archiveExists) {
16+
console.error("Error: Archive file does not exist:", archivePath);
17+
process.exit(1);
18+
}
19+
20+
const locationsDir = path.join(databaseDir, "locations");
21+
if (await fs.pathExists(locationsDir)) {
22+
await fs.remove(locationsDir);
23+
console.log("Existing locations directory removed.");
24+
}
25+
26+
let hadError = false;
27+
28+
const myStream = Seven.extractFull(archivePath, databaseDir, {
29+
$bin: path7za,
30+
overwrite: "a",
31+
});
32+
33+
myStream.on("end", () => {
34+
if (!hadError) {
35+
console.log("Decompression completed successfully.");
36+
}
37+
});
38+
39+
myStream.on("error", (err) => {
40+
hadError = true;
41+
console.error(`Error decompressing locations: ${err}`);
42+
});
43+
} catch (err) {
44+
console.error(`Error during decompression: ${err}`);
45+
process.exit(1);
46+
}
47+
})();
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// This script sets the execute permission on the 7za binary if you're on macOS or Linux.
2+
3+
const fs = require("node:fs");
4+
const path = require("node:path");
5+
const os = require("node:os");
6+
7+
const platform = os.platform();
8+
const arch = os.arch();
9+
10+
let sevenZipPath;
11+
12+
if (platform === "darwin") {
13+
// macOS
14+
sevenZipPath = path.join(__dirname, "..", "node_modules", "7zip-bin", "mac", arch, "7za");
15+
} else if (platform === "linux") {
16+
// Linux
17+
sevenZipPath = path.join(__dirname, "..", "node_modules", "7zip-bin", "linux", arch, "7za");
18+
} else {
19+
// Windows (or other)
20+
process.exit(0);
21+
}
22+
23+
fs.chmod(sevenZipPath, 0o755, (err) => {
24+
if (err) {
25+
console.error("Failed to set execute permission on 7za:", err);
26+
process.exit(1);
27+
} else {
28+
console.log("Execute permission set on 7za.");
29+
}
30+
});

0 commit comments

Comments
 (0)