Skip to content

Commit

Permalink
feat: New lifecycle hooks: afterAsar, afterComplete, afterCopyExtraRe…
Browse files Browse the repository at this point in the history
…sources, 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 <erik@nullbug.dev>
Co-authored-by: Keeley Hammond <khammond@slack-corp.com>
  • Loading branch information
3 people authored Oct 25, 2022
1 parent df3383c commit 5aac8c9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 10 deletions.
31 changes: 30 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ declare namespace electronPackager {
/**
* @param buildPath - For [[afterExtract]], the path to the temporary folder where the prebuilt
* Electron binary has been extracted to. For [[afterCopy]] and [[afterPrune]], the path to the
* folder where the Electron app has been copied to.
* folder where the Electron app has been copied to. For [[afterComplete]], the final directory
* of the packaged application.
* @param electronVersion - the version of Electron that is being bundled with the application.
* @param platform - The target platform you are packaging for.
* @param arch - The target architecture you are packaging for.
Expand Down Expand Up @@ -183,12 +184,24 @@ declare namespace electronPackager {
interface Options {
/** The source directory. */
dir: string;
/**
* Functions to be called after your app directory has been packaged into an .asar file.
*
* **Note**: `afterAsar` will only be called if the [[asar]] option is set.
*/
afterAsar?: HookFunction[];
/** Functions to be called after the packaged application has been moved to the final directory. */
afterComplete?: HookFunction[];
/**
* Functions to be called after your app directory has been copied to a temporary directory.
*
* **Note**: `afterCopy` will not be called if the [[prebuiltAsar]] option is set.
*/
afterCopy?: HookFunction[];
/**
* Functions to be called after the files specified in the [[extraResource]] option have been copied.
**/
afterCopyExtraResources?: HookFunction[];
/** Functions to be called after the prebuilt Electron binary has been extracted to a temporary directory. */
afterExtract?: HookFunction[];
/**
Expand Down Expand Up @@ -280,6 +293,22 @@ declare namespace electronPackager {
* **Note:** `asar` will have no effect if the [[prebuiltAsar]] option is set.
*/
asar?: boolean | AsarOptions;
/**
* Functions to be called before your app directory is packaged into an .asar file.
*
* **Note**: `beforeAsar` will only be called if the [[asar]] option is set.
*/
beforeAsar?: HookFunction[];
/**
* Functions to be called before your app directory is copied to a temporary directory.
*
* **Note**: `beforeCopy` will not be called if the [[prebuiltAsar]] option is set.
*/
beforeCopy?: HookFunction[];
/**
* Functions to be called before the files specified in the [[extraResource]] option are copied.
**/
beforeCopyExtraResources?: HookFunction[];
/**
* The build version of the application. Defaults to the value of the [[appVersion]] option.
* Maps to the `FileVersion` metadata property on Windows, and `CFBundleVersion` on macOS.
Expand Down
53 changes: 44 additions & 9 deletions src/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ class App {
return path.join(this.originalResourcesDir, 'app.asar')
}

get commonHookArgs () {
return [
this.opts.electronVersion,
this.opts.platform,
this.opts.arch
]
}

get hookArgsWithOriginalResourcesAppDir () {
return [
this.originalResourcesAppDir,
...this.commonHookArgs
]
}

async relativeRename (basePath, oldName, newName) {
debug(`Renaming ${oldName} to ${newName} in ${basePath}`)
await fs.rename(path.join(basePath, oldName), path.join(basePath, newName))
Expand Down Expand Up @@ -107,6 +122,8 @@ class App {
} else {
await this.buildApp()
}

await hooks.promisifyHooks(this.opts.afterInitialize, this.hookArgsWithOriginalResourcesAppDir)
}

async buildApp () {
Expand All @@ -116,20 +133,15 @@ class App {
}

async copyTemplate () {
const hookArgs = [
this.originalResourcesAppDir,
this.opts.electronVersion,
this.opts.platform,
this.opts.arch
]
await hooks.promisifyHooks(this.opts.beforeCopy, this.hookArgsWithOriginalResourcesAppDir)

await fs.copy(this.opts.dir, this.originalResourcesAppDir, {
filter: copyFilter.userPathFilter(this.opts),
dereference: this.opts.derefSymlinks
})
await hooks.promisifyHooks(this.opts.afterCopy, hookArgs)
await hooks.promisifyHooks(this.opts.afterCopy, this.hookArgsWithOriginalResourcesAppDir)
if (this.opts.prune) {
await hooks.promisifyHooks(this.opts.afterPrune, hookArgs)
await hooks.promisifyHooks(this.opts.afterPrune, this.hookArgsWithOriginalResourcesAppDir)
}
}

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

for (const hookName of ['afterCopy', 'afterPrune']) {
for (const hookName of ['beforeCopy', 'afterCopy', 'afterPrune']) {
if (this.opts[hookName]) {
throw new Error(`${hookName} is incompatible with prebuiltAsar`)
}
Expand Down Expand Up @@ -202,6 +214,9 @@ class App {
}

debug(`Running asar with the options ${JSON.stringify(this.asarOptions)}`)

await hooks.promisifyHooks(this.opts.beforeAsar, this.hookArgsWithOriginalResourcesAppDir)

await asar.createPackageWithOptions(this.originalResourcesAppDir, this.appAsarPath, this.asarOptions)
const { headerString } = asar.getRawHeader(this.appAsarPath)
this.asarIntegrity = {
Expand All @@ -211,16 +226,27 @@ class App {
}
}
await fs.remove(this.originalResourcesAppDir)

await hooks.promisifyHooks(this.opts.afterAsar, this.hookArgsWithOriginalResourcesAppDir)
}

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

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

const hookArgs = [
this.stagingPath,
...this.commonHookArgs
]

await hooks.promisifyHooks(this.opts.beforeCopyExtraResources, hookArgs)

await Promise.all(extraResources.map(
resource => fs.copy(resource, path.resolve(this.stagingPath, this.resourcesDir, path.basename(resource)))
))

await hooks.promisifyHooks(this.opts.afterCopyExtraResources, hookArgs)
}

async move () {
Expand All @@ -231,6 +257,15 @@ class App {
await fs.move(this.stagingPath, finalPath)
}

if (this.opts.afterComplete) {
const hookArgs = [
finalPath,
...this.commonHookArgs
]

await hooks.promisifyHooks(this.opts.afterComplete, hookArgs)
}

return finalPath
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ function createHookTest (hookName) {
return util.packagerTest(async (t, opts) => hookTest(true, hookName, t, opts))
}

test.serial('platform=all (one arch) for beforeCopy hook', createHookTest('beforeCopy'))
test.serial('platform=all (one arch) for afterCopy hook', createHookTest('afterCopy'))
test.serial('platform=all (one arch) for afterPrune hook', createHookTest('afterPrune'))
test.serial('platform=all (one arch) for afterExtract hook', createHookTest('afterExtract'))
test.serial('platform=all (one arch) for afterComplete hook', createHookTest('afterComplete'))

test('promisifyHooks executes functions in parallel', async t => {
let output = '0'
Expand Down
6 changes: 6 additions & 0 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,15 @@ await packager({

await packager({
dir: '.',
afterAsar: [completeFunction],
afterComplete: [completeFunction],
afterCopy: [completeFunction],
afterCopyExtraResources: [completeFunction],
afterExtract: [completeFunction],
afterPrune: [completeFunction],
beforeAsar: [completeFunction],
beforeCopy: [completeFunction],
beforeCopyExtraResources: [completeFunction],
appCopyright: 'Copyright',
appVersion: '1.0',
arch: 'ia32',
Expand Down

0 comments on commit 5aac8c9

Please sign in to comment.