@alu0101350158/constant-folding
Version:
Constant Folding javascript code
44 lines (40 loc) • 1.31 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>", "description 1", "output.js")
.action((filename, options) => {
if (options.output === undefined) {
transpile(filename);
} else {
transpile(filename, options.output);
}
});
program.parse(process.argv);
/**
* A function that takes two file names and applies
* constant folding to the js code present in the first
* file and outputs the result in the second one.
* @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
*
* @private
*/
function transpile(input_file, output_file = null) {
let code = fs.readFileSync(input_file, 'utf8');
let result = constantFolding(code);
if (output_file !== null) {
fs.writeFileSync(output_file, result);
} else {
console.log(result);
}
}