minecraft-java-core
Version:
A library starting minecraft game NW.js and Electron.js
188 lines (185 loc) ⢠6.92 kB
JavaScript
;
/**
* @author Luuxis
* Luuxis License v1.0 (see LICENSE file for FR/EN details)
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runZipper = runZipper;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const child_process_1 = require("child_process");
const DEFAULT_OPTIONS = {
keep: false,
dryRun: false,
level: 9,
dirs: ['resourcepacks', 'shaderpacks', 'datapacks'],
target: null,
};
const HELP_TEXT = `mjc-zip ā Compress subfolders of resourcepacks/shaderpacks/datapacks.
Usage:
npx mjc-zip [target] [options]
Arguments:
target Instance folder to process (default: cwd)
Options:
--keep Keep original folders after compression
--dry-run, -n Do not write anything, only show what would be done
--level=0..9 Zip compression level (default: 9)
--dirs=a,b,c Subfolders to process (default: resourcepacks,shaderpacks,datapacks)
-h, --help Show this help`;
function parseArgs(argv) {
const opts = { ...DEFAULT_OPTIONS, dirs: [...DEFAULT_OPTIONS.dirs] };
for (const a of argv) {
if (a === '--keep') {
opts.keep = true;
}
else if (a === '--dry-run' || a === '-n') {
opts.dryRun = true;
}
else if (a.startsWith('--level=')) {
const parsed = parseInt(a.split('=')[1], 10);
opts.level = Math.max(0, Math.min(9, Number.isNaN(parsed) ? 9 : parsed));
}
else if (a.startsWith('--dirs=')) {
opts.dirs = a.split('=')[1].split(',').map(s => s.trim()).filter(Boolean);
}
else if (a === '-h' || a === '--help') {
console.log(HELP_TEXT);
process.exit(0);
}
else if (!a.startsWith('-') && opts.target === null) {
opts.target = a;
}
else {
console.error(`ā Unknown argument: ${a}`);
process.exit(1);
}
}
return opts;
}
function hasZipCommand() {
try {
(0, child_process_1.execSync)('zip -v', { stdio: 'ignore' });
return true;
}
catch {
return false;
}
}
function dirSize(dir) {
let total = 0;
for (const entry of fs_1.default.readdirSync(dir, { withFileTypes: true })) {
const p = path_1.default.join(dir, entry.name);
if (entry.isDirectory())
total += dirSize(p);
else if (entry.isFile())
total += fs_1.default.statSync(p).size;
}
return total;
}
function fmtBytes(n) {
const units = ['B', 'KB', 'MB', 'GB'];
let i = 0;
while (n >= 1024 && i < units.length - 1) {
n /= 1024;
i++;
}
return `${n.toFixed(2)} ${units[i]}`;
}
function rmDir(p) {
fs_1.default.rmSync(p, { recursive: true, force: true });
}
function runZipper(options = {}) {
const opts = {
...DEFAULT_OPTIONS,
...options,
dirs: options.dirs ?? [...DEFAULT_OPTIONS.dirs],
};
const root = path_1.default.resolve(process.cwd(), opts.target || '.');
if (!fs_1.default.existsSync(root) || !fs_1.default.statSync(root).isDirectory()) {
throw new Error(`Folder not found: ${root}`);
}
if (!hasZipCommand()) {
throw new Error('The "zip" system command was not found. macOS/Linux: already installed. Windows: use WSL or install Info-ZIP.');
}
console.log(`š Target: ${root}`);
let totalBefore = 0;
let totalAfter = 0;
let processed = 0;
let skipped = 0;
for (const sub of opts.dirs) {
const base = path_1.default.join(root, sub);
if (!fs_1.default.existsSync(base) || !fs_1.default.statSync(base).isDirectory()) {
console.log(`⢠${sub}/ : not found, skipped`);
continue;
}
console.log(`\n=== ${sub}/ ===`);
const entries = fs_1.default.readdirSync(base, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isDirectory())
continue;
const srcDir = path_1.default.join(base, entry.name);
const zipPath = path_1.default.join(base, `${entry.name}.zip`);
if (fs_1.default.existsSync(zipPath)) {
console.log(` - ${entry.name} : ${path_1.default.basename(zipPath)} already exists, skipped`);
skipped++;
continue;
}
const sizeBefore = dirSize(srcDir);
console.log(` ā ${entry.name} (${fmtBytes(sizeBefore)})`);
if (opts.dryRun) {
processed++;
totalBefore += sizeBefore;
continue;
}
const result = (0, child_process_1.spawnSync)('zip', ['-r', '-q', `-${opts.level}`, zipPath, entry.name], { cwd: base, stdio: 'inherit' });
if (result.status !== 0) {
console.error(` ā failed to compress ${entry.name}`);
if (fs_1.default.existsSync(zipPath))
fs_1.default.unlinkSync(zipPath);
continue;
}
const sizeAfter = fs_1.default.statSync(zipPath).size;
const ratio = sizeBefore > 0 ? 100 - (sizeAfter / sizeBefore) * 100 : 0;
console.log(` ā ${fmtBytes(sizeBefore)} ā ${fmtBytes(sizeAfter)} (-${ratio.toFixed(1)}%)`);
totalBefore += sizeBefore;
totalAfter += sizeAfter;
processed++;
if (!opts.keep) {
rmDir(srcDir);
}
}
}
console.log('\nāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā');
console.log(`Folders processed: ${processed}`);
console.log(`Folders skipped : ${skipped}`);
if (!opts.dryRun && processed > 0) {
const saved = totalBefore - totalAfter;
const ratio = totalBefore > 0 ? (saved / totalBefore) * 100 : 0;
console.log(`Before: ${fmtBytes(totalBefore)}`);
console.log(`After : ${fmtBytes(totalAfter)}`);
console.log(`Saved : ${fmtBytes(saved)} (-${ratio.toFixed(1)}%)`);
if (!opts.keep)
console.log('Original folders have been removed.');
else
console.log('Original folders have been kept (--keep).');
}
else if (opts.dryRun) {
console.log('(--dry-run) no changes were made.');
}
return { processed, skipped, totalBefore, totalAfter };
}
if (require.main === module) {
try {
const opts = parseArgs(process.argv.slice(2));
runZipper(opts);
}
catch (err) {
console.error(`ā ${err.message}`);
process.exit(1);
}
}
//# sourceMappingURL=Minecraft-resourceziper.js.map