-
Notifications
You must be signed in to change notification settings - Fork 62
/
genKaitaiFsFiles.js
43 lines (36 loc) · 1.15 KB
/
genKaitaiFsFiles.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
const { readdirSync, statSync, writeFileSync, mkdirSync } = require("fs");
const { join } = require("path");
function recursiveFind(dir, pattern, results = []) {
const files = readdirSync(dir, "utf-8")
.map(fn => join(dir, fn))
.map(fn => ({ stat: statSync(fn), fn }));
results.push.apply(
results,
files
.filter(s => s.stat.isFile())
.map(s => s.fn)
.filter(fn => pattern.test(fn))
);
files
.filter(s => s.stat.isDirectory())
.forEach(s => recursiveFind(s.fn, pattern, results));
return results;
}
function generate(outDir) {
const files =
recursiveFind("formats/", /\.ksy$/)
.concat(recursiveFind("samples/", /.+/))
.map(path => path.replace(/\\/g,'/'));
files.sort();
const js = `var kaitaiFsFiles = ${JSON.stringify(files, null, 4)};`;
mkdirSync(outDir + "js", { recursive: true });
writeFileSync(outDir + "js/kaitaiFsFiles.js", js);
}
function main() {
const outDir = process.argv.length > 2 ? process.argv[2] + "/" : "";
generate(outDir);
}
if (!module.parent) {
main();
}
module.exports = generate;