-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathmacroExpander.ts
67 lines (58 loc) · 2.2 KB
/
macroExpander.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
import { InvalidConfigurationError } from "builder-util"
import { Nullish } from "builder-util-runtime"
import { AppInfo } from "../appInfo"
export function expandMacro(pattern: string, arch: string | Nullish, appInfo: AppInfo, extra: any = {}, isProductNameSanitized = true): string {
if (arch == null) {
pattern = pattern
// tslint:disable-next-line:no-invalid-template-strings
.replace("-${arch}", "")
// tslint:disable-next-line:no-invalid-template-strings
.replace(" ${arch}", "")
// tslint:disable-next-line:no-invalid-template-strings
.replace("_${arch}", "")
// tslint:disable-next-line:no-invalid-template-strings
.replace("/${arch}", "")
}
return pattern.replace(/\${([_a-zA-Z./*+]+)}/g, (match, p1): string => {
switch (p1) {
case "productName":
return isProductNameSanitized ? appInfo.sanitizedProductName : appInfo.productName
case "arch":
if (arch == null) {
// see above, we remove macro if no arch
return ""
}
return arch
case "author": {
const companyName = appInfo.companyName
if (companyName == null) {
throw new InvalidConfigurationError(`cannot expand pattern "${pattern}": author is not specified`, "ERR_ELECTRON_BUILDER_AUTHOR_UNSPECIFIED")
}
return companyName
}
case "platform":
return process.platform
case "channel":
return appInfo.channel || "latest"
default: {
if (p1 in appInfo) {
return (appInfo as any)[p1]
}
if (p1.startsWith("env.")) {
const envName = p1.substring("env.".length)
const envValue = process.env[envName]
if (envValue == null) {
throw new InvalidConfigurationError(`cannot expand pattern "${pattern}": env ${envName} is not defined`, "ERR_ELECTRON_BUILDER_ENV_NOT_DEFINED")
}
return envValue
}
const value = extra[p1]
if (value == null) {
throw new InvalidConfigurationError(`cannot expand pattern "${pattern}": macro ${p1} is not defined`, "ERR_ELECTRON_BUILDER_MACRO_NOT_DEFINED")
} else {
return value
}
}
}
})
}