@adrianepi/constant-folding
Version:
50 lines (46 loc) • 1.8 kB
JavaScript
const {program} = require('commander');
const fs = require('fs');
const {version, description} = require('../package.json');
const constantFolding = require('../src/constant-folding'); // Your libs
;
program
.version(version)
.name('Constant-Folding')
.arguments("<filename>")
.description(description, {
filename: 'file with the original code'
})
.option("-o, --output <filename>", "file where the output is going to be stored", "output.js")
.option("-p, --pattern <pattern>", "other parameters (--js | -j) for only store the js output and (--ast | -a) for only storing the ast")
.action((filename, options) => {
transpile(filename, options.output, options.pattern);
});
program.parse(process.argv);
/**
* A function that takes two file names and a pattern and parses the js code
* into an AST or js code.
*
* @param {string} input_file The file from which to read the code
* @param {string} output_file The file to which to write the code with
* the logs etc.
* @param {string} [pattern=""] The pattern
* @private
*/
function transpile(input_file, output_file, pattern = "") {
let program = fs.readFileSync(input_file, 'utf8', (err, input) => {
if (err) {
console.log("Couldn't read the file: " + err);
return;
}
});
console.log("File read succesfully");
let output = constantFolding(program, pattern);
fs.writeFile(output_file, output, (err) => {
if (err) {
console.log("Couldn't write the output to " + output_file + ": " + err);
return;
}
console.log("Output written succesfully in " + output_file);
});
}