-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.js
89 lines (74 loc) · 2.05 KB
/
utils.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
'use strict'
var yazl = require('yazl')
var fs = require('fs')
var path = require('path')
var glob = require('glob')
var shortid = require('shortid')
function zip(cartridges, metadataPath, outfile){
return new Promise((resolve, reject) => {
var zipfile = new yazl.ZipFile()
//glob(cartridges + '/**/*(static|scripts|templates|forms|pipelines)/**', {
glob(cartridges + '/**', {
follow: true,
nosort: true,
nodir: true
},
function(err, matches){
if (err){
reject(err)
return
}
if (matches.length === 0){
reject('No matches for cartridges provided in dw.json')
return
}
for (var i = 0; i < matches.length; i++){
var file = matches[i]
//var compress = !/\.(png|gif|jpg|jpeg)$/.test(file)
var zipPath = path.join(metadataPath, path.relative(cartridges, file))
zipfile.addFile(file, zipPath)
}
zipfile.end()
zipfile.outputStream.on('end', () => {
resolve()
})
zipfile.outputStream.pipe(fs.createWriteStream(outfile))
.on('end', (code) => {
console.log('write Stream Ended', code, '\n')
})
.on('error', (err) => {
reject(err)
})
})
})
}
function zipFiles(files, local_base, remote_base, server){
return new Promise((resolve, reject) => {
var zipfile = new yazl.ZipFile()
var tempzip = shortid.generate() + '.zip'
for (var i = 0; i < files.length; i++){
var file = files[i]
if (file[0]){
zipfile.addEmptyDirectory(file[1])
} else {
var zipPath = path.join(remote_base, path.relative(local_base, file[1]))
zipfile.addFile(file[1], zipPath)
}
}
zipfile.end()
zipfile.outputStream.on('end', () => {
})
zipfile.outputStream.pipe(server.upload_stream(tempzip))
.on('end', (code) => {
resolve(tempzip)
})
.on('error', (err) => {
reject(err)
})
zipfile.on('error', (err) => {
reject(err)
})
})
}
module.exports.zip = zip
module.exports.zipFiles = zipFiles