forked from rockybars/atom-shell-packager
/
common.js
258 lines (213 loc) · 7.26 KB
/
common.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
250
251
252
253
254
255
256
257
258
var asar = require('asar')
var child = require('child_process')
var fs = require('fs-extra')
var minimist = require('minimist')
var os = require('os')
var path = require('path')
var series = require('run-series')
var archs = ['ia32', 'x64']
var platforms = ['darwin', 'linux', 'mas', 'win32']
function parseCLIArgs (argv) {
var args = minimist(argv, {
boolean: [
'prune',
'asar',
'all',
'overwrite',
'download.strictSSL'
],
alias: {
'download.strictSSL': 'strict-ssl'
},
default: {
'download.strictSSL': true
}
})
args.dir = args._[0]
args.name = args._[1]
var protocolSchemes = [].concat(args.protocol || [])
var protocolNames = [].concat(args['protocol-name'] || [])
if (protocolSchemes && protocolNames && protocolNames.length === protocolSchemes.length) {
args.protocols = protocolSchemes.map(function (scheme, i) {
return {schemes: [scheme], name: protocolNames[i]}
})
}
// minimist doesn't support multiple types for a single argument (in this case, `String` or `false`)
if (args.tmpdir === 'false') {
args.tmpdir = false
}
// (in this case, `Object` or `true`)
if (args['osx-sign'] === 'true') {
args['osx-sign'] = true
}
return args
}
function asarApp (appPath, asarOptions, cb) {
var src = path.join(appPath)
var dest = path.join(appPath, '..', 'app.asar')
asar.createPackageWithOptions(src, dest, asarOptions, function (err) {
if (err) return cb(err)
fs.remove(src, function (err) {
if (err) return cb(err)
cb(null, dest)
})
})
}
function generateFinalBasename (opts) {
return `${opts.name}-${opts.platform}-${opts.arch}`
}
function generateFinalPath (opts) {
return path.join(opts.out || process.cwd(), generateFinalBasename(opts))
}
function subOptionWarning (properties, optionName, parameter, value) {
if (properties.hasOwnProperty(parameter)) {
console.warn(`WARNING: ${optionName}.${parameter} will be inferred from the main options`)
}
properties[parameter] = value
}
function userIgnoreFilter (opts) {
var ignore = opts.ignore || []
var ignoreFunc = null
if (typeof (ignore) === 'function') {
ignoreFunc = function (file) { return !ignore(file) }
} else {
if (!Array.isArray(ignore)) ignore = [ignore]
ignoreFunc = function filterByRegexes (file) {
for (var i = 0; i < ignore.length; i++) {
if (file.match(ignore[i])) {
return false
}
}
return true
}
}
var normalizedOut = opts.out ? path.resolve(opts.out) : null
var outIgnores = []
if (normalizedOut === null || normalizedOut === process.cwd()) {
platforms.forEach(function (platform) {
archs.forEach(function (arch) {
outIgnores.push(path.join(process.cwd(), `${opts.name}-${platform}-${arch}`))
})
})
} else {
outIgnores.push(normalizedOut)
}
return function filter (file) {
if (outIgnores.indexOf(file) !== -1) {
return false
}
var name = file.split(path.resolve(opts.dir))[1]
if (path.sep === '\\') {
// convert slashes so unix-format ignores work
name = name.replace(/\\/g, '/')
}
return ignoreFunc(name)
}
}
module.exports = {
archs: archs,
platforms: platforms,
parseCLIArgs: parseCLIArgs,
isPlatformMac: function isPlatformMac (platform) {
return platform === 'darwin' || platform === 'mas'
},
subOptionWarning: subOptionWarning,
createDownloadOpts: function createDownloadOpts (opts, platform, arch) {
if (opts.hasOwnProperty('cache')) {
console.warn('The cache parameter is deprecated, use download.cache instead')
}
if (opts.hasOwnProperty('strict-ssl')) {
console.warn('The strict-ssl parameter is deprecated, use download.strictSSL instead')
}
var downloadOpts = Object.assign({
cache: opts.cache,
strictSSL: opts['strict-ssl']
}, opts.download)
subOptionWarning(downloadOpts, 'download', 'platform', platform)
subOptionWarning(downloadOpts, 'download', 'arch', arch)
subOptionWarning(downloadOpts, 'download', 'version', opts.version)
return downloadOpts
},
generateFinalBasename: generateFinalBasename,
generateFinalPath: generateFinalPath,
initializeApp: function initializeApp (opts, templatePath, appRelativePath, callback) {
// Performs the following initial operations for an app:
// * Creates temporary directory
// * Copies template into temporary directory
// * Copies user's app into temporary directory
// * Prunes non-production node_modules (if opts.prune is set)
// * Creates an asar (if opts.asar is set)
var tempPath
if (opts.tmpdir === false) {
tempPath = generateFinalPath(opts)
} else {
tempPath = path.join(opts.tmpdir || os.tmpdir(), 'electron-packager', `${opts.platform}-${opts.arch}`, generateFinalBasename(opts))
}
// Path to `app` directory
var appPath = path.join(tempPath, appRelativePath)
var resourcesPath = path.resolve(appPath, '..')
var operations = [
function (cb) {
fs.move(templatePath, tempPath, {clobber: true}, cb)
},
function (cb) {
fs.copy(opts.dir, appPath, {filter: userIgnoreFilter(opts), dereference: true}, cb)
},
function (cb) {
// Support removing old default_app folder that is now an asar archive
fs.remove(path.join(resourcesPath, 'default_app'), cb)
},
function (cb) {
fs.remove(path.join(resourcesPath, 'default_app.asar'), cb)
}
]
// Prune and asar are now performed before platform-specific logic, primarily so that
// appPath is predictable (e.g. before .app is renamed for mac)
if (opts.prune) {
operations.push(function (cb) {
child.exec('npm prune --production', {cwd: appPath}, cb)
})
}
if (opts.asar) {
operations.push(function (cb) {
var asarOptions = {}
if (opts['asar-unpack']) {
asarOptions.unpack = opts['asar-unpack']
}
if (opts['asar-unpack-dir']) {
asarOptions.unpackDir = opts['asar-unpack-dir']
}
asarApp(path.join(appPath), asarOptions, cb)
})
}
series(operations, function (err) {
if (err) return callback(err)
// Resolve to path to temporary app folder for platform-specific processes to use
callback(null, tempPath)
})
},
moveApp: function finalizeApp (opts, tempPath, callback) {
var finalPath = generateFinalPath(opts)
if (opts.tmpdir === false) {
callback(null, finalPath)
return
}
fs.move(tempPath, finalPath, function (err) {
callback(err, finalPath)
})
},
normalizeExt: function normalizeExt (filename, targetExt, cb) {
// Forces a filename to a given extension and fires the given callback with the normalized filename,
// if it exists. Otherwise reports the error from the fs.stat call.
// (Used for resolving icon filenames, particularly during --all runs.)
// This error path is used by win32.js if no icon is specified
if (!filename) return cb(new Error('No filename specified to normalizeExt'))
var ext = path.extname(filename)
if (ext !== targetExt) {
filename = filename.slice(0, filename.length - ext.length) + targetExt
}
fs.stat(filename, function (err) {
cb(err, err ? null : filename)
})
}
}