index.js
1 /** 2 * Archiver Vending 3 * 4 * @ignore 5 * @license [MIT]{@link https://github.com/archiverjs/node-archiver/blob/master/LICENSE} 6 * @copyright (c) 2012-2014 Chris Talkington, contributors. 7 */ 8 var Archiver = require('./lib/core'); 9 10 var formats = {}; 11 12 /** 13 * Dispenses a new Archiver instance. 14 * 15 * @constructor 16 * @param {String} format The archive format to use. 17 * @param {Object} options See [Archiver]{@link Archiver} 18 * @return {Archiver} 19 */ 20 var vending = function(format, options) { 21 return vending.create(format, options); 22 }; 23 24 /** 25 * Creates a new Archiver instance. 26 * 27 * @param {String} format The archive format to use. 28 * @param {Object} options See [Archiver]{@link Archiver} 29 * @return {Archiver} 30 */ 31 vending.create = function(format, options) { 32 if (formats[format]) { 33 var instance = new Archiver(format, options); 34 instance.setFormat(format); 35 instance.setModule(new formats[format](options)); 36 37 return instance; 38 } else { 39 throw new Error('create(' + format + '): format not registered'); 40 } 41 }; 42 43 /** 44 * Registers a format for use with archiver. 45 * 46 * @param {String} format The name of the format. 47 * @param {Function} module The function for archiver to interact with. 48 * @return void 49 */ 50 vending.registerFormat = function(format, module) { 51 if (formats[format]) { 52 throw new Error('register(' + format + '): format already registered'); 53 } 54 55 if (typeof module !== 'function') { 56 throw new Error('register(' + format + '): format module invalid'); 57 } 58 59 if (typeof module.prototype.append !== 'function' || typeof module.prototype.finalize !== 'function') { 60 throw new Error('register(' + format + '): format module missing methods'); 61 } 62 63 formats[format] = module; 64 }; 65 66 /** 67 * Check if the format is already registered. 68 * 69 * @param {String} format the name of the format. 70 * @return boolean 71 */ 72 vending.isRegisteredFormat = function (format) { 73 if (formats[format]) { 74 return true; 75 } 76 77 return false; 78 }; 79 80 vending.registerFormat('zip', require('./lib/plugins/zip')); 81 vending.registerFormat('tar', require('./lib/plugins/tar')); 82 vending.registerFormat('json', require('./lib/plugins/json')); 83 84 module.exports = vending;