forked from electron/osx-sign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
249 lines (221 loc) · 7.38 KB
/
util.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
245
246
247
248
249
/**
* @module util
*/
'use strict'
const child = require('child_process')
const fs = require('fs')
const path = require('path')
const Promise = require('bluebird')
const debug = require('debug')
/**
* This callback is used across signing and flattening.
* @callback RequestCallback
* @param {?Error} err
*/
/** @function */
const debuglog = module.exports.debuglog = debug('electron-osx-sign')
debuglog.log = console.log.bind(console)
/** @function */
const debugwarn = module.exports.debugwarn = debug('electron-osx-sign:warn')
debugwarn.log = console.warn.bind(console)
/** @function */
const isBinaryFileAsync = module.exports.isBinaryFileAsync = Promise.promisify(require('isbinaryfile'))
/** @function */
const removePassword = function (input) {
return input.replace(/(-P |pass:|\/p|-pass )([^ ]+)/, function (match, p1, p2) {
return `${p1}***`
})
}
/** @function */
module.exports.execFileAsync = function (file, args, options) {
if (debuglog.enabled) {
debuglog('Executing...', file, args && Array.isArray(args) ? removePassword(args.join(' ')) : '')
}
return new Promise(function (resolve, reject) {
child.execFile(file, args, options, function (err, stdout, stderr) {
if (err) {
debuglog('Error executing file:', '\n',
'> Stdout:', stdout, '\n',
'> Stderr:', stderr)
reject(err)
return
}
resolve(stdout)
})
})
}
/** @function */
const lstatAsync = module.exports.lstatAsync = Promise.promisify(fs.lstat)
/** @function */
const readdirAsync = module.exports.readdirAsync = Promise.promisify(fs.readdir)
/** @function */
module.exports.readFileAsync = Promise.promisify(fs.readFile)
/** @function */
module.exports.writeFileAsync = Promise.promisify(fs.writeFile)
/**
* This function returns a flattened list of elements from an array of lists.
* @function
* @param {*} list - List.
* @returns Flattened list.
*/
var flatList = module.exports.flatList = function (list) {
function populateResult (list) {
if (!Array.isArray(list)) {
result.push(list)
} else if (list.length > 0) {
for (let item of list) if (item) populateResult(item)
}
}
var result = []
populateResult(list)
return result
}
/**
* This function returns the path to app contents.
* @function
* @param {Object} opts - Options.
* @returns {string} App contents path.
*/
var getAppContentsPath = module.exports.getAppContentsPath = function (opts) {
return path.join(opts.app, 'Contents')
}
/**
* This function returns the path to app frameworks within contents.
* @function
* @param {Object} opts - Options.
* @returns {string} App frameworks path.
*/
var getAppFrameworksPath = module.exports.getAppFrameworksPath = function (opts) {
return path.join(getAppContentsPath(opts), 'Frameworks')
}
/**
* This function returns a promise copying a file from the source to the target.
* @function
* @param {string} source - Source path.
* @param {string} target - Target path.
* @returns {Promise} Promise.
*/
module.exports.copyFileAsync = function (source, target) {
debuglog('Copying file...', '\n',
'> Source:', source, '\n',
'> Target:', target)
return new Promise(function (resolve, reject) {
var readStream = fs.createReadStream(source)
readStream.on('error', reject)
var writeStream = fs.createWriteStream(target)
writeStream.on('error', reject)
writeStream.on('close', resolve)
readStream.pipe(writeStream)
})
}
/**
* This function returns a promise with platform resolved.
* @function
* @param {Object} opts - Options.
* @returns {Promise} Promise resolving platform.
*/
var detectElectronPlatformAsync = module.exports.detectElectronPlatformAsync = function (opts) {
return new Promise(function (resolve) {
var appFrameworksPath = getAppFrameworksPath(opts)
// The presence of Squirrel.framework identifies a Mac App Store build as used in https://github.com/atom/electron/blob/master/docs/tutorial/mac-app-store-submission-guide.md
return lstatAsync(path.join(appFrameworksPath, 'Squirrel.framework'))
.then(function () {
resolve('darwin')
})
.catch(function () {
resolve('mas')
})
})
}
/**
* This function returns a promise resolving the file path if file binary.
* @function
* @param {string} filePath - Path to file.
* @returns {Promise} Promise resolving file path or undefined.
*/
var getFilePathIfBinaryAsync = module.exports.getFilePathIfBinaryAsync = function (filePath) {
return isBinaryFileAsync(filePath)
.then(function (isBinary) {
return isBinary ? filePath : undefined
})
}
/**
* This function returns a promise validating opts.app, the application to be signed or flattened.
* @function
* @param {Object} opts - Options.
* @returns {Promise} Promise.
*/
module.exports.validateOptsAppAsync = function (opts) {
if (!opts.app) {
return Promise.reject(new Error('Path to aplication must be specified.'))
}
if (path.extname(opts.app) !== '.app') {
return Promise.reject(new Error('Extension of application must be `.app`.'))
}
return lstatAsync(opts.app)
.thenReturn()
}
/**
* This function returns a promise validating opts.platform, the platform of Electron build. It allows auto-discovery if no opts.platform is specified.
* @function
* @param {Object} opts - Options.
* @returns {Promise} Promise.
*/
module.exports.validateOptsPlatformAsync = function (opts) {
if (opts.platform) {
if (opts.platform === 'mas' || opts.platform === 'darwin') {
return Promise.resolve()
} else {
debugwarn('`platform` passed in arguments not supported, checking Electron platform...')
}
} else {
debugwarn('No `platform` passed in arguments, checking Electron platform...')
}
return detectElectronPlatformAsync(opts)
.then(function (platform) {
opts.platform = platform
})
}
/**
* This function returns a promise resolving all child paths within the directory specified.
* @function
* @param {string} dirPath - Path to directory.
* @returns {Promise} Promise resolving child paths needing signing in order.
*/
module.exports.walkAsync = function (dirPath) {
debuglog('Walking... ' + dirPath)
var unlinkAsync = Promise.promisify(fs.unlink)
function _walkAsync (dirPath) {
return readdirAsync(dirPath)
.then(function (names) {
return Promise.map(names, function (name) {
var filePath = path.join(dirPath, name)
return lstatAsync(filePath)
.then(function (stat) {
if (stat.isFile()) {
switch (path.extname(filePath)) {
case '.cstemp': // Temporary file generated from past codesign
debuglog('Removing... ' + filePath)
return unlinkAsync(filePath)
.thenReturn(undefined)
default:
return getFilePathIfBinaryAsync(filePath)
}
} else if (stat.isDirectory() && !stat.isSymbolicLink()) {
return _walkAsync(filePath)
.then(function (result) {
switch (path.extname(filePath)) {
case '.app': // Application
case '.framework': // Framework
result.push(filePath)
}
return result
})
}
})
})
})
}
return _walkAsync(dirPath)
.then(flatList)
}