UNPKG

adcutil

Version:

Utilities tools for Askia Design Control

368 lines (346 loc) 13 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 path = require(&#39;path&#39;); var wrench = require(&#39;wrench&#39;); var common = require(&#39;./common/common.js&#39;); var errMsg = common.messages.error; var Validator = require(&#39;./validator/ADCValidator.js&#39;).Validator; var Builder = require(&#39;./builder/ADCBuilder.js&#39;).Builder; var Show = require(&#39;./show/ADCShow.js&#39;).Show; var Generator = require(&#39;./generator/ADCGenerator.js&#39;).Generator; var Configurator = require(&#39;./configurator/ADCConfigurator.js&#39;).Configurator; var InteractiveADXShell = require(&#39;./common/InteractiveADXShell.js&#39;).InteractiveADXShell; var preferences = require(&#39;./preferences/ADCPreferences.js&#39;).preferences; <span id='ADC'>/** </span> * Object used to generate, validate, show and build an ADC * * * Example of usage of existing ADC * * var ADC = require(&#39;adcutil&#39;).ADC; * var myAdc = new ADC(&#39;path/to/adc/dir&#39;); * * // Validate an ADC * myAdc.validate({test : false, autoTest : false}, function (err, report) { * // Callback when the ADC structure has been validated * }); * * // Show the output of an ADC * myAdc.show({ output : &#39;fallback&#39;, fixture : &#39;single.xml&#39; }, function (err, output) { * // Callback with the output of the ADC * }); * * // Build the ADC (package it) * myAdc.build({test : false, autoTest : false}, function (err, path, report) { * // Callback when the ADC has been built * }); * * Generate and use the new ADC instance * * ADC.generate(&#39;myNewADC&#39;, {output : &#39;/path/of/parent/dir&#39;, template : &#39;blank&#39;}, function (err, adc) { * console.log(adc.path); * adc.load(function (err) { * if (err) { * console.log(err); * return; * } * console.log(adc.configurator.info.get()); * }); * }); * * * @class ADC */ function ADC(adcDirPath) { if (!adcDirPath) { throw new Error(errMsg.invalidPathArg); } // Let it throw an exception fs.statSync(adcDirPath); <span id='ADC-property-path'> /** </span> * Path to the ADC directory * @type {string} */ this.path = path.normalize(adcDirPath); <span id='ADC-property-configurator'> /** </span> * Configurator of the ADC * Expose the object to manipulate the config.xml * * @type {ADC.Configurator} */ this.configurator = null; <span id='ADC-property-_adxShell'> /** </span> * Interactive ADX Shell * * @type {*|InteractiveADXShell} * @private */ this._adxShell = new InteractiveADXShell(this.path); } <span id='ADC-method-constructor'>/** </span> * Create a new instance of ADC object * * * var ADC = require(&#39;adcutil&#39;).ADC; * var myAdc = new ADC(&#39;path/to/adc/dir&#39;); * * @constructor * @param {String} adcDirPath Path of the ADC directory */ ADC.prototype.constructor = ADC; <span id='ADC-method-load'>/** </span> * Load the config of the current ADC instance * * * var ADC = require(&#39;adcutil&#39;).ADC; * var myAdc = new ADC(&#39;path/to/adc/dir&#39;); * * // Load an ADC * myAdc.load(function (err) { * // Callback when the ADC has been loaded * }); * * @param {Function} [callback] Callback function * @param {Error} [callback.err] Error */ ADC.prototype.load = function load(callback) { var configurator = new Configurator(this.path), self = this; callback = callback || function (){}; configurator.load(function (err) { if (err) { callback(err); return; } self.configurator = configurator; callback(null); }); }; <span id='ADC-method-validate'>/** </span> * Validate the current ADC instance * * * var ADC = require(&#39;adcutil&#39;).ADC; * var myAdc = new ADC(&#39;path/to/adc/dir&#39;); * * // Validate an ADC * myAdc.validate({test : false, autoTest : false}, function (err, report) { * // Callback when the ADC structure has been validated * }); * * @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 {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 {Object} [callback.report] Validation report */ ADC.prototype.validate = function validate(options, callback) { var validator = new Validator(this.path); options = options || {}; options.adxShell = this._adxShell; validator.validate(options, callback); }; <span id='ADC-method-build'>/** </span> * Build the ADC * * var ADC = require(&#39;adcutil&#39;).ADC; * var myAdc = new ADC(&#39;path/to/adc/dir&#39;); * * // Build the ADC (package it) * myAdc.build({test : false, autoTest : false}, function (err, path, report) { * // Callback when the ADC has been built * }); * * @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 {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 */ ADC.prototype.build = function build(options, callback){ var builder = new Builder(this.path); options = options || {}; options.adxShell = this._adxShell; builder.build(options, callback); }; <span id='ADC-method-show'>/** </span> * Show the ADC output * * var ADC = require(&#39;adcutil&#39;).ADC; * var myAdc = new ADC(&#39;path/to/adc/dir&#39;); * * // Show the output of an ADC * myAdc.show({ output : &#39;fallback&#39;, fixture : &#39;single.xml&#39; }, function (err, output) { * // Callback with the output of the ADC * }); * * @param {Object} options Options * @param {String} options.output Name of the ADC Output to use * @param {String} options.fixture FileName of the ADC fixture to use * @param {String} [options.masterPage] Path of the master page to use * @param {Boolean} [options.silent=false] Silent mode: Don&#39;t message in the console but only through the callback * @param {Function} callback Callback function * @param {Error} callback.err Error * @param {String} callback.output Output string */ ADC.prototype.show = function show(options, callback) { var show = new Show(this.path); options = options || {}; options.adxShell = this._adxShell; show.show(options, callback); }; <span id='ADC-method-getFixtureList'>/** </span> * Returns the list of fixtures * * var ADC = require(&#39;adcutil&#39;).ADC; * var myAdc = new ADC(&#39;path/to/adc/dir&#39;); * * // List all fixtures on the ADC * myAdc.getFixtureList(function (err, list) { * console.log(list[0]); // -&gt; &quot;Single.xml&quot; * }); * * @param {Function} callback Callback * @param {Error} callback.err Error * @param {String[]} callback.list List of fixtures */ ADC.prototype.getFixtureList = function getFixtureList(callback) { var fixturePath = path.join(this.path, common.FIXTIRES_DIR_PATH); fs.readdir(fixturePath, function (err, files) { if (err) { callback(err, null); return; } var fixtures = [], i, l; for (i = 0, l = files.length; i &lt; l; i += 1) { if (/\.xml$/.test(files[i])) { fixtures.push(files[i]); } } callback(null, fixtures); }); }; <span id='ADC-method-checkFixtures'>/** </span> * Verify if the fixture exist and create it if it doesn&#39;t * @param {Function} callback Callback when the operation is complete */ ADC.prototype.checkFixtures = function checkFixtures(callback) { var fixturePath = path.join(this.path, common.FIXTIRES_DIR_PATH); var self = this; common.dirExists(fixturePath, function (err, isExist) { if (isExist) { if (typeof callback === &#39;function&#39;) { callback(); } return; } var testPath = path.join(fixturePath, &#39;../&#39;); var sourcePath = path.join(path.resolve(__dirname, &quot;../&quot;), common.TEMPLATES_PATH, common.DEFAULT_TEMPLATE_NAME, common.FIXTIRES_DIR_PATH); fs.mkdir(testPath, function () { wrench.copyDirRecursive(sourcePath, fixturePath, { forceDelete : false, excludeHiddenUnix : true, preserveFiles : true }, function () { if (typeof callback === &#39;function&#39;) { callback(); } }); }); }); }; <span id='ADC-static-method-generate'>/** </span> * Generate a new ADC structure * * // Generate the ADC structure in &#39;/path/of/parent/dir/myNewADC&#39; * ADC.generate(&#39;myNewADC&#39;, {output : &#39;/path/of/parent/dir&#39;, template : &#39;blank&#39;}, function (err, adc) { * console.log(adc.path); * }); * * @param {String} name Name of the ADC to generate * @param {Object} [options] Options * @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.output=process.cwd()] Path of the output director * @param {String} [options.template=&quot;blank&quot;] Name of the template to use * @param {Function} [callback] * @param {Error} [callback.err] Error * @param {ADC} [callback.adc] Instance of the new generated ADC * @static */ ADC.generate = function generate(name, options, callback) { var generator = new Generator(); // Swap the options if (typeof options === &#39;function&#39;) { callback = options; options = null; } callback = callback || function () {}; generator.generate(name, options, function (err, outputPath) { if (err) { callback(err, null); return; } callback(null, new ADC(outputPath)); }); }; <span id='ADC-static-method-getTemplateList'>/** </span> * Returns the list of templates directory * * @param {Function} callback Callback * @param {Error} callback.err Error * @param {Object[]} callback.dirs List of template * @param {String} callback.dirs[].name Name of the template * @param {String} callback.dirs[].path Path of the template directory * @static */ ADC.getTemplateList = function getTemplateList(callback) { common.getTemplateList(callback); }; <span id='ADC-property-preferences'>/** </span> * Instance of the object to manage the preferences * * @type {ADC.Preferences} */ ADC.preferences = preferences; // Make it public exports.ADC = ADC; exports.Configurator = Configurator; </pre> </body> </html>