1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var ArchiverCore = require('./core');
|
9 | var formats = {};
|
10 |
|
11 | var archiver = module.exports = function(format, options) {
|
12 | return archiver.create(format, options);
|
13 | };
|
14 |
|
15 | archiver.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 |
|
27 | archiver.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 |
|
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 |
|
49 | archiver.registerFormat('zip', require('./plugins/zip'));
|
50 | archiver.registerFormat('tar', require('./plugins/tar'));
|
51 | archiver.registerFormat('json', require('./plugins/json')); |
\ | No newline at end of file |