UNPKG

adcutil

Version:

Utilities tools for Askia Design Control

310 lines (264 loc) 10.6 kB
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>The source code</title> <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" /> <script type="text/javascript" src="../resources/prettify/prettify.js"></script> <style type="text/css"> .highlight { display: block; background-color: #ddd; } </style> <script type="text/javascript"> function highlight() { document.getElementById(location.hash.replace(/#/, "")).className = "highlight"; } </script> </head> <body onload="prettyPrint(); highlight();"> <pre class="prettyprint lang-js">var fs = require(&#39;fs&#39;); var pathHelper = require(&#39;path&#39;); var common = require(&#39;../common/common.js&#39;); var errMsg = common.messages.error; var successMsg = common.messages.success; var Validator = require(&#39;../validator/ADCValidator.js&#39;).Validator; <span id='ADC-Builder'>/** </span> * Validate and compress the ADC directory to an `.adc` file * * @class ADC.Builder * @private */ function Builder(adcDirPath) { <span id='ADC-Builder-property-rootdir'> /** </span> * Root dir of the current ADCUtil */ this.rootdir = pathHelper.resolve(__dirname, &quot;../../&quot;); <span id='ADC-Builder-property-adcName'> /** </span> * Name of the ADC * @type {string} */ this.adcName = &#39;&#39;; <span id='ADC-Builder-property-adcDirectoryPath'> /** </span> * Path to the ADC directory * @type {string} */ this.adcDirectoryPath = adcDirPath ? pathHelper.normalize(adcDirPath) : process.cwd(); <span id='ADC-Builder-property-binPath'> /** </span> * Bin path of the ADC * @type {string} */ this.binPath = &#39;&#39;; <span id='ADC-Builder-property-outputPath'> /** </span> * Path of the output file * @type {string} */ this.outputPath = &#39;&#39;; <span id='ADC-Builder-property-sequence'> /** </span> * Sequence of calls * @type {*|Sequence} */ this.sequence = new common.Sequence([ this.createBinDir, this.compressADC ], this.done, this); <span id='ADC-Builder-property-validationReport'> /** </span> * Report of the validation * * @type {{startTime: null, endTime: null, runs: number, total: number, success: number, warnings: number, errors: number}} */ this.validationReport = null; <span id='ADC-Builder-property-logger'> /** </span> * Logger to override with an object * @type {{writeMessage : function, writeSuccess : function, writeWarning: function, writeError : function}} */ this.logger = null; } <span id='ADC-Builder-method-constructor'>/** </span> * Create a new instance of ADC Builder * * @constructor * @param {String} adcDirPath Path of the ADC directory */ Builder.prototype.constructor = Builder; <span id='ADC-Builder-method-build'>/** </span> * Build the ADC * * @param {Object} [options] Options of validation * @param {Boolean} [options.test=true] Run unit tests * @param {Boolean} [options.autoTest=true] Run auto unit tests * @param {Boolean} [options.xml=true] Validate the config.xml file * @param {InteractiveADXShell} [options.adxShell] Interactive ADXShell process * @param {Object} [options.logger] Logger * @param {Function} [options.writeMessage] Function where regular messages will be print * @param {Function} [options.writeSuccess] Function where success messages will be print * @param {Function} [options.writeWarning] Function where warning messages will be print * @param {Function} [options.writeError] Function where error messages will be print * @param {Function} [callback] Callback function * @param {Error} [callback.err] Error * @param {String} [callback.outputPath] Path of the output * @param {Object} [callback.report] Validation report */ Builder.prototype.build = function build(options, callback) { // Swap the options if (typeof options === &#39;function&#39;) { callback = options; options = null; } this.buildCallback = callback; this.validator = new Validator(this.adcDirectoryPath); var self = this; options = options || {}; options.xml = true; options.autoTest = true; if (options.logger) { this.logger = options.logger; } this.validator.validate(options, function validateCallback(err, report) { if (err) { return self.sequence.resume(new Error(errMsg.validationFailed)); } self.adcName = self.validator.adcName; self.binPath = pathHelper.join(self.adcDirectoryPath, common.ADC_BIN_PATH); self.validationReport = report; return self.sequence.resume(); }); }; <span id='ADC-Builder-method-writeError'>/** </span> * Write an error output in the console or in the logger * @param {String} text Text to write in the console */ Builder.prototype.writeError = function writeError(text) { if (this.logger &amp;&amp; typeof this.logger.writeError === &#39;function&#39;) { this.logger.writeError.apply(this.logger, arguments); } else { common.writeError.apply(common, arguments); } }; <span id='ADC-Builder-method-writeWarning'>/** </span> * Write a warning output in the console or in the logger * @param {String} text Text to write in the console */ Builder.prototype.writeWarning = function writeWarning(text) { if (this.logger &amp;&amp; typeof this.logger.writeWarning === &#39;function&#39;) { this.logger.writeWarning.apply(this.logger, arguments); } else { common.writeWarning.apply(common, arguments); } }; <span id='ADC-Builder-method-writeSuccess'>/** </span> * Write a success output in the console or in the logger * @param {String} text Text to write in the console */ Builder.prototype.writeSuccess = function writeSuccess(text) { if (this.logger &amp;&amp; typeof this.logger.writeSuccess === &#39;function&#39;) { this.logger.writeSuccess.apply(this.logger, arguments); } else { common.writeSuccess.apply(common, arguments); } }; <span id='ADC-Builder-method-writeMessage'>/** </span> * Write an arbitrary message in the console or in the logger without specific prefix or in the logger * @param {String} text Text to write in the console */ Builder.prototype.writeMessage = function writeMessage(text) { if (this.logger &amp;&amp; typeof this.logger.writeMessage === &#39;function&#39;) { this.logger.writeMessage.apply(this.logger, arguments); } else { common.writeMessage.apply(common, arguments); } }; <span id='ADC-Builder-method-done'>/** </span> * End of the sequence chain * @param {Error} err Error */ Builder.prototype.done = function done(err) { if (err) { this.writeError(err.message); if (typeof this.buildCallback === &#39;function&#39;) { this.buildCallback(err, this.outputPath, this.validationReport); } return; } var output = pathHelper.join(this.binPath, this.adcName + &#39;.adc&#39;); if (!this.validationReport.warnings) { this.writeSuccess(successMsg.buildSucceed, output); } else { this.writeSuccess(successMsg.buildSucceedWithWarning, this.validationReport.warnings, output); } if (typeof this.buildCallback === &#39;function&#39;) { this.buildCallback(err, this.outputPath, this.validationReport); } }; <span id='ADC-Builder-method-createBinDir'>/** </span> * Create a bin directory */ Builder.prototype.createBinDir = function createBinDir() { var self = this; common.dirExists(this.binPath, function binPathExist(err, exist) { if (!exist || err) { var er = fs.mkdirSync(self.binPath); if (er) { return self.sequence.resume(er); } } return self.sequence.resume(); }); }; <span id='ADC-Builder-method-compressADC'>/** </span> * Compress the ADC directory */ Builder.prototype.compressADC = function compressADC() { var self = this; common.getDirStructure(self.adcDirectoryPath, function callbackGetStructure(err, structure) { if (err) { return self.sequence.resume(err); } var zip = common.getNewZip(), zipDir = &#39;&#39;; structure.forEach(function appendInZip(file) { var prevDir, folderLower, zipDirLower = zipDir.toLowerCase(); if (typeof file === &#39;string&#39;) { // File if (zipDirLower === &#39;resources/&#39;) return; // Exclude extra files if (zipDirLower === &#39;&#39; &amp;&amp; !/^(config\.xml|readme|changelog)/i.test(file)) return; // Exclude extra files if (common.isIgnoreFile(file)) return; // Ignore files zip.file(pathHelper.join(zipDir, file), fs.readFileSync(pathHelper.join(self.adcDirectoryPath, zipDir, file))); } else { // Directory if (!file.sub || !file.sub.length) return; // Exclude empty folder folderLower = file.name.toLowerCase(); if (folderLower === &#39;bin&#39;) return; // Exclude the bin folder if (folderLower === &#39;tests&#39;) return; // Exclude tests folder if (zipDirLower === &#39;resources/&#39; &amp;&amp; !/^(dynamic|static|share)$/i.test(folderLower)) return; // Exclude extra directories if (zipDirLower === &#39;&#39; &amp;&amp; !/^(resources)$/.test(folderLower)) return; // Exclude extra directories prevDir = zipDir; zipDir += file.name + &#39;/&#39;; zip.folder(zipDir); file.sub.forEach(appendInZip); zipDir = prevDir; } }); var buffer = zip.generate({type:&quot;nodebuffer&quot;}); self.outputPath = pathHelper.join(self.binPath, self.adcName + &#39;.adc&#39;); fs.writeFile(self.outputPath, buffer, function writeZipFile(err) { if (err) { throw err; } }); self.sequence.resume(); }); }; // Export the Builder object exports.Builder = Builder; /* * Build the ADC file * * @param {Command} program Commander object which hold the arguments pass to the program * @param {String} path Path of the ADC to directory */ exports.build = function build(program, path) { var builder = new Builder(path); builder.build(program); }; </pre> </body> </html>