-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
executable file
·129 lines (112 loc) · 3.21 KB
/
index.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env node --experimental-json-modules
import { parseArgs } from 'node:util';
import { basename, extname } from 'node:path';
import { open, writeFile } from 'node:fs/promises';
import pkg from './package.json' with { type: 'json' };
import { loadRom } from './src/lib/cliUtils.js';
import parseRom from './src/lib/parser/parseRom.js';
import { stringifyResources } from './src/lib/cliUtils.js';
const options = {
version: {
type: 'boolean',
short: 'v',
default: false,
},
help: {
type: 'boolean',
short: 'h',
default: false,
},
input: {
type: 'string',
short: 'i',
},
'base-rom': {
type: 'string',
short: 'b',
},
output: {
type: 'string',
short: 'o',
},
};
const { values } = parseArgs({
options,
strict: true,
});
if (values.version) {
const { version } = pkg;
// See https://www.gnu.org/prep/standards/html_node/_002d_002dversion.html
console.log(`scumm-nes ${version}`);
process.exit(0);
}
if (values.help || (!values.input && !values.output)) {
const { description, version, bugs, homepage } = pkg;
// See https://www.gnu.org/prep/standards/html_node/_002d_002dhelp.html
console.log(`\x1b[1mDescription\x1b[0m
${description} [version ${version}]
\x1b[1mSynopsis\x1b[0m
This CLI exports the parsed resources of a ROM or PRG to JSON.
\x1b[1mUsage\x1b[0m
node ${basename(import.meta.url)} --input path/to/rom --output resources.json
\x1b[1mOptions\x1b[0m
--input, -i Path to a ROM or PRG file.
--base-rom, -b The base ROM version of the input ROM.
--output, -o Path to a JSON filename to be created.
--version, -v Print the version number.
Report bugs to: <\x1b[1m${bugs.url || bugs}\x1b[0m>
Home page: <\x1b[1m${homepage}\x1b[0m>`);
process.exit(0);
}
if (!values.input && !values.output) {
console.error(
'The \x1b[1m--input\x1b[0m and \x1b[1m--output\x1b[0m arguments are required.',
);
process.exit(1);
}
if (!values.input) {
console.error('The \x1b[1m--input\x1b[0m argument is required.');
process.exit(1);
}
if (!values.output) {
console.error('The \x1b[1m--output\x1b[0m argument is required.');
process.exit(1);
}
const outputExtname = extname(values.output).toLowerCase();
if (outputExtname !== '.json') {
console.error(
'Specify a \x1b[1m.json\x1b[0m file for the \x1b[1m--output\x1b[0m argument.',
);
process.exit(1);
}
// Check that the output file does not already exist.
let outputFileExists = null;
try {
await open(values.output);
outputFileExists = true;
} catch (e) {
outputFileExists = false;
}
if (outputFileExists) {
console.error(`File '${values.output}' already exists.`);
process.exit(1);
}
// Load the input file.
let rom, res, hash;
try {
({ rom, res, hash } = await loadRom(values.input, values['base-rom']));
} catch (err) {
console.error(`Input ROM file could not be opened.`);
console.error(err);
process.exit(1);
}
const resources = parseRom(rom, res);
const content = stringifyResources(hash, rom.byteLength, resources, res);
try {
await writeFile(values.output, content, { encoding: 'utf-8' });
} catch (err) {
console.error(`Error writing output file '${values.output}'.`);
console.error(err);
process.exit(1);
}
console.log('Output file successfully written.');