-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
90 lines (72 loc) · 2.81 KB
/
cli.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
#!/usr/bin/env node
'use strict'
const fs = require("fs");
const path = require("path");
const { ArgumentParser } = require("argparse");
const { performance } = require("perf_hooks");
const onelua = require("./onelua.js");
function cli() {
/* Parse args */
const parser = new ArgumentParser({
description: 'Argparse example',
add_help: true
});
parser.add_argument('source', { help: "Path to the Lua project or entry point Lua file" });
parser.add_argument('-o', '--output', { help: "Path to the output Lua file" });
parser.add_argument('--debug', { help: "Turn on debugging logs", action: 'store_true' });
parser.add_argument('--no-minify', { help: "Turn off minified output", action: 'store_true' });
parser.add_argument('--prepend-meta', { help: "Prepend the name & date-time generated", action: 'store_true' });
const args = parser.parse_args();
if (!fs.existsSync(args.source)) {
console.log(`Error: entry point Lua source file not found (${args.source})`);
return 1;
}
var entryFile = null;
var outputFile = args.output;
if (fs.lstatSync(args.source).isDirectory()) {
var pkgPath = path.resolve(args.source, "package.json");
var pkgCfg = require(pkgPath);
if (!pkgCfg) {
console.log(`Error: directory specified, but package.json not found (${args.source})`);
return 1;
}
if (!pkgCfg.onelua) {
console.log(`Error: package.json found, but has no onelua build instructions`);
return 1;
}
entryFile = pkgCfg.onelua.main;
if (!entryFile) {
console.log(`Error: no main file was specified in package.json)`);
return 1;
}
if (!outputFile) {
outputFile = pkgCfg.onelua.output;
if (!outputFile) {
console.log(`Error: no output file was specified in package.json)`);
return 1;
}
}
} else {
entryFile = args.source;
}
var time_start = performance.now();
var output = onelua.process(entryFile, { debug: args.debug, minify: !args.no_minify });
if (args.prepend_meta) {
let name = path.basename(outputFile);
let time = new Date().toUTCString();
output = `--[[\n ${name}\n Generated on ${time}\n]]--\n` + output;
}
try {
fs.writeFileSync(outputFile, output);
} catch (err) {
console.error(`An error occurred while trying to write to ${outputFile}`)
if (err) console.error(err);
return 1;
}
var time_end = performance.now();
var seconds_taken = ((time_end - time_start) / 1000).toFixed(2);
console.log(`> Wrote file to ${path.resolve(outputFile)}\nBuild successful! (${seconds_taken}s)`)
return 0;
}
if (cli() != 0)
process.exit(1);