@alu0100953275/constant-folding
Version:
Constant Folding javascript code
55 lines (50 loc) • 1.81 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")
.option("-p, --pattern <pattern>", "other parameters")
.action((filename, options) => {
if (options.pattern === undefined) {
transpile(filename, options.output);
} else {
transpile(filename, options.output, options.pattern);
}
});
program.parse(process.argv);
/**
* A function that takes two file names ames, read de input file(first name) and
* take those strings of code and do a constant folding whit it and then,
* write the output in the outputfile (second name)
*
* @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) {
fs.readFile(input_file, 'utf-8', (err, input) => {
if (err) {
console.log("Couldn't read the file: " + err);
return;
}
console.log("File read succesfully");
let output = constantFolding(input); // Constant folding
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);
});
});
}