-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
78 lines (73 loc) · 2.14 KB
/
main.ts
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
import nearley from 'nearley';
const grammar = require('./grammar.js');
import arg from 'arg';
import * as Types from './types';
import { readFileSync } from 'fs';
import { Generator } from './fileGeneration';
import util from 'util';
import log from 'loglevel';
import prefix from 'loglevel-plugin-prefix';
import chalk from 'chalk';
import path from 'path';
const colors = {
TRACE: chalk.magenta,
DEBUG: chalk.cyan,
INFO: chalk.blue,
WARN: chalk.yellow,
ERROR: chalk.red,
};
prefix.reg(log);
log.enableAll();
prefix.apply(log, {
format(level, name, timestamp) {
return `${chalk.gray(`[${timestamp}]`)} ${colors[
level.toUpperCase() as 'ERROR' // Prevent typescript from being silly
](level)}`;
},
});
prefix.apply(log.getLogger('critical'), {
format(level, name, timestamp) {
return chalk.red.bold(`[${timestamp}] ${level} ${name}:`);
},
});
// Parse command arguments
const args = arg({
// Types
'--help': Boolean,
'--compile': String,
'--output': String,
'-c': '--compile',
'-o': '--output',
});
if (args['--help']) {
console.log('--compile <file> --output <sb3 name> Compile a file');
process.exit(0);
} else if (args['--compile'] && args['--output']) {
log.info(`Parsing ${args['--compile']}`);
let fileData = readFileSync(args['--compile']).toString();
// Fill in @include statements
const includeRegex = /@include "(.*)"\n/g;
const includes = fileData.matchAll(includeRegex);
if (includes)
for (const include of includes) {
fileData = fileData.replace(
include[0],
readFileSync(
path.dirname(args['--compile']) + '/' + include[1]
).toString()
);
}
// Parse data
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
parser.feed(fileData + '\n');
const results = parser.results[0] as Array<Types.Token>;
console.log(
util.inspect(results, { showHidden: false, depth: null, colors: true })
);
log.info(`Compiling ${args['--compile']}`);
Generator.blank().createFromParse(results).exportToFile(args['--output']);
log.info('Finished compiling');
} else {
log.error('No valid options given');
process.exit(1);
}