This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path8pack.js
78 lines (65 loc) · 2.43 KB
/
8pack.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
const
assert = require("assert")
, argv = require("minimist")(process.argv.slice(2))
, fs = require("fs")
, path = require("path")
, {getVersion} = require("./src/versions")
, writeOutput = (path, src) => {
fs.writeFileSync(path, src);
console.log("writing " + path);
}
, readmodules = require("./src/readmodules")
;
if (!argv._.length) {
console.warn("missing mandatory arguments");
//show man page
console.log(require("./src/help"));
process.exit(0);
}
if (argv.h || argv.help) {
//show man page
console.log(require("./src/help"));
process.exit(0);
}
assert(argv._.length <= 2, "too many arguments");
const input = path.resolve(argv._[0]); //use input path from arguments
assert.ok(input, "missing mandatory argument 'input'"); //throw when missing input
assert.doesNotThrow(() => fs.existsSync(input), `couldn't locate input file under ${input}`);
const output = argv._[1]; //use output from arguments
if (output) {
assert.doesNotThrow(() => fs.existsSync(output), `couldn't locate output file under ${output}`);
}
const watch = argv.w || argv.watch;
const template = argv.t || argv.template;
console.log("using template", template);
//read from input path
const fileInputPath = path.resolve(input);
console.log("reading " + fileInputPath);
//inject input to inject path
const fileInjectPath = output ? path.resolve(output) : getVersion(template); //use specified file from output argument or template
//output the result to output path
const fileOutputPath = output ? path.resolve(output) : fileInputPath + ".p8"; //create a new file when not specified the output
console.log(`using ${fileOutputPath} as output`);
//read input files, find & collect modules
readmodules(fileInputPath);
const picoSource = fs.readFileSync(fileInjectPath, "utf8"); //contents of output file
const src = [
picoSource.slice(0, picoSource.indexOf("__lua__") + 7),
readmodules.convertInjects(),
picoSource.slice(picoSource.indexOf("__gfx__")),
]
.join("\n");
if (watch) {
console.log("watching for file changes on " + fileInputPath);
fs.watch(fileInputPath, {encoding: "buffer"}, (eventType, filename) => {
if (filename && eventType === "change") {
console.log("changes detected on " + filename.toString());
writeOutput(fileOutputPath, src);
}
});
}
else {
writeOutput(fileOutputPath, src);
console.log("done");
process.exit(0);
}