-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.mjs
More file actions
71 lines (59 loc) · 2.46 KB
/
Copy pathcli.mjs
File metadata and controls
71 lines (59 loc) · 2.46 KB
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
#!/usr/bin/env node
// pjfix CLI — inline the core into game HTML files and rebuild the userscript.
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { buildUserscript, die, patchHtml, unpatchHtml } from './lib.mjs';
const dirname = path.dirname(fileURLToPath(import.meta.url));
const corePath = path.join(dirname, 'pointer-jump-fix.js');
const userPath = path.join(dirname, 'pointer-jump-fix.user.js');
const HELP = `Usage: pjfix [files...] [options]
Inline pointer-jump-fix core into HTML files (backup as *.orig),
and rebuild the userscript.
No Node project? Run it without installing: npx -y pjfix ...
Targets default to *.html in the current directory.
Options:
--help, -h show this help
--version, -V print the version
--root <dir> base directory for targets (default: current directory)
--unpatch restore targets from *.orig backups
Examples:
npx -y pjfix all *.html in the current directory
npx -y pjfix a.html b.html only these files
npx -y pjfix --root .. all *.html in the parent directory
npx -y pjfix --unpatch restore from backups`;
function parseArgs(argv) {
const opts = { unpatch: false, root: process.cwd(), files: [] };
for (let i = 0; i < argv.length; i++) {
const arg = argv[i];
if (arg === '--help' || arg === '-h') return { help: true };
if (arg === '--version' || arg === '-V') return { version: true };
if (arg === '--unpatch') opts.unpatch = true;
else if (arg === '--root') opts.root = path.resolve(argv[++i]);
else if (arg.startsWith('-')) die(`unknown option: ${arg}`);
else opts.files.push(arg);
}
return opts;
}
function main() {
const opts = parseArgs(process.argv.slice(2));
if (opts.help) {
console.log(HELP);
process.exit(0);
}
if (opts.version) {
const { version } = JSON.parse(fs.readFileSync(path.join(dirname, 'package.json'), 'utf8'));
console.log(version);
process.exit(0);
}
const core = fs.readFileSync(corePath, 'utf8');
if (!opts.unpatch) buildUserscript(core, userPath);
const targets = opts.files.length > 0
? opts.files.map((f) => path.resolve(opts.root, f))
: fs.readdirSync(opts.root).filter((f) => f.endsWith('.html')).map((f) => path.join(opts.root, f));
for (const file of targets) {
const result = opts.unpatch ? unpatchHtml(file) : patchHtml(file, core);
console.log(result.message);
}
}
main();