UNPKG

1.62 kBJavaScriptView Raw
1/**
2 * node-archiver
3 *
4 * Copyright (c) 2012-2014 Chris Talkington, contributors.
5 * Licensed under the MIT license.
6 * https://github.com/ctalkington/node-archiver/blob/master/LICENSE-MIT
7 */
8var ArchiverCore = require('./core');
9var formats = {};
10
11var archiver = module.exports = function(format, options) {
12 return archiver.create(format, options);
13};
14
15archiver.create = function(format, options) {
16 if (formats[format]) {
17 var instance = new ArchiverCore(options);
18 instance.setFormat(format);
19 instance.setModule(new formats[format](options));
20
21 return instance;
22 } else {
23 throw new Error('create(' + format + '): format not registered');
24 }
25};
26
27archiver.registerFormat = function(format, module) {
28 if (formats[format]) {
29 throw new Error('register(' + format + '): format already registered');
30 }
31
32 if (typeof module !== 'function') {
33 throw new Error('register(' + format + '): format module invalid');
34 }
35
36 if (typeof module.prototype.append !== 'function' || typeof module.prototype.finalize !== 'function') {
37 throw new Error('register(' + format + '): format module missing methods');
38 }
39
40 formats[format] = module;
41
42 // backwards compat - to be removed in 0.14
43 var compatName = 'create' + format.charAt(0).toUpperCase() + format.slice(1);
44 archiver[compatName] = function(options) {
45 return archiver.create(format, options);
46 };
47};
48
49archiver.registerFormat('zip', require('./plugins/zip'));
50archiver.registerFormat('tar', require('./plugins/tar'));
51archiver.registerFormat('json', require('./plugins/json'));
\No newline at end of file