UNPKG

3.08 kBJavaScriptView Raw
1'use strict';
2
3var archiver = require('archiver');
4var fs = require('fs');
5var path = require('path');
6var stream = require('stream');
7
8archiver.registerFormat('nupkg', require('./nupkg'));
9
10module.exports = function(type, options) {
11 options = options || {};
12 type = type || 'tar.gz';
13 if(type[0] === '.'){
14 type = type.substring(1);
15 }
16
17 var methods = {};
18 var details = getPackageDetails(options || {});
19 var archive = createArchiver(type, details);
20
21 archive.on('error', function (err) {
22 throw err;
23 });
24
25 methods.append = function append(name, file, options) {
26 if (Buffer.isBuffer(file) || isReadableStream(file)) {
27 name = name.replace(/\\/g, '/') + (!file ? '/' : '');
28 options = options || {};
29 archive.append(file, {name: name, date: options.date});
30 } else {
31 name = name.replace(/\\/g, '/');
32 var content = typeof file === 'string' ? file : name;
33 archive.file(content, {name: name.replace(/\\/g, '/')});
34 }
35
36 return methods;
37 };
38
39 methods.toFile = function toFile(dir, cb) {
40
41 if(typeof dir === 'function') {
42 cb = dir;
43 dir = __dirname;
44 }
45 var fileName = getFileName();
46
47 fs.mkdir(dir, function(err) {
48 if(err && err.code !== 'EEXIST') {
49 cb(err);
50 } else {
51 var filePath = path.join(dir, fileName);
52 var output = fs.createWriteStream(filePath);
53
54 archive.pipe(output);
55 archive.finalize();
56
57 output.on('close', function () {
58 cb(null, { size: archive.pointer(), name: fileName, path: filePath});
59 });
60 }
61 });
62 };
63
64 methods.toStream = function toStream(cb) {
65 archive.finalize();
66 cb(null, { stream: archive, name: getFileName()});
67 };
68
69 function getFileName() {
70 return details.id + '.' + details.version + getExtension(type);
71 }
72
73 return methods;
74};
75
76function isReadableStream(obj) {
77 return obj instanceof stream.Stream && typeof (obj._read) === 'function' && typeof (obj._readableState) === 'object';
78}
79
80
81function createArchiver(type, details) {
82 switch (type) {
83 case 'targz':
84 case 'tar.gz':
85 return archiver('tar', {gzip: true});
86 case 'tar':
87 return archiver('tar');
88 case 'zip':
89 return archiver('zip');
90 case 'nupkg':
91 case 'nuget':
92 return archiver('nupkg', {nupkgOptions: details});
93 }
94 throw 'Unknown archive type';
95}
96
97function getExtension(type){
98 switch (type) {
99 case 'targz':
100 case 'tar.gz':
101 return '.tar.gz';
102 case 'tar':
103 return '.tar';
104 case 'zip':
105 return '.zip';
106 case 'nupkg':
107 case 'nuget':
108 return '.nupkg';
109 }
110 throw 'Unknown archive type';
111}
112
113function getPackageDetails(options){
114 var packagejson = require(options.packagejson || path.join(process.cwd(), 'package.json'));
115
116 return {
117 id: options.id || packagejson.name.replace('/', '.').replace('@',''),
118 version: options.version || packagejson.version,
119 authors: options.authors || packagejson.author,
120 description: options.description ||packagejson.description,
121 title: options.name || packagejson.name
122 };
123}
\No newline at end of file