UNPKG

adcutil

Version:

Utilities tools for Askia Design Control

360 lines (318 loc) 12.5 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 format = require(&#39;util&#39;).format; var pathHelper = require(&#39;path&#39;); var common = require(&#39;../common/common.js&#39;); var preferences = require(&#39;../preferences/ADCPreferences.js&#39;); var wrench = require(&#39;wrench&#39;); var uuid = require(&#39;node-uuid&#39;); var errMsg = common.messages.error; var successMsg = common.messages.success; <span id='ADC-Generator'>/** </span> * Generate the file structure of an ADC using a template * * @class ADC.Generator * @private */ function Generator() { <span id='ADC-Generator-property-rootdir'> /** </span> * Root dir of the current ADCUtil */ this.rootdir = pathHelper.resolve(__dirname, &quot;../../&quot;); <span id='ADC-Generator-property-adcName'> /** </span> * Name of the ADC * @type {string} */ this.adcName = &#39;&#39;; <span id='ADC-Generator-property-adcDescription'> /** </span> * Description of the ADC * @type {string} */ this.adcDescription = &#39;&#39;; <span id='ADC-Generator-property-adcAuthor'> /** </span> * Author * @type {Object} */ this.adcAuthor = { name : &#39;&#39;, email : &#39;&#39;, company : &#39;&#39;, webSite : &#39;&#39; }; <span id='ADC-Generator-property-templateSrc'> /** </span> * Path of the template directory * @type {string} */ this.templateSrc = &#39;&#39;; <span id='ADC-Generator-property-outputDirectory'> /** </span> * Output directory * @type {string} */ this.outputDirectory = &#39;&#39;; <span id='ADC-Generator-property-template'> /** </span> * Name of the template to use * @type {string} */ this.template = common.DEFAULT_TEMPLATE_NAME; <span id='ADC-Generator-property-sequence'> /** </span> * Sequence of calls * @type {*|Sequence} */ this.sequence = new common.Sequence([ this.verifyOutputDirExist, this.verifyADCDirNotAlreadyExist, this.copyFromTemplate, this.updateFiles ], this.done, this); } <span id='ADC-Generator-method-constructor'>/** </span> * Create a new instance of ADC Generator * * @constructor */ Generator.prototype.constructor = Generator; <span id='ADC-Generator-method-writeError'>/** </span> * Write an error output in the console * @param {String} text Text to write in the console */ Generator.prototype.writeError = function writeError(text) { common.writeError.apply(common, arguments); }; <span id='ADC-Generator-method-writeWarning'>/** </span> * Write a warning output in the console * @param {String} text Text to write in the console */ Generator.prototype.writeWarning = function writeWarning(text) { common.writeWarning.apply(common, arguments); }; <span id='ADC-Generator-method-writeSuccess'>/** </span> * Write a success output in the console * @param {String} text Text to write in the console */ Generator.prototype.writeSuccess = function writeSuccess(text) { common.writeSuccess.apply(common, arguments); }; <span id='ADC-Generator-method-writeMessage'>/** </span> * Write an arbitrary message in the console without specific prefix * @param {String} text Text to write in the console */ Generator.prototype.writeMessage = function writeMessage(text) { common.writeMessage.apply(common, arguments); }; <span id='ADC-Generator-method-generate'>/** </span> * Generate a new ADC structure * * @param {String} name Name of the ADC to generate * @param {Object} [options] Options * @param {String} [options.output=process.cwd()] Path of the output director * @param {String} [options.description=&#39;&#39;] Description of the ADC * @param {Object} [options.author] Author of the ADC * @param {String} [options.author.name=&#39;&#39;] Author name * @param {String} [options.author.email=&#39;&#39;] Author email * @param {String} [options.author.company=&#39;&#39;] Author Company * @param {String} [options.author.website=&#39;&#39;] Author web site * @param {String} [options.template=&quot;blank&quot;] Name of the template to use * @param {Function} [callback] * @param {Error} [callback.err] Error * @param {String} [callback.outputDirectory] Path of the output directory */ Generator.prototype.generate = function generate(name, options, callback) { // Swap the options &amp; callback if (typeof options === &#39;function&#39;) { callback = options; options = null; } this.generateCallback = callback; if (!name) { this.done(new Error(errMsg.missingNameArgument)); return; } if (!/^([a-z0-9_ .-]+)$/gi.test(name)) { this.done(new Error(errMsg.incorrectADCName)); return; } this.outputDirectory = (options &amp;&amp; options.output) || process.cwd(); if (!this.outputDirectory) { this.done(new Error(errMsg.missingOutputArgument)); return; } var self = this; preferences.read({silent : true}, function (prefs) { var prefAuthor = (prefs &amp;&amp; prefs.author) || {}; self.adcName = name; self.adcDescription = (options &amp;&amp; options.description) || &#39;&#39;; self.adcAuthor = (options &amp;&amp; options.author) || {}; self.adcAuthor.name = self.adcAuthor.name || prefAuthor.name || &#39;&#39;; self.adcAuthor.email = self.adcAuthor.email || prefAuthor.email || &#39;&#39;; self.adcAuthor.company = self.adcAuthor.company || prefAuthor.company || &#39;&#39;; self.adcAuthor.website = self.adcAuthor.website || prefAuthor.website || &#39;&#39;; self.template = (options &amp;&amp; options.template) || common.DEFAULT_TEMPLATE_NAME; self.templateSrc = pathHelper.join(self.rootdir, common.TEMPLATES_PATH, self.template); common.getTemplatePath(self.template, function (err, src) { if (err) { return self.done(err); } self.templateSrc = src; return self.sequence.resume(); }); }); }; <span id='ADC-Generator-method-done'>/** </span> * End of the sequence chain * @param {Error} err Error */ Generator.prototype.done = function done(err) { if (err) { this.writeError(err.message); if (typeof this.generateCallback === &#39;function&#39;) { this.generateCallback(err, this.outputDirectory); } return; } var self = this; common.getDirStructure(self.outputDirectory, function getDirStructure(err, structure) { if (err) { self.writeError(err.message); if (typeof self.generateCallback === &#39;function&#39;) { self.generateCallback(err, self.outputDirectory); } return; } var level = 0, s = []; function indent(text) { var str = &#39;|--&#39;; for (var i = 0; i &lt; level; i++) { str += &#39;|--&#39;; } str += &#39; &#39; + text; return str; } structure.forEach(function write(o) { if (typeof o === &#39;string&#39;) { s.push(indent(o)); } else { s.push(indent(o.name) + &#39;\\&#39;); level++; if (o.sub) { o.sub.forEach(write); } level--; } }); s = s.join(&#39;\r\n&#39;); self.writeSuccess(successMsg.adcStructureGenerated, s, self.adcName, self.outputDirectory); if (typeof self.generateCallback === &#39;function&#39;) { self.generateCallback(err, self.outputDirectory); } }); }; <span id='ADC-Generator-method-verifyOutputDirExist'>/** </span> * Verify that the output directory */ Generator.prototype.verifyOutputDirExist = function verifyOutputDirExist() { // Validate the existence of the specify the output directory var self = this; common.dirExists(self.outputDirectory, function outputDirExist(err, exists) { if (!exists || err) { return self.sequence.resume(new Error(format(errMsg.noSuchFileOrDirectory, self.outputDirectory))); } self.outputDirectory = pathHelper.join(self.outputDirectory, self.adcName); return self.sequence.resume(); }); }; <span id='ADC-Generator-method-verifyADCDirNotAlreadyExist'>/** </span> * Verify that the ADC directory doesn&#39;t exist */ Generator.prototype.verifyADCDirNotAlreadyExist = function verifyADCDirNotAlreadyExist() { var self = this; common.dirExists(self.outputDirectory, function adcDirExist(err, exists) { if (exists &amp;&amp; !err) { return self.sequence.resume(new Error(format(errMsg.directoryAlreadyExist, self.outputDirectory))); } return self.sequence.resume(); }); }; <span id='ADC-Generator-method-copyFromTemplate'>/** </span> * Copy an ADC structure from the template */ Generator.prototype.copyFromTemplate = function copyFromTemplate() { var self = this; wrench.copyDirRecursive(self.templateSrc, self.outputDirectory, { forceDelete : false, excludeHiddenUnix : true, preserveFiles : true }, function copyDirRecursive(err) { self.sequence.resume(err); }); }; <span id='ADC-Generator-method-updateFiles'>/** </span> * Update the config.xml and the readme files with the name of the ADC, the GUID and the creation date */ Generator.prototype.updateFiles = function updateFiles() { var self = this, files = [ pathHelper.join(self.outputDirectory, common.CONFIG_FILE_NAME), pathHelper.join(self.outputDirectory, common.README_FILE_NAME) ], treat = 0; files.forEach(function (file) { fs.readFile(file, &#39;utf8&#39;, function readFile(err, data) { if (err) { treat++; self.sequence.resume(err); return; } var result = data, authorFullName = &#39;&#39;; result = result.replace(/\{\{ADCName\}\}/gi, self.adcName); result = result.replace(/\{\{ADCGuid\}\}/gi, uuid.v4()); result = result.replace(/\{\{ADCDescription\}\}/gi, self.adcDescription); result = result.replace(/\{\{ADCAuthor.Name\}\}/gi, self.adcAuthor.name); result = result.replace(/\{\{ADCAuthor.Email\}\}/gi, self.adcAuthor.email); result = result.replace(/\{\{ADCAuthor.Company\}\}/gi, self.adcAuthor.company); result = result.replace(/\{\{ADCAuthor.website\}\}/gi, self.adcAuthor.website); authorFullName = self.adcAuthor.name || &#39;&#39;; if (self.adcAuthor.email) { authorFullName += &#39; &lt;&#39; + self.adcAuthor.email + &#39;&gt;&#39;; } result = result.replace(/\{\{ADCAuthor\}\}/gi, authorFullName); result = result.replace(/2000-01-01/, common.formatXmlDate()); result = result.replace(&#39;\ufeff&#39;, &#39;&#39;); // Remove the BOM characters (Marker of the UTF-8 in the string) fs.writeFile(file, result, function writeFileCallback(err) { treat++; if (treat === files.length) { return self.sequence.resume(err); } }); }); }); }; // Make it public exports.Generator = Generator; /* * Generate a new ADC structure * * @param {Command} program Commander object which hold the arguments pass to the program * @param {String} name Name of the ADC to generate */ exports.generate = function generate(program, name) { var generator = new Generator(); generator.generate(name, program); };</pre> </body> </html>