util.js
7.39 KB
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
/**
* @module util
*/
'use strict'
const child = require('child_process')
const fs = require('fs-extra')
const path = require('path')
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 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)
})
})
}
/**
* This function returns a flattened list of elements from an array of lists.
* @function
* @param {*} list - List.
* @returns Flattened list.
*/
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)
}
}
let 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 fs.lstat(path.join(appFrameworksPath, 'Squirrel.framework'))
.then(function () {
resolve('darwin')
})
.catch(function () {
resolve('mas')
})
})
}
const isBinaryFile = require("istextorbinary").isBinary;
/**
* This function returns a promise resolving the file path if file binary. We check filepath extension first to reduce overhead of reading the file to a buffer for it to scan start/mid/end encoding of the file
* @function
* @param {string} filePath - Path to file.
* @returns {Promise} Promise resolving file path or undefined.
*/
const getFilePathIfBinarySync = module.exports.getFilePathIfBinarySync = function (filePath) {
const forceAllowedExts = [
'.dylib', // Dynamic library
'.node' // Native node addon
]
const name = path.basename(filePath)
const ext = path.extname(filePath)
// Sometimes .node is the base name, not as a file extension
// https://github.com/electron/electron-osx-sign/pull/169
if (forceAllowedExts.includes(ext) || forceAllowedExts.includes(name)) {
return filePath
}
// Still consider the file as binary if extension is empty or seems invalid
// https://github.com/electron-userland/electron-builder/issues/5465
if ((ext === '' || ext.indexOf(' ') >= 0) && name[0] !== '.') {
return (isBinaryFile(filePath, null) || isBinaryFile(null, fs.readFileSync(filePath))) ? filePath : undefined
}
return 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 = async function (opts) {
if (!opts.app) {
throw new Error('Path to aplication must be specified.')
}
if (path.extname(opts.app) !== '.app') {
throw new Error('Extension of application must be `.app`.')
}
await fs.lstat(opts.app)
}
/**
* 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 = async function (dirPath) {
debuglog('Walking... ' + dirPath)
async function _walkAsync(dirPath) {
const names = await fs.readdir(dirPath)
return await Promise.all(names.map(async (name) => {
let filePath = path.join(dirPath, name)
const stat = await fs.lstat(filePath)
if (stat.isFile()) {
const forceRemoveExts = [
'.cstemp' // Temporary file generated from past codesign
]
if (forceRemoveExts.includes(path.extname(filePath))) {
debuglog('Removing... ' + filePath)
await fs.unlink(filePath)
return
}
const binaryPath = getFilePathIfBinarySync(filePath)
if (binaryPath) {
return binaryPath
}
} else if (stat.isDirectory() && !stat.isSymbolicLink()) {
const result = await _walkAsync(filePath)
switch (path.extname(filePath)) {
case '.app': // Application
case '.framework': // Framework
result.push(filePath)
}
return result
}
}))
}
return module.exports.flatList(await _walkAsync(dirPath))
}