forked from rektide/gulp-crx-e-lance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulp-crx-e-lance.js
244 lines (216 loc) · 5.3 KB
/
gulp-crx-e-lance.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
'use strict';
var fs= require('fs');
var path= require('path')
var gutil= require('gulp-util')
var through= require('through2')
var mkdirp= require('mkdirp')
var mktmpdir= require('mktmpdir')
var crx= require('crx')
var _crx= '.crx'
// polyfill for String.prototype.endsWith (ES6)
// see: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith#Polyfill
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if (position === undefined || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}
module.exports= (function pack(options) {
options= options || {}
if (!options.dest){
throw new gutil.PluginError('gulp-crx-e-lance', '`dest` or `name` required')
}
if(options.dest.endsWith(_crx)){
options.dest= options.dest.substring(0, options.dest.length-_crx.length)
}
function sendAwaits(awaits, err, ok){
if(err)
err= new gutil.PluginError('gulp-crx-e-lance', err)
for(var i in awaits){
awaits[i](err, ok)
}
awaits.splice(0)
}
function awaitThunk(awaits){
return function(err, ok){
sendAwaits(awaits, err, ok)
}
}
var awaitTmp= [function(err,ok){
options.tmp= function(cb){
cb(err, ok)
}
}],
tmp= options.tmp
options.tmp= function(cb){
awaitTmp.push(cb)
}
function insureDir(err, dir){
if(err){
sendAwaits(awaitTmp, err)
return
}
mkdirp(dir, awaitThunk(awaitTmp))
}
if(tmp instanceof Function){
tmp(insureDir)
}else if(tmp instanceof String){
insureDir(undefined, tmp)
}else if(tmp){
sendAwaits(new TypeError('unexpected type for `tmp` parameter'))
}else{
var base= path.dirname(options.dest)
if(base == '.')
base= undefined
else
base= [base]
mktmpdir('gulp-crx-e-lance', base, awaitThunk(awaitTmp))
}
var cleanup
if(options.cleanup instanceof Function){
cleanup= options.cleanup
}else if(options.cleanup === true || options.cleanup === false){
// default cleanup
options.cleanup= true
cleanup= function(err){
options.tmp(function(err,ok){
if(err)
return
//console.log('cleanup', ok)
fs.rmdir(ok)
})
}
}else if(options.cleanup){
sendAwaits(new TypeError('unexpected type for `cleanup` parameter'))
}
var awaitDone= []
function doneAwait(){
var _cb,
_err,
_ok
awaitDone.push(function(cb){
if(_err !== undefined || _ok !== undefined){
// finish
cb(_err, _ok)
_cb= null
_err= null
_ok= null
}else{
// cb when ready
_cb= cb
}
})
// resolve handler
function cb(err, ok){
var noCb = _cb === undefined
if(_cb){
_cb(err, ok)
_cb= null
return
}
if(noCb && _err !== undefined && _ok !== undefined ){
throw new Error('freaky weird gulp-crx-e-lance state')
}
_err= err
_ok= ok
}
return cb
}
var keyDone= doneAwait()
if(options.key){
var key= options.key
options.key= function(cb){
keyDone(undefined, key)
}
}else{
var keyfile= options.keyfile|| 'key.pem'
fs.exists(keyfile, function(exists) {
function haveFile(file){
fs.readFile(file, function(err, contents){
if(!err)
key= contents
options.key= contents
//console.log('key', contents !== undefined)
keyDone(undefined, contents)
})
return
}
if (exists){
//console.log('keyfile!')
haveFile(keyfile)
return
}
var base= path.basename(keyfile)
//console.log('mkdir',base)
mkdirp(base, function(){
var pubPath = keyfile + '.pub',
command = 'ssh-keygen -N "" -b 2048 -t rsa -f ' + path.basename(key),
keybase= path.dirname(keyfile)
//console.log('exec', command)
exec(command, {cwd: keybase}, function(err) {
if (err)
throw err
//console.log('nokey')
haveFile(keyfile)
// TODO: find a way to prevent .pub output
// TODO: i kind of like it but i'm not sure where this'll land and if it's persistent enough
//fs.unlink(pubPath)
})
})
})
}
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
//console.log('nullfile')
cb(null, file)
return
}
if (file.isStream()) {
//console.log('streamfile')
cb(new gutil.PluginError('gulp-crx-e-lance', 'Streaming not supported'))
return
}
//console.log('file', file)
var self= this,
await= doneAwait()
options.tmp(function(err, tmp){
//console.log('got tmp', process.cwd(), file.path, options.input)
if (err) {
//console.log('never tmped', err)
//console.log('but tmp', tmp)
cb(new gutil.PluginError('gulp-crx-e-lance', err, {fileName: file.path}))
return
}
var destFilename= path.join(tmp, path.relative(process.cwd(), file.path))
//console.log('desting', destFilename, path.dirname(destFilename))
mkdirp(path.dirname(destFilename), function(err){
//console.log('mkdir', destFilename, file.contents)
fs.writeFile(destFilename, file.contents, function(err){
await(err)
cb()
})
})
})
}).on('end', function(){
//console.log('end')
function done(){
//console.log('done')
if(options.cleanup)
options.cleanup()
}
function wait(err, ok){
//console.log('WAIT',err, ok)
if(awaitDone.length){
awaitDone.pop()(wait)
}else{
done()
}
}
wait()
})
})