forked from rockybars/atom-shell-packager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
246 lines (213 loc) · 8.17 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
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
'use strict'
const common = require('./common')
const debug = require('debug')('electron-packager')
const download = require('electron-download')
const extract = require('extract-zip')
const fs = require('fs-extra')
const prop = require('lodash.get')
const metadata = require('./package.json')
const os = require('os')
const path = require('path')
const resolve = require('resolve')
const series = require('run-series')
var supportedArchs = common.archs.reduce(function (result, arch) {
result[arch] = 1
return result
}, {})
var supportedPlatforms = {
// Maps to module ID for each platform (lazy-required if used)
darwin: './mac',
linux: './linux',
mas: './mac', // map to darwin
win32: './win32'
}
function debugHostInfo () {
debug(`Electron Packager ${metadata.version}`)
debug(`Node ${process.version}`)
debug(`Host Operating system: ${process.platform} (${process.arch})`)
}
function validateList (list, supported, name) {
// Validates list of architectures or platforms.
// Returns a normalized array if successful, or an error message string otherwise.
if (!list) return `Must specify ${name}`
if (list === 'all') return Object.keys(supported)
if (!Array.isArray(list)) list = list.split(',')
for (var i = list.length; i--;) {
if (!supported[list[i]]) {
return `Unsupported ${name} ${list[i]}; must be one of: ${Object.keys(supported).join(', ')}`
}
}
return list
}
function getNameAndVersion (opts, dir, cb) {
var pkg = require(path.join(dir, 'package.json'))
if (!pkg) return cb(Error(`no package.json file found in ${dir}`))
// Name and version provided, no need to infer
if (opts.name && opts.version) return cb(null)
// Infer name from package.json
opts.name = opts.name || prop(pkg, 'productName') || prop(pkg, 'name')
// Infer electron version from package.json
var electronVersion = prop(pkg, 'dependencies.electron') || prop(pkg, 'devDependencies.electron')
var electronPrebuiltVersion = prop(pkg, 'dependencies.electron-prebuilt') || prop(pkg, 'devDependencies.electron-prebuilt')
opts.version = opts.version || electronVersion || electronPrebuiltVersion
if (!electronVersion && !electronPrebuiltVersion) return cb(null)
var packageName = electronVersion ? 'electron' : 'electron-prebuilt'
resolve(packageName, {basedir: dir}, function (err, res, pkg) {
if (err) return cb(err)
debug('Inferring target Electron version from `' + packageName + '` dependency or devDependency in package.json')
opts.version = pkg.version
return cb(null)
})
}
function createSeries (opts, archs, platforms) {
var tempBase = path.join(opts.tmpdir || os.tmpdir(), 'electron-packager')
function testSymlink (cb) {
var testPath = path.join(tempBase, 'symlink-test')
var testFile = path.join(testPath, 'test')
var testLink = path.join(testPath, 'testlink')
series([
function (cb) {
fs.outputFile(testFile, '', cb)
},
function (cb) {
fs.symlink(testFile, testLink, cb)
}
], function (err) {
var result = !err
fs.remove(testPath, function () {
cb(result) // ignore errors on cleanup
})
})
}
var combinations = []
archs.forEach(function (arch) {
platforms.forEach(function (platform) {
// Electron does not have 32-bit releases for Mac OS X, so skip that combination
if (common.isPlatformMac(platform) && arch === 'ia32') return
combinations.push(common.createDownloadOpts(opts, platform, arch))
})
})
var tasks = []
var useTempDir = opts.tmpdir !== false
if (useTempDir) {
tasks.push(function (cb) {
fs.remove(tempBase, cb)
})
}
return tasks.concat(combinations.map(function (combination) {
var arch = combination.arch
var platform = combination.platform
var version = combination.version
return function (callback) {
download(combination, function (err, zipPath) {
if (err) return callback(err)
function createApp (comboOpts) {
var buildParentDir
if (useTempDir) {
buildParentDir = tempBase
} else {
buildParentDir = opts.out || process.cwd()
}
var buildDir = path.join(buildParentDir, `${platform}-${arch}-template`)
console.error(`Packaging app for platform ${platform} ${arch} using electron v${version}`)
series([
function (cb) {
fs.mkdirs(buildDir, cb)
},
function (cb) {
extract(zipPath, {dir: buildDir}, cb)
},
function (cb) {
if (!opts.afterExtract || !Array.isArray(opts.afterExtract)) {
cb()
} else {
var newFunctions = opts.afterExtract.map(function (fn) {
return fn.bind(this, buildDir, version, platform, arch)
})
series(newFunctions, cb)
}
}
], function () {
require(supportedPlatforms[platform]).createApp(comboOpts, buildDir, callback)
})
}
// Create delegated options object with specific platform and arch, for output directory naming
var comboOpts = Object.create(opts)
comboOpts.arch = arch
comboOpts.platform = platform
if (!useTempDir) {
createApp(comboOpts)
return
}
function checkOverwrite () {
var finalPath = common.generateFinalPath(comboOpts)
fs.exists(finalPath, function (exists) {
if (exists) {
if (opts.overwrite) {
fs.remove(finalPath, function () {
createApp(comboOpts)
})
} else {
console.error(`Skipping ${platform} ${arch} (output dir already exists, use --overwrite to force)`)
callback()
}
} else {
createApp(comboOpts)
}
})
}
if (common.isPlatformMac(combination.platform)) {
testSymlink(function (result) {
if (result) return checkOverwrite()
console.error(`Cannot create symlinks; skipping ${combination.platform} platform`)
callback()
})
} else {
checkOverwrite()
}
})
}
}))
}
module.exports = function packager (opts, cb) {
debugHostInfo()
var archs = validateList(opts.all ? 'all' : opts.arch, supportedArchs, 'arch')
var platforms = validateList(opts.all ? 'all' : opts.platform, supportedPlatforms, 'platform')
if (!Array.isArray(archs)) return cb(new Error(archs))
if (!Array.isArray(platforms)) return cb(new Error(platforms))
debug(`Target Platforms: ${platforms.join(', ')}`)
debug(`Target Architectures: ${archs.join(', ')}`)
getNameAndVersion(opts, opts.dir || process.cwd(), function (err) {
if (err) {
err.message = 'Unable to determine application name or Electron version. ' +
'Please specify an application name and Electron version.\n\n' +
'For more infomation, please see \n' +
'https://github.com/electron-userland/electron-packager/blob/master/docs/api.md#name or \n' +
'https://github.com/electron-userland/electron-packager/blob/master/docs/api.md#version\n\n' +
err.message
return cb(err)
}
debug(`Application name: ${opts.name}`)
debug(`Target Electron version: ${opts.version}`)
// Ignore this and related modules by default
var defaultIgnores = [
'/node_modules/electron($|/)',
'/node_modules/electron-prebuilt($|/)',
'/node_modules/electron-packager($|/)',
'/\\.git($|/)',
'/node_modules/\\.bin($|/)'
]
if (typeof (opts.ignore) !== 'function') {
if (opts.ignore && !Array.isArray(opts.ignore)) opts.ignore = [opts.ignore]
opts.ignore = (opts.ignore) ? opts.ignore.concat(defaultIgnores) : defaultIgnores
debug(`Ignored path regular expressions:\n${opts.ignore.map(function (ignore) { return `* ${ignore}` }).join('\n')}`)
}
series(createSeries(opts, archs, platforms), function (err, appPaths) {
if (err) return cb(err)
cb(null, appPaths.filter(function (appPath) {
// Remove falsy entries (e.g. skipped platforms)
return appPath
}))
})
})
}