From a9a1ac03f22cbdd58fd86b87126e297c406020b6 Mon Sep 17 00:00:00 2001 From: Greg Swindle Date: Sat, 1 Apr 2017 19:06:09 -0500 Subject: [PATCH] chore(cli): uncomment cli --- lib/crc-model-cli.js | 218 +++++++++++++++++++++---------------------- 1 file changed, 109 insertions(+), 109 deletions(-) diff --git a/lib/crc-model-cli.js b/lib/crc-model-cli.js index e0592c52..7b4955f7 100644 --- a/lib/crc-model-cli.js +++ b/lib/crc-model-cli.js @@ -1,109 +1,109 @@ -// #!/usr/bin/env node --harmony -// Const CrcModelFormatter = require('./crc-model-formatter'); -// Const CrcModelList = require('./crc-model-list'); -// Const concat = require('concat'); -// Const fs = require('fs'); -// Const glob = require('glob'); -// Const mkdirp = require('mkdirp'); -// Const path = require('path'); -// Const program = require('commander'); -// Const {version} = require('../package'); -// -// /** -// * Prepend JavaScript comments to shebangs (!#) to prevent breaking the AST parser. -// * -// * @private -// * @param {string} code The source code to be parsed. -// * -// * @returns {string} The source code with shebangs commented (if any). -// */ -// Const preprocessShebangs = (code) => { -// Return code.replace('#!', '// #!'); -// }; -// -// /** -// * Saves `CrcModels` to file. -// * -// * @private -// * @param {array} files A list of files to be parsed. -// * @param {string} reportFilePath The filesystem location for the report. -// * -// * @returns {void} -// */ -// Const generateReport = (files, output) => { -// Concat(files).then((code) => { -// Const crcModelList = new CrcModelList(preprocessShebangs(code)); -// Const template = fs.readFileSync(path.resolve(__dirname, 'templates', 'crc-card.html')); -// Const formatter = new CrcModelFormatter(template); -// Const report = formatter.format(crcModelList); -// SaveToFile(report, output); -// }, (reason) => { -// Console.error(reason); -// }).catch((err) => { -// Console.warn(err); -// }); -// }; -// -// /** -// * Saves a Class-Responsibility-Collaboration report to the filesystem. -// * -// * @private -// * @param {string} report The CRC Model report. -// * @param {string} filepath The filesystem location for the report. -// * -// * @throws {Error} Throws an `Error` if the report cannot be saved. -// * -// * @returns {void} -// */ -// Const saveToFile = (report, filepath) => { -// Const buffer = new Buffer(report); -// Const msg = 'Class-Responsibility-Collaborators report'; -// Fs.open(filepath, 'w', function(err, fd) { -// If (err) { -// Throw `Error saving ${msg}: ${err}`; -// } -// -// Fs.write(fd, buffer, 0, buffer.length, null, function(err) { -// If (err) { -// Throw `Error saving ${msg}: ${err}`; -// } -// Fs.close(fd, function() { -// Console.log(`Saved ${msg} to ${filepath}.`); -// }); -// }); -// }); -// }; -// -// /** -// * A command-line interface that creates a Class-Responsibility-Collaborators report. -// */ -// Program -// .version(version) -// .description('Generates a Class-Responsibility-Collaboration report for analysis and refactoring.') -// .arguments('') -// .option('-o, --output [output]', 'The report destination. Defaults to "./crc-model-report."') -// .action((filePattern) => { -// Glob(filePattern, (err, files) => { -// If (err) { -// Console.error(err); -// } -// Const destFile = program.output || './reports/crc-model-report.md'; -// Const destPath = path.dirname(destFile); -// Mkdirp(destPath, function (err) { -// If (err) { -// Console.error(err); -// } -// Else { -// GenerateReport(files, destFile); -// } -// }); -// }); -// }); -// -// Const isCli = require.main === module; -// -// If (isCli) { -// Program.parse(process.argv); -// } -// -// Module.exports = program; +#!/usr/bin/env node --harmony +const CrcModelFormatter = require('./crc-model-formatter'); +const CrcModelList = require('./crc-model-list'); +const concat = require('concat'); +const fs = require('fs'); +const glob = require('glob'); +const mkdirp = require('mkdirp'); +const path = require('path'); +const program = require('commander'); +const {version} = require('../package'); + +/** + * Prepend JavaScript comments to shebangs (!#) to prevent breaking the AST parser. + * + * @private + * @param {string} code The source code to be parsed. + * + * @returns {string} The source code with shebangs commented (if any). + */ +const preprocessShebangs = (code) => { + return code.replace('#!', '// #!'); +}; + +/** + * Saves `CrcModels` to file. + * + * @private + * @param {array} files A list of files to be parsed. + * @param {string} reportFilePath The filesystem location for the report. + * + * @returns {void} + */ +const generateReport = (files, output) => { + concat(files).then((code) => { + const crcModelList = new CrcModelList(preprocessShebangs(code)); + const template = fs.readFileSync(path.resolve(__dirname, 'templates', 'crc-card.html')); + const formatter = new CrcModelFormatter(template); + const report = formatter.format(crcModelList); + saveToFile(report, output); + }, (reason) => { + console.error(reason); + }).catch((err) => { + console.warn(err); + }); +}; + +/** + * Saves a Class-Responsibility-Collaboration report to the filesystem. + * + * @private + * @param {string} report The CRC Model report. + * @param {string} filepath The filesystem location for the report. + * + * @throws {Error} throws an `Error` if the report cannot be saved. + * + * @returns {void} + */ +const saveToFile = (report, filepath) => { + const buffer = new Buffer(report); + const msg = 'Class-Responsibility-Collaborators report'; + fs.open(filepath, 'w', function(err, fd) { + if (err) { + throw `Error saving ${msg}: ${err}`; + } + + fs.write(fd, buffer, 0, buffer.length, null, function(err) { + if (err) { + throw `Error saving ${msg}: ${err}`; + } + fs.close(fd, function() { + console.log(`Saved ${msg} to ${filepath}.`); + }); + }); + }); +}; + +/** + * A command-line interface that creates a Class-Responsibility-Collaborators report. + */ +program + .version(version) + .description('Generates a Class-Responsibility-Collaboration report for analysis and refactoring.') + .arguments('') + .option('-o, --output [output]', 'The report destination. Defaults to "./crc-model-report."') + .action((filePattern) => { + glob(filePattern, (err, files) => { + if (err) { + console.error(err); + } + const destFile = program.output || './reports/crc-model-report.md'; + const destPath = path.dirname(destFile); + mkdirp(destPath, function (err) { + if (err) { + console.error(err); + } + else { + generateReport(files, destFile); + } + }); + }); + }); + +const isCli = require.main === module; + +if (isCli) { + program.parse(process.argv); +} + +module.exports = program;