-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
148 lines (135 loc) · 3.81 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
/*! cross-zip. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
module.exports = {
zip,
zipSync,
unzip,
unzipSync
}
const cp = require('child_process')
const fs = require('fs')
const os = require('os')
const path = require('path')
function zip (inPath, outPath, cb) {
if (!cb) cb = function () {}
if (process.platform === 'win32') {
fs.stat(inPath, function (err, stats) {
if (err) return cb(err)
if (stats.isFile()) {
copyToTemp()
} else {
doZip()
}
})
} else {
doZip()
}
// Windows zip command cannot zip files, only directories. So move the file into
// a temporary directory before zipping.
function copyToTemp () {
fs.readFile(inPath, function (err, inFile) {
if (err) return cb(err)
const tmpPath = path.join(os.tmpdir(), 'cross-zip-' + Date.now())
fs.mkdir(tmpPath, function (err) {
if (err) return cb(err)
fs.writeFile(path.join(tmpPath, path.basename(inPath)), inFile, function (err) {
if (err) return cb(err)
inPath = tmpPath
doZip()
})
})
})
}
// Windows zip command does not overwrite existing files. So do it manually first.
function doZip () {
if (process.platform === 'win32') {
fs.rmdir(outPath, { recursive: true, maxRetries: 3 }, doZip2)
} else {
doZip2()
}
}
function doZip2 () {
const opts = {
cwd: path.dirname(inPath),
maxBuffer: Infinity
}
cp.execFile(getZipCommand(), getZipArgs(inPath, outPath), opts, function (err) {
cb(err)
})
}
}
function zipSync (inPath, outPath) {
if (process.platform === 'win32') {
if (fs.statSync(inPath).isFile()) {
const inFile = fs.readFileSync(inPath)
const tmpPath = path.join(os.tmpdir(), 'cross-zip-' + Date.now())
fs.mkdirSync(tmpPath)
fs.writeFileSync(path.join(tmpPath, path.basename(inPath)), inFile)
inPath = tmpPath
}
fs.rmdirSync(outPath, { recursive: true, maxRetries: 3 })
}
const opts = {
cwd: path.dirname(inPath),
maxBuffer: Infinity
}
cp.execFileSync(getZipCommand(), getZipArgs(inPath, outPath), opts)
}
function unzip (inPath, outPath, cb) {
if (!cb) cb = function () {}
const opts = {
maxBuffer: Infinity
}
cp.execFile(getUnzipCommand(), getUnzipArgs(inPath, outPath), opts, function (err) {
cb(err)
})
}
function unzipSync (inPath, outPath) {
const opts = {
maxBuffer: Infinity
}
cp.execFileSync(getUnzipCommand(), getUnzipArgs(inPath, outPath), opts)
}
function getZipCommand () {
if (process.platform === 'win32') {
return 'powershell.exe'
} else {
return 'zip'
}
}
function getUnzipCommand () {
if (process.platform === 'win32') {
return 'powershell.exe'
} else {
return 'unzip'
}
}
function quotePath (pathToTransform) {
return '"' + pathToTransform + '"'
}
function getZipArgs (inPath, outPath) {
if (process.platform === 'win32') {
return [
'-nologo',
'-noprofile',
'-command', '& { param([String]$myInPath, [String]$myOutPath); Add-Type -A "System.IO.Compression.FileSystem"; [IO.Compression.ZipFile]::CreateFromDirectory($myInPath, $myOutPath); exit !$? }',
'-myInPath', quotePath(inPath),
'-myOutPath', quotePath(outPath)
]
} else {
const fileName = path.basename(inPath)
return ['-r', '-y', outPath, fileName]
}
}
function getUnzipArgs (inPath, outPath) {
if (process.platform === 'win32') {
return [
'-nologo',
'-noprofile',
'-command', '& { param([String]$myInPath, [String]$myOutPath); Add-Type -A "System.IO.Compression.FileSystem"; [IO.Compression.ZipFile]::ExtractToDirectory($myInPath, $myOutPath); exit !$? }',
'-myInPath', quotePath(inPath),
'-myOutPath', quotePath(outPath)
]
} else {
return ['-o', inPath, '-d', outPath]
}
}