Skip to content

Commit

Permalink
NPM Compression Scripts
Browse files Browse the repository at this point in the history
Adds two NPM scripts to handle compressing and decompressing the locations data in a reproducible way.

Needs to be tested on Windows.
  • Loading branch information
refringe committed Nov 27, 2024
1 parent 464fa27 commit ff364c8
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ yarn.lock
## windows
desktop.ini

## OSX
.DS_Store

## parcel
.parcel-cache/

Expand Down
10 changes: 7 additions & 3 deletions project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"node": "20.11.1"
},
"scripts": {
"postinstall": "node scripts/fix-7za-permissions.js",
"check:circular": "madge --circular --ts-config tsconfig.json --extensions ts ./src/",
"lint": "npx @biomejs/biome lint ./",
"lint:fix": "npx @biomejs/biome lint --write ./",
Expand All @@ -31,7 +32,9 @@
"gen:types": "tsc -p tsconfig.typedef.json --resolveJsonModule",
"gen:docs": "typedoc --options ./typedoc.json --entryPointStrategy expand ./src",
"gen:items": "ts-node -r tsconfig-paths/register ./src/tools/ItemTplGenerator/ItemTplGeneratorProgram.ts",
"gen:productionquests": "ts-node -r tsconfig-paths/register ./src/tools/ProductionQuestsGen/ProductionQuestsGenProgram.ts"
"gen:productionquests": "ts-node -r tsconfig-paths/register ./src/tools/ProductionQuestsGen/ProductionQuestsGenProgram.ts",
"database:compress": "node scripts/databaseCompress.js",
"database:decompress": "node scripts/databaseDecompress.js"
},
"dependencies": {
"atomically": "~1.7",
Expand Down Expand Up @@ -68,6 +71,7 @@
"@vitest/ui": "~2",
"@yao-pkg/pkg": "5.12",
"@yao-pkg/pkg-fetch": "3.5.9",
"7zip-bin": "^5.2.0",
"cross-env": "~7.0",
"fs-extra": "~11.2",
"gulp": "~5.0",
Expand All @@ -77,11 +81,11 @@
"gulp-rename": "~2.0",
"madge": "~7",
"minimist": "~1.2",
"node-7z": "^3.0.0",
"resedit": "~2.0",
"ts-node-dev": "~2.0",
"tsconfig-paths": "~4.2",
"typedoc": "~0.26",
"typemoq": "~2.1"
"typedoc": "~0.26"
},
"targets": {
"default": {
Expand Down
31 changes: 31 additions & 0 deletions project/scripts/databaseCompress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// This script compresses the locations database into a 7z archive.

const Seven = require("node-7z");
const path = require("node:path");
const { path7za } = require("7zip-bin");

const archivePath = path.resolve(__dirname, "../assets/compressed/locations.7z");
const locationsDir = path.resolve(__dirname, "../assets/database/locations/*");

let hadError = false;

console.log(archivePath);
console.log(locationsDir);

const myStream = Seven.add(archivePath, locationsDir, {
recursive: true,
$bin: path7za,
method: ["0=LZMA2"],
compressionLevel: 9,
});

myStream.on("end", () => {
if (!hadError) {
console.log("Compression completed successfully.");
}
});

myStream.on("error", (err) => {
hadError = true;
console.error(`Error compressing locations: ${err}`);
});
47 changes: 47 additions & 0 deletions project/scripts/databaseDecompress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// This script removes the contents of the locations directory and then decompresses
// the locations database from a 7z archive.

const Seven = require("node-7z");
const path = require("node:path");
const fs = require("fs-extra");
const { path7za } = require("7zip-bin");

const archivePath = path.resolve(__dirname, "../assets/compressed/locations.7z");
const databaseDir = path.resolve(__dirname, "../assets/database/locations");

(async () => {
try {
const archiveExists = await fs.pathExists(archivePath);
if (!archiveExists) {
console.error("Error: Archive file does not exist:", archivePath);
process.exit(1);
}

const locationsDir = path.join(databaseDir, "locations");
if (await fs.pathExists(locationsDir)) {
await fs.remove(locationsDir);
console.log("Existing locations directory removed.");
}

let hadError = false;

const myStream = Seven.extractFull(archivePath, databaseDir, {
$bin: path7za,
overwrite: "a",
});

myStream.on("end", () => {
if (!hadError) {
console.log("Decompression completed successfully.");
}
});

myStream.on("error", (err) => {
hadError = true;
console.error(`Error decompressing locations: ${err}`);
});
} catch (err) {
console.error(`Error during decompression: ${err}`);
process.exit(1);
}
})();
30 changes: 30 additions & 0 deletions project/scripts/fix-7za-permissions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// This script sets the execute permission on the 7za binary if you're on macOS or Linux.

const fs = require("node:fs");
const path = require("node:path");
const os = require("node:os");

const platform = os.platform();
const arch = os.arch();

let sevenZipPath;

if (platform === "darwin") {
// macOS
sevenZipPath = path.join(__dirname, "..", "node_modules", "7zip-bin", "mac", arch, "7za");
} else if (platform === "linux") {
// Linux
sevenZipPath = path.join(__dirname, "..", "node_modules", "7zip-bin", "linux", arch, "7za");
} else {
// Windows (or other)
process.exit(0);
}

fs.chmod(sevenZipPath, 0o755, (err) => {
if (err) {
console.error("Failed to set execute permission on 7za:", err);
process.exit(1);
} else {
console.log("Execute permission set on 7za.");
}
});

0 comments on commit ff364c8

Please sign in to comment.