LemonJS/main.js

92 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-01-28 12:08:59 +00:00
/**
* Created by Aleksey Chichenkov <a.chichenkov@initi.ru> on 1/28/19.
*/
2019-01-30 15:03:10 +00:00
var js_beautify = require("./libs/js-beautifier");
var args = require("./libs/args-parser")(process.argv);
2019-01-28 12:08:59 +00:00
var fs = require("fs");
var exec = require('child_process').exec;
2019-01-29 08:02:47 +00:00
var config;
if(args["c"] !== undefined) {
config = require("./" + args["c"]);
} else {
config = require("./config.js");
}
var program_path = config.lemon_bin;
var lemon_flags = config.flags;
2019-01-30 11:50:02 +00:00
var input_path = config.input_path;
var output_path = config.output_path;
2019-01-29 08:02:47 +00:00
var file_name = config.file_name;
var fn_y = file_name + ".y";
var fn_js = file_name + ".js";
var fn_out = file_name + ".out";
var temp_fn_y = "temp_" + "file_name" + ".y";
var temp_fn_js = "temp_" + "file_name" + ".js";
var temp_fn_out = "temp_" + "file_name" + ".out";
2019-01-28 12:08:59 +00:00
var update_parser_y = function () {
2019-01-30 11:50:02 +00:00
var source_parser_y = fs.readFileSync(input_path + fn_y, "utf8");
2019-01-28 12:08:59 +00:00
2019-01-29 09:09:17 +00:00
source_parser_y = source_parser_y.replace(/&&.*?REPLACER\{(.*?)\}&&/gm, function (match, file_path, offset, string) {
2019-01-30 11:50:02 +00:00
return fs.readFileSync("./" + input_path + file_path, "utf8");
2019-01-29 09:09:17 +00:00
});
2019-01-30 11:50:02 +00:00
fs.writeFileSync(input_path + temp_fn_y, source_parser_y);
2019-01-28 12:08:59 +00:00
};
var post_process_parser = function () {
2019-01-30 11:50:02 +00:00
var out_js = fs.readFileSync(input_path + temp_fn_js, "utf8");
2019-01-29 08:02:47 +00:00
2019-01-29 11:25:40 +00:00
if (args["t"] !== undefined) {
2019-01-29 08:02:47 +00:00
switch (args["t"]) {
case "web":
2019-01-29 11:25:40 +00:00
var header = fs.readFileSync(config.require_templates + "header.tmpl", "utf8");
var footer = fs.readFileSync(config.require_templates + "footer.tmpl", "utf8");
out_js = header + "\n" + out_js + "\n" + footer;
2019-01-29 08:02:47 +00:00
break;
case "node":
out_js += "\n\n module.exports = LemonJS;";
break;
}
}
2019-01-30 15:03:10 +00:00
out_js = js_beautify(out_js, {
indent_size: 4,
indent_char: ' ',
preserve_newlines: true,
space_after_anon_function: true,
keep_array_indentation: false,
braces_on_own_line: false
});
2019-01-28 12:08:59 +00:00
2019-01-30 11:50:02 +00:00
if(!fs.existsSync(output_path)){
fs.mkdirSync(output_path);
}
fs.writeFileSync(output_path + fn_js, out_js);
2019-01-29 08:02:47 +00:00
2019-01-30 11:50:02 +00:00
var temp_parser_out = fs.readFileSync(input_path + temp_fn_out, "utf8");
fs.writeFileSync(output_path + fn_out, temp_parser_out);
2019-01-28 12:08:59 +00:00
};
var start = function () {
update_parser_y();
2019-01-30 11:50:02 +00:00
exec(program_path + " " + input_path + temp_fn_y + " " + lemon_flags, function(err, stdout, stderr) {
2019-01-28 12:08:59 +00:00
err && console.log("ERROR: ", err);
err && process.exit(1);
post_process_parser();
2019-01-30 11:50:02 +00:00
fs.unlinkSync(input_path + temp_fn_y);
fs.unlinkSync(input_path + temp_fn_js);
fs.unlinkSync(input_path + temp_fn_out);
2019-01-28 12:08:59 +00:00
});
};
start();