-
Notifications
You must be signed in to change notification settings - Fork 1
/
pack.js
executable file
·76 lines (60 loc) · 2.17 KB
/
pack.js
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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
function printHelpAndExit() {
process.stdout.write(`
Usage:
- pack.js <path/to/submarine.sub> [targetdir]
Unpacks a .sub file to XML.
If the targetdir parameter is passed, will unpack to that directory.
Otherwise, will unpack to the current working directory.
- pack.js <path/to/submarine.xml> [targetdir]
Packs an XML file to .sub format.
If targetdir is specified, the .sub file will be created in that folder.
Setting environment variable BAROTRAUMA_SUB_DIRECTORY will default to that directory.
Otherwise, will pack to the currnet working directory.\n\n`);
process.exit(1);
}
let file = process.argv[2];
if (!file) {
printHelpAndExit();
}
const extname = path.extname(file);
if (!~['.sub', '.xml'].indexOf(extname)) {
printHelpAndExit();
}
const isUnpack = path.extname(file) === '.sub';
if (isUnpack) {
extractXml();
} else {
createSub();
}
function createSub() {
const gzip = zlib.createGzip();
const fileIn = fs.createReadStream(file);
const targetDir = process.argv[3] || process.env.BAROTRAUMA_SUB_DIRECTORY || process.cwd();
const fileName = path.parse(file).name;
const outputFileName = path.join(targetDir, fileName + '.sub');
const fileOut = fs.createWriteStream(outputFileName);
fileIn.pipe(gzip).pipe(fileOut).on('finish', () => {
process.stdout.write(`Packed ${fileName}.xml to ${outputFileName}\n`);
process.exit(0);
});
}
function extractXml() {
const gunzip = zlib.createGunzip();
if (!fs.existsSync(file) &&
process.env.BAROTRAUMA_SUB_DIRECTORY && fs.existsSync(path.join(process.env.BAROTRAUMA_SUB_DIRECTORY, file))) {
file = path.join(process.env.BAROTRAUMA_SUB_DIRECTORY, file);
}
const fileIn = fs.createReadStream(file);
const fileName = path.parse(file).name;
const targetDir = process.argv[3] || process.cwd();
const outputFileName = path.join(targetDir, fileName + '.xml');
const fileOut = fs.createWriteStream(outputFileName);
fileIn.pipe(gunzip).pipe(fileOut).on('finish', () => {
process.stdout.write(`Extracted ${fileName}.sub to ${outputFileName}\n`);
process.exit(0);
});
}