Skip to content

Commit 5aac8c9

Browse files
erikianVerteDinde
andauthored
feat: New lifecycle hooks: afterAsar, afterComplete, afterCopyExtraResources, beforeAsar, beforeCopy, beforeCopyExtraResources (#1297)
* (feat) implements the following lifecycle hooks: afterAsar, afterComplete, afterCopyExtraResources, beforeAsar, beforeCopy, beforeCopyExtraResources * fix: minor style issues fixed * test: new hooks added to test/hooks.js * fix: restored accidentally removed test for afterExtract hook in test/hooks.js * fix: removed failing tests for conditional hooks * chore: clean up TODO, add tests for new hooks Co-authored-by: Erik Moura <[email protected]> Co-authored-by: Keeley Hammond <[email protected]>
1 parent df3383c commit 5aac8c9

File tree

4 files changed

+82
-10
lines changed

4 files changed

+82
-10
lines changed

src/index.d.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ declare namespace electronPackager {
106106
/**
107107
* @param buildPath - For [[afterExtract]], the path to the temporary folder where the prebuilt
108108
* Electron binary has been extracted to. For [[afterCopy]] and [[afterPrune]], the path to the
109-
* folder where the Electron app has been copied to.
109+
* folder where the Electron app has been copied to. For [[afterComplete]], the final directory
110+
* of the packaged application.
110111
* @param electronVersion - the version of Electron that is being bundled with the application.
111112
* @param platform - The target platform you are packaging for.
112113
* @param arch - The target architecture you are packaging for.
@@ -183,12 +184,24 @@ declare namespace electronPackager {
183184
interface Options {
184185
/** The source directory. */
185186
dir: string;
187+
/**
188+
* Functions to be called after your app directory has been packaged into an .asar file.
189+
*
190+
* **Note**: `afterAsar` will only be called if the [[asar]] option is set.
191+
*/
192+
afterAsar?: HookFunction[];
193+
/** Functions to be called after the packaged application has been moved to the final directory. */
194+
afterComplete?: HookFunction[];
186195
/**
187196
* Functions to be called after your app directory has been copied to a temporary directory.
188197
*
189198
* **Note**: `afterCopy` will not be called if the [[prebuiltAsar]] option is set.
190199
*/
191200
afterCopy?: HookFunction[];
201+
/**
202+
* Functions to be called after the files specified in the [[extraResource]] option have been copied.
203+
**/
204+
afterCopyExtraResources?: HookFunction[];
192205
/** Functions to be called after the prebuilt Electron binary has been extracted to a temporary directory. */
193206
afterExtract?: HookFunction[];
194207
/**
@@ -280,6 +293,22 @@ declare namespace electronPackager {
280293
* **Note:** `asar` will have no effect if the [[prebuiltAsar]] option is set.
281294
*/
282295
asar?: boolean | AsarOptions;
296+
/**
297+
* Functions to be called before your app directory is packaged into an .asar file.
298+
*
299+
* **Note**: `beforeAsar` will only be called if the [[asar]] option is set.
300+
*/
301+
beforeAsar?: HookFunction[];
302+
/**
303+
* Functions to be called before your app directory is copied to a temporary directory.
304+
*
305+
* **Note**: `beforeCopy` will not be called if the [[prebuiltAsar]] option is set.
306+
*/
307+
beforeCopy?: HookFunction[];
308+
/**
309+
* Functions to be called before the files specified in the [[extraResource]] option are copied.
310+
**/
311+
beforeCopyExtraResources?: HookFunction[];
283312
/**
284313
* The build version of the application. Defaults to the value of the [[appVersion]] option.
285314
* Maps to the `FileVersion` metadata property on Windows, and `CFBundleVersion` on macOS.

src/platform.js

+44-9
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ class App {
7373
return path.join(this.originalResourcesDir, 'app.asar')
7474
}
7575

76+
get commonHookArgs () {
77+
return [
78+
this.opts.electronVersion,
79+
this.opts.platform,
80+
this.opts.arch
81+
]
82+
}
83+
84+
get hookArgsWithOriginalResourcesAppDir () {
85+
return [
86+
this.originalResourcesAppDir,
87+
...this.commonHookArgs
88+
]
89+
}
90+
7691
async relativeRename (basePath, oldName, newName) {
7792
debug(`Renaming ${oldName} to ${newName} in ${basePath}`)
7893
await fs.rename(path.join(basePath, oldName), path.join(basePath, newName))
@@ -107,6 +122,8 @@ class App {
107122
} else {
108123
await this.buildApp()
109124
}
125+
126+
await hooks.promisifyHooks(this.opts.afterInitialize, this.hookArgsWithOriginalResourcesAppDir)
110127
}
111128

112129
async buildApp () {
@@ -116,20 +133,15 @@ class App {
116133
}
117134

118135
async copyTemplate () {
119-
const hookArgs = [
120-
this.originalResourcesAppDir,
121-
this.opts.electronVersion,
122-
this.opts.platform,
123-
this.opts.arch
124-
]
136+
await hooks.promisifyHooks(this.opts.beforeCopy, this.hookArgsWithOriginalResourcesAppDir)
125137

126138
await fs.copy(this.opts.dir, this.originalResourcesAppDir, {
127139
filter: copyFilter.userPathFilter(this.opts),
128140
dereference: this.opts.derefSymlinks
129141
})
130-
await hooks.promisifyHooks(this.opts.afterCopy, hookArgs)
142+
await hooks.promisifyHooks(this.opts.afterCopy, this.hookArgsWithOriginalResourcesAppDir)
131143
if (this.opts.prune) {
132-
await hooks.promisifyHooks(this.opts.afterPrune, hookArgs)
144+
await hooks.promisifyHooks(this.opts.afterPrune, this.hookArgsWithOriginalResourcesAppDir)
133145
}
134146
}
135147

@@ -171,7 +183,7 @@ class App {
171183
common.warning('prebuiltAsar has been specified, all asar options will be ignored')
172184
}
173185

174-
for (const hookName of ['afterCopy', 'afterPrune']) {
186+
for (const hookName of ['beforeCopy', 'afterCopy', 'afterPrune']) {
175187
if (this.opts[hookName]) {
176188
throw new Error(`${hookName} is incompatible with prebuiltAsar`)
177189
}
@@ -202,6 +214,9 @@ class App {
202214
}
203215

204216
debug(`Running asar with the options ${JSON.stringify(this.asarOptions)}`)
217+
218+
await hooks.promisifyHooks(this.opts.beforeAsar, this.hookArgsWithOriginalResourcesAppDir)
219+
205220
await asar.createPackageWithOptions(this.originalResourcesAppDir, this.appAsarPath, this.asarOptions)
206221
const { headerString } = asar.getRawHeader(this.appAsarPath)
207222
this.asarIntegrity = {
@@ -211,16 +226,27 @@ class App {
211226
}
212227
}
213228
await fs.remove(this.originalResourcesAppDir)
229+
230+
await hooks.promisifyHooks(this.opts.afterAsar, this.hookArgsWithOriginalResourcesAppDir)
214231
}
215232

216233
async copyExtraResources () {
217234
if (!this.opts.extraResource) return Promise.resolve()
218235

219236
const extraResources = common.ensureArray(this.opts.extraResource)
220237

238+
const hookArgs = [
239+
this.stagingPath,
240+
...this.commonHookArgs
241+
]
242+
243+
await hooks.promisifyHooks(this.opts.beforeCopyExtraResources, hookArgs)
244+
221245
await Promise.all(extraResources.map(
222246
resource => fs.copy(resource, path.resolve(this.stagingPath, this.resourcesDir, path.basename(resource)))
223247
))
248+
249+
await hooks.promisifyHooks(this.opts.afterCopyExtraResources, hookArgs)
224250
}
225251

226252
async move () {
@@ -231,6 +257,15 @@ class App {
231257
await fs.move(this.stagingPath, finalPath)
232258
}
233259

260+
if (this.opts.afterComplete) {
261+
const hookArgs = [
262+
finalPath,
263+
...this.commonHookArgs
264+
]
265+
266+
await hooks.promisifyHooks(this.opts.afterComplete, hookArgs)
267+
}
268+
234269
return finalPath
235270
}
236271
}

test/hooks.js

+2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ function createHookTest (hookName) {
3232
return util.packagerTest(async (t, opts) => hookTest(true, hookName, t, opts))
3333
}
3434

35+
test.serial('platform=all (one arch) for beforeCopy hook', createHookTest('beforeCopy'))
3536
test.serial('platform=all (one arch) for afterCopy hook', createHookTest('afterCopy'))
3637
test.serial('platform=all (one arch) for afterPrune hook', createHookTest('afterPrune'))
3738
test.serial('platform=all (one arch) for afterExtract hook', createHookTest('afterExtract'))
39+
test.serial('platform=all (one arch) for afterComplete hook', createHookTest('afterComplete'))
3840

3941
test('promisifyHooks executes functions in parallel', async t => {
4042
let output = '0'

test/index.test-d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,15 @@ await packager({
8585

8686
await packager({
8787
dir: '.',
88+
afterAsar: [completeFunction],
89+
afterComplete: [completeFunction],
8890
afterCopy: [completeFunction],
91+
afterCopyExtraResources: [completeFunction],
8992
afterExtract: [completeFunction],
9093
afterPrune: [completeFunction],
94+
beforeAsar: [completeFunction],
95+
beforeCopy: [completeFunction],
96+
beforeCopyExtraResources: [completeFunction],
9197
appCopyright: 'Copyright',
9298
appVersion: '1.0',
9399
arch: 'ia32',

0 commit comments

Comments
 (0)