-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
packager.ts
321 lines (265 loc) · 11.1 KB
/
packager.ts
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import * as path from "path"
import {
computeDefaultAppDirectory, installDependencies, getElectronVersion, use,
exec, isEmptyOrSpaces, statOrNull, getGypEnv
} from "./util/util"
import { all, executeFinally } from "./util/promise"
import { EventEmitter } from "events"
import { Promise as BluebirdPromise } from "bluebird"
import { AppMetadata, DevMetadata, Platform, Arch } from "./metadata"
import { PlatformPackager, BuildInfo, ArtifactCreated, Target } from "./platformPackager"
import { WinPackager } from "./winPackager"
import * as errorMessages from "./errorMessages"
import * as util from "util"
import { deepAssign } from "./util/deepAssign"
import semver = require("semver")
import { warn, log } from "./util/log"
import { AppInfo } from "./appInfo"
import MacPackager from "./macPackager"
import { createTargets } from "./targets/targetFactory"
import { readPackageJson } from "./util/readPackageJson"
import { TmpDir } from "./util/tmp"
import { BuildOptions } from "./builder"
//noinspection JSUnusedLocalSymbols
const __awaiter = require("./util/awaiter")
function addHandler(emitter: EventEmitter, event: string, handler: Function) {
emitter.on(event, handler)
}
export class Packager implements BuildInfo {
readonly projectDir: string
appDir: string
metadata: AppMetadata
devMetadata: DevMetadata
isTwoPackageJsonProjectLayoutUsed = true
electronVersion: string
readonly eventEmitter = new EventEmitter()
appInfo: AppInfo
readonly tempDirManager = new TmpDir()
//noinspection JSUnusedGlobalSymbols
constructor(public options: BuildOptions) {
this.projectDir = options.projectDir == null ? process.cwd() : path.resolve(options.projectDir)
}
artifactCreated(handler: (event: ArtifactCreated) => void): Packager {
addHandler(this.eventEmitter, "artifactCreated", handler)
return this
}
get devPackageFile(): string {
return path.join(this.projectDir, "package.json")
}
async build(): Promise<Map<Platform, Map<String, Target>>> {
const devPackageFile = this.devPackageFile
const extraMetadata = this.options.extraMetadata
const extraBuildMetadata = extraMetadata == null ? null : extraMetadata.build
this.devMetadata = deepAssign(await readPackageJson(devPackageFile), this.options.devMetadata)
if (extraMetadata != null) {
if (extraBuildMetadata != null) {
deepAssign(this.devMetadata, {build: extraBuildMetadata})
delete extraMetadata.build
}
if (extraMetadata.directories != null) {
deepAssign(this.devMetadata, {directories: extraMetadata.directories})
delete extraMetadata.directories
}
}
this.appDir = await computeDefaultAppDirectory(this.projectDir, use(this.devMetadata.directories, it => it!.app))
this.isTwoPackageJsonProjectLayoutUsed = this.appDir !== this.projectDir
const appPackageFile = this.isTwoPackageJsonProjectLayoutUsed ? path.join(this.appDir, "package.json") : devPackageFile
if (this.isTwoPackageJsonProjectLayoutUsed) {
this.metadata = deepAssign(await readPackageJson(appPackageFile), this.options.appMetadata, extraMetadata)
}
else {
if (this.options.appMetadata != null) {
deepAssign(this.devMetadata, this.options.appMetadata)
}
if (extraMetadata != null) {
deepAssign(this.devMetadata, extraMetadata)
}
this.metadata = <any>this.devMetadata
}
this.checkMetadata(appPackageFile, devPackageFile)
checkConflictingOptions(this.devMetadata.build)
this.electronVersion = await getElectronVersion(this.devMetadata, devPackageFile)
this.appInfo = new AppInfo(this.metadata, this.devMetadata)
const cleanupTasks: Array<() => Promise<any>> = []
return executeFinally(this.doBuild(cleanupTasks), () => all(cleanupTasks.map(it => it()).concat(this.tempDirManager.cleanup())))
}
private async doBuild(cleanupTasks: Array<() => Promise<any>>): Promise<Map<Platform, Map<String, Target>>> {
const distTasks: Array<Promise<any>> = []
const outDir = path.resolve(this.projectDir, use(this.devMetadata.directories, it => it!.output) || "dist")
const platformToTarget: Map<Platform, Map<String, Target>> = new Map()
// custom packager - don't check wine
let checkWine = this.options.platformPackagerFactory == null
for (let [platform, archToType] of this.options.targets!) {
if (platform === Platform.MAC && process.platform === Platform.WINDOWS.nodeName) {
throw new Error("Build for MacOS is supported only on MacOS, please see https://github.com/electron-userland/electron-builder/wiki/Multi-Platform-Build")
}
let wineCheck: Promise<string> | null = null
if (checkWine && process.platform !== "win32" && platform === Platform.WINDOWS) {
wineCheck = exec("wine", ["--version"])
}
const helper = this.createHelper(platform, cleanupTasks)
const nameToTarget: Map<String, Target> = new Map()
platformToTarget.set(platform, nameToTarget)
for (let [arch, targets] of archToType) {
await this.installAppDependencies(platform, arch)
if (checkWine && wineCheck != null) {
checkWine = false
checkWineVersion(wineCheck)
}
await helper.pack(outDir, arch, createTargets(nameToTarget, targets, outDir, helper, cleanupTasks), distTasks)
}
for (let target of nameToTarget.values()) {
distTasks.push(target.finishBuild())
}
}
await BluebirdPromise.all(distTasks)
return platformToTarget
}
private createHelper(platform: Platform, cleanupTasks: Array<() => Promise<any>>): PlatformPackager<any> {
if (this.options.platformPackagerFactory != null) {
return this.options.platformPackagerFactory!(this, platform, cleanupTasks)
}
switch (platform) {
case Platform.MAC:
{
const helperClass: typeof MacPackager = require("./macPackager").default
return new helperClass(this)
}
case Platform.WINDOWS:
{
const helperClass: typeof WinPackager = require("./winPackager").WinPackager
return new helperClass(this)
}
case Platform.LINUX:
return new (require("./linuxPackager").LinuxPackager)(this)
default:
throw new Error(`Unknown platform: ${platform}`)
}
}
private checkMetadata(appPackageFile: string, devAppPackageFile: string): void {
const reportError = (missedFieldName: string) => {
throw new Error(`Please specify '${missedFieldName}' in the application package.json ('${appPackageFile}')`)
}
const checkNotEmpty = (name: string, value: string | n) => {
if (isEmptyOrSpaces(value)) {
reportError(name)
}
}
const appMetadata = this.metadata
checkNotEmpty("name", appMetadata.name)
checkNotEmpty("description", appMetadata.description)
checkNotEmpty("version", appMetadata.version)
checkDependencies(this.devMetadata.dependencies)
if ((<any>appMetadata) !== this.devMetadata) {
checkDependencies(appMetadata.dependencies)
if ((<any>appMetadata).build != null) {
throw new Error(util.format(errorMessages.buildInAppSpecified, appPackageFile, devAppPackageFile))
}
}
const build = <any>this.devMetadata.build
if (build == null) {
throw new Error(util.format(errorMessages.buildIsMissed, devAppPackageFile))
}
else {
const author = appMetadata.author
if (author == null) {
throw new Error(`Please specify "author" in the application package.json ('${appPackageFile}') — it is used as company name.`)
}
if (build.name != null) {
throw new Error(util.format(errorMessages.nameInBuildSpecified, appPackageFile))
}
if (build.osx != null) {
warn('"build.osx" is deprecated — please use "mac" instead of "osx"')
}
if (build.prune != null) {
warn("prune is deprecated — development dependencies are never copied in any case")
}
}
}
private async installAppDependencies(platform: Platform, arch: Arch): Promise<any> {
if (this.devMetadata.build.nodeGypRebuild === true) {
log(`Execute node-gyp rebuild for arch ${Arch[arch]}`)
await exec(process.platform === "win32" ? "node-gyp.cmd" : "node-gyp", ["rebuild"], {
env: getGypEnv(this.electronVersion, Arch[arch]),
})
}
if (this.isTwoPackageJsonProjectLayoutUsed) {
if (this.devMetadata.build.npmRebuild === false) {
log("Skip app dependencies rebuild because npmRebuild is set to false")
}
else if (platform.nodeName === process.platform) {
await installDependencies(this.appDir, this.electronVersion, Arch[arch], (await statOrNull(path.join(this.appDir, "node_modules"))) == null ? "install" : "rebuild")
}
else {
log("Skip app dependencies rebuild because platform is different")
}
}
else {
log("Skip app dependencies rebuild because dev and app dependencies are not separated")
}
}
}
export function normalizePlatforms(rawPlatforms: Array<string | Platform> | string | Platform | n): Array<Platform> {
const platforms = rawPlatforms == null || Array.isArray(rawPlatforms) ? (<Array<string | Platform | n>>rawPlatforms) : [rawPlatforms]
if (<any>platforms == null || platforms.length === 0) {
return [Platform.fromString(process.platform)]
}
else if (platforms[0] === "all") {
if (process.platform === Platform.MAC.nodeName) {
return [Platform.MAC, Platform.LINUX, Platform.WINDOWS]
}
else if (process.platform === Platform.LINUX.nodeName) {
// macOS code sign works only on macOS
return [Platform.LINUX, Platform.WINDOWS]
}
else {
return [Platform.WINDOWS]
}
}
else {
return platforms.map(it => it instanceof Platform ? it : Platform.fromString(it!))
}
}
function checkConflictingOptions(options: any) {
for (let name of ["all", "out", "tmpdir", "version", "platform", "dir", "arch", "name", "extra-resource"]) {
if (name in options) {
throw new Error(`Option ${name} is ignored, do not specify it.`)
}
}
}
async function checkWineVersion(checkPromise: Promise<string>) {
function wineError(prefix: string): string {
return `${prefix}, please see https://github.com/electron-userland/electron-builder/wiki/Multi-Platform-Build#${(process.platform === "linux" ? "linux" : "os-x")}`
}
let wineVersion: string
try {
wineVersion = (await checkPromise).trim()
}
catch (e) {
if (e.code === "ENOENT") {
throw new Error(wineError("wine is required"))
}
else {
throw new Error(`Cannot check wine version: ${e}`)
}
}
if (wineVersion.startsWith("wine-")) {
wineVersion = wineVersion.substring("wine-".length)
}
if (wineVersion.split(".").length === 2) {
wineVersion += ".0"
}
if (semver.lt(wineVersion, "1.8.0")) {
throw new Error(wineError(`wine 1.8+ is required, but your version is ${wineVersion}`))
}
}
function checkDependencies(dependencies?: { [key: string]: string }) {
if (dependencies == null) {
return
}
for (let name of ["electron", "electron-prebuilt", "electron-builder"]) {
if (name in dependencies) {
throw new Error(`${name} must be in the devDependencies`)
}
}
}