-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
177 lines (154 loc) · 4.94 KB
/
index.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
import PluginBase from "@electron-forge/plugin-base"
import {
ForgeMakeResult,
ForgeMultiHookMap,
IForgeResolvableMaker,
ResolvedForgeConfig,
} from "@electron-forge/shared-types"
import { execSync, ExecSyncOptionsWithStringEncoding } from "child_process"
import fs from "fs-extra"
import path from "path"
type ConfigTypes = {
userName: string
password: string
credentialId: string
userTotp: string
signToolPath: string
}
class ElectronForgeSslCodeSignPlugin extends PluginBase<ConfigTypes> {
name = "@burzo/electron-forge-ssl-code-sign-plugin"
constructor(config: ConfigTypes) {
super(config)
this.config = config
}
getHooks(): ForgeMultiHookMap {
return { postMake: [this.postMake] }
}
postMake = async (
forgeConfig: ResolvedForgeConfig,
makeResults: ForgeMakeResult[],
) => {
const squirrelMaker: IForgeResolvableMaker = forgeConfig.makers.find(
(maker) =>
(maker as IForgeResolvableMaker)?.name ===
"@electron-forge/maker-squirrel",
) as IForgeResolvableMaker
if (!squirrelMaker) {
throw new Error(
`The plugin ${this.name} can not work without @electron-forge/maker-squirrel. Remove it from the plugins array.`,
)
}
const { userName, password, credentialId, userTotp, signToolPath } =
this.config
return makeResults.map((data) => {
const { artifacts, platform } = data
if (platform !== "win32") {
return data
}
if (!userName || !password || !credentialId || !signToolPath) {
throw new Error(
`You did not provide all the required config variables to ${
this.name
}.\nCurrent values:\n${Object.keys(this.config)
.map((key) => `${key} - ${this.config[key as keyof ConfigTypes]}`)
.join("\n")}`,
)
}
const releasesPath = artifacts[0]
/**
* The exe is located where RELEASES is.
*/
if (releasesPath.endsWith("RELEASES")) {
/**
* We first parse the RELEASES file to get out the .nupkg file name
* saved there. If setupExe wasn't changed in the squirrel maker
* config, then we can use this parsed name to get the .exe file
* name as well.
*
* If setupExe was set, then we need to use that instead to get
* the correct .exe path for signing.
*/
const nupkgFileName = fs
.readFileSync(releasesPath, "utf8")
.split(" ")[1]
const nupkgFilePath = releasesPath.replace("RELEASES", nupkgFileName)
const exeName = squirrelMaker.config?.setupExe
? squirrelMaker.config?.setupExe
: nupkgFileName.replace("-full.nupkg", ".exe")
const exeFilePath = releasesPath.replace("RELEASES", exeName)
/**
* The CodeSignTool calls other subfolders which means it needs
* the absolute path in case you're calling it from anywhere
* else other then it's root folder. This is their code:
*
* set code_sign_tool_path=%CODE_SIGN_TOOL_PATH%
*
* if defined code_sign_tool_path (
* %code_sign_tool_path%\jdk-11.0.2\bin\java -jar %code_sign_tool_path%\jar\code_sign_tool-1.3.0.jar %*
* ) else (
* .\jdk-11.0.2\bin\java -jar .\jar\code_sign_tool-1.3.0.jar %*
* )
*/
const { dir: codeSignToolFolder, name } = path.parse(signToolPath)
const codeSignToolFile = path.join(codeSignToolFolder, name)
const execSyncSettings = {
stdio: "inherit",
encoding: "utf8",
env: {
...process.env,
CODE_SIGN_TOOL_PATH: codeSignToolFolder,
},
} as ExecSyncOptionsWithStringEncoding
/**
* We sign both the .exe and .nupkg files.
*/
try {
execSync(
`${codeSignToolFile} sign -input_file_path="${exeFilePath}" -override="true" -credential_id="${credentialId}" -username="${userName}" -password="${password}" ${
userTotp ? `-totp_secret="${userTotp}"` : ""
}`,
execSyncSettings,
)
/**
* To sign the .nupkg files we also need to make sure the CodeSignTool
* has the following config property set - TSA_LEGACY_URL=http://ts.ssl.com/legacy
* as per https://www.ssl.com/how-to/code-signing-nuget-packages-with-esigner-codesigntool/.
*/
const pathtoCodeSignToolConf = path.join(
codeSignToolFolder,
"conf",
"code_sign_tool.properties",
)
const codeSignToolConfig = fs.readFileSync(
pathtoCodeSignToolConf,
"utf8",
)
if (
!codeSignToolConfig.includes(
"TSA_LEGACY_URL=http://ts.ssl.com/legacy",
)
) {
fs.appendFileSync(
pathtoCodeSignToolConf,
`\nTSA_LEGACY_URL=http://ts.ssl.com/legacy`,
)
}
execSync(
`${codeSignToolFile} sign -input_file_path="${nupkgFilePath}" -override="true" -credential_id="${credentialId}" -username="${userName}" -password="${password}" ${
userTotp ? `-totp_secret="${userTotp}"` : ""
}`,
execSyncSettings,
)
} catch (e) {
if (e instanceof Error) {
throw new Error(e.message)
} else {
throw e
}
}
}
return data
})
}
}
module.exports = ElectronForgeSslCodeSignPlugin