-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
197 lines (155 loc) · 4.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict';
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var assign = require('object-assign');
var tar = require('tar-stream');
var lodash = require('lodash');
var ar = require('./lib/ar');
var streamToBuffer = require('stream-to-buffer');
var zlib = require('zlib');
var fs = require('fs');
module.exports = function (filename, options) {
if (!filename) {
throw new gutil.PluginError('gulp-tar', '`filename` required');
}
var firstFile;
var archive = tar.pack();
return through.obj(function (file, enc, cb) {
if (file.relative === '') {
cb();
return;
}
if (firstFile === undefined) {
firstFile = file;
}
gutil.log('Packing:', file.relative);
if(file.stat.isSymbolicLink()) {
archive.entry({
name: file.relative.replace(/\\/g, '/') + (file.isNull() ? '/' : ''),
mode: file.stat.mode,
type: 'symlink',
linkname: fs.readlinkSync(file.path)
});
return cb();
} else if(file.stat.isDirectory()) {
archive.entry({
name: file.relative.replace(/\\/g, '/') + (file.isNull() ? '/' : ''),
mode: file.stat.mode,
type: 'directory',
});
return cb();
} else {
archive.entry({
name: file.relative.replace(/\\/g, '/') + (file.isNull() ? '/' : ''),
mode: file.stat.mode,
size: file.stat.size,
uid: 0,
gid: 0,
type: 'file',
mtime: file.stat.mtime
}, file.contents);
return cb();
}
}, function (cb) {
if (firstFile === undefined) {
gutil.log('First file is undefined!');
cb();
return;
}
archive.finalize();
createDeb(archive, options, function (deb) {
this.push(new gutil.File({
cwd: firstFile.cwd,
base: firstFile.base,
path: path.join(firstFile.base, filename),
contents: deb
}));
cb();
}.bind(this))
});
};
function createDeb (data, options, cb) {
var deb = new ar.Archive();
streamToBuffer(data, compress(function (err, dataBuffer) {
generateControl(options, compress(function (err, controlBuffer) {
cb(deb
.append(generateDebianBinary(), {
name: 'debian-binary'
})
.append(controlBuffer, {
name: 'control.tar.gz'
})
.append(dataBuffer, {
name: 'data.tar.gz'
})
.finalize()
)
}))
}));
}
function generateControl(options, cb) {
var archive = tar.pack();
archive.entry({name: 'control'}, new Buffer(createControlFile(options)));
if (options.scripts) {
// Adding preinst, postinst, prerm, postrm to package
for (var script in options.scripts) {
if (options.scripts.hasOwnProperty(script)) {
var content = options.scripts[script];
archive.entry({name: script}, new Buffer(content));
}
}
}
streamToBuffer(archive, cb);
archive.finalize();
}
function generateDebianBinary() {
return new Buffer('2.0\n');
}
function compress (cb) {
return function (err, buffer) {
if (err) {
return cb(err);
}
zlib.gzip(buffer, cb);
}
}
function createControlFile (options) {
var controlFileTemplate = [
'Package: <%= name %>',
'Version: <%= version %>',
'Maintainer: <%= maintainer.name %> <<%= maintainer.email %>>'
];
checkFieldOption('architecture', 'Architecture: <%= architecture %>', false, 'all');
checkFieldOption('installedSize', 'Installed-Size: <%= installedSize %>');
checkFieldOption('preDepends', 'Pre-Depends: <%= preDepends %>', true);
checkFieldOption('depends', 'Depends: <%= depends %>', true);
checkFieldOption('recommends', 'Recommends: <%= recommends %>', true);
checkFieldOption('suggests', 'Suggests: <%= suggests %>', true);
checkFieldOption('enhances', 'Enhances: <%= enhances %>', true);
checkFieldOption('section', 'Section: <%= section %>');
checkFieldOption('priority', 'Priority: <%= priority %>');
checkFieldOption('homepage', 'Homepage: <%= homepage %>');
controlFileTemplate.push('Description: <%= short_description %>');
controlFileTemplate.push(' <%= long_description %>');
return lodash.template(controlFileTemplate.join('\n') + '\n')(options);
/**
* @param {string} prop
* @param {string} template
* @param {boolean} [canBeArray=false]
* @param {string=} defaultValue
*/
function checkFieldOption (prop, template, canBeArray, defaultValue) {
canBeArray = canBeArray === true;
if (!options.hasOwnProperty(prop) || !options[prop]) {
if (undefined === defaultValue) {
return;
}
options[prop] = defaultValue;
}
if (canBeArray && Array.isArray(options[prop])) {
options[prop] = options[prop].join(',');
}
controlFileTemplate.push(template);
}
}