-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
93 lines (75 loc) · 3.05 KB
/
cli.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* eslint-disable no-process-exit */
const { program } = require("commander");
const glob = require("glob");
const path = require("path");
const fs = require("fs/promises");
const os = require("os");
const { readQsp, readQsps, writeQsp, writeQsps } = require("@qsp/converters");
const { TextDecoder } = require("util");
const languageEncoding = require("detect-file-encoding-and-language");
program.name("qsp-cli").description("CLI tools for QSP").version("1.0.0");
program
.argument("<input-files>", "File(s) to convert - supports glob patterns")
.option("--directory <output-directory>", "Path to save the converted file")
.option("--unicode", "Save qsps files in utf16 encoding compatible with old txt2gam");
program.parse();
const { unicode, directory: outputDirectory } = program.opts();
const files = program.args.reduce(
(acc, pattern) => [...acc, ...glob.sync(pattern, { nodir: true })],
[]
);
if (!files.length) {
console.log("No matching files found");
process.exit(1);
}
for (const file of new Set(files)) {
converFile(file, outputDirectory, unicode);
}
function converFile(filePath, outputDirectory, unicode) {
const ext = path.extname(filePath).toLowerCase();
switch (ext) {
case ".qsp":
case ".gam":
return convertQspFile(filePath, outputDirectory, unicode);
case ".qsps":
case ".qsp-txt":
case ".txt-qsp":
return convertQspsFile(filePath, outputDirectory);
}
console.log(`Unknown file format: ${ext}`);
process.exit(1);
}
async function convertQspFile(filePath, outputDirectory, unicode) {
const content = await fs.readFile(path.resolve(filePath));
const locations = readQsp(content.buffer);
let converted = writeQsps(locations, os.EOL);
if (unicode) {
const utf16buffer = Buffer.from(`\ufeff${converted}`, "utf16le");
converted = new Uint8Array(utf16buffer);
}
const outPath = await writeFile(filePath, outputDirectory, ".qsps", converted);
console.log(`Finished converting ${filePath} -> ${outPath}`);
}
async function convertQspsFile(filePath, outputDirectory) {
let { encoding } = await languageEncoding(filePath);
const data = await fs.readFile(path.resolve(filePath));
if (!encoding) {
encoding = data[1] === 0 ? 'utf-16le' : 'utf-8';
}
const decoder = new TextDecoder(encoding || 'utf-8');
const content = decoder.decode(data);
const locations = readQsps(content);
const converted = writeQsp(locations);
const outPath = await writeFile(filePath, outputDirectory, ".qsp", new Uint8Array(converted));
console.log(`Finished converting ${filePath} -> ${outPath}`);
}
async function writeFile(filePath, outputDirectory, outExtension, content) {
const ext = path.extname(filePath);
const basename = path.basename(filePath, ext);
const directory = path.dirname(filePath);
const targetDirectory = path.resolve(outputDirectory || directory);
const outPath = path.resolve(targetDirectory, basename + outExtension);
await fs.mkdir(targetDirectory, { recursive: true });
await fs.writeFile(outPath, content);
return `${outputDirectory || directory}/${basename}${outExtension}`;
}