Skip to content

Commit

Permalink
feat: add new afterFinalizePackageTargets hook (#1437)
Browse files Browse the repository at this point in the history
* feat: add new afterFinalizeTargetMatrix hook

* chore: rename to target calculation hook

* chore: update NEWS
  • Loading branch information
MarshallOfSound committed Oct 31, 2022
1 parent 5aac8c9 commit fff1487
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Changed

* Replaced `electron-notarize` with `@electron/notarize`. The configuration options are unchanged. This migration is purely cosmetic.
* Added new `afterFinalizePackageTargets` hook. This hook exposes the platform/arch combinations that are being packaged when the `arch:all` or `platform:all` options are set.

## [17.0.0]

Expand Down
11 changes: 11 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ declare namespace electronPackager {
callback: (err?: Error | null) => void
) => void;

type TargetDefinition = {
arch: TargetArch;
platform: TargetPlatform;
}
type FinalizePackageTargetsHookFunction = (targets: TargetDefinition[], callback: (err?: Error | null) => void) => void;

/** See the documentation for [`@electron/osx-sign`](https://npm.im/@electron/osx-sign#opts) for details. */
type OsxSignOptions = Omit<SignOptions, 'app' | 'binaries' | 'platform' | 'version'>;

Expand Down Expand Up @@ -204,6 +210,11 @@ declare namespace electronPackager {
afterCopyExtraResources?: HookFunction[];
/** Functions to be called after the prebuilt Electron binary has been extracted to a temporary directory. */
afterExtract?: HookFunction[];
/**
* Functions to be called after the final matrix of platform/arch combination is determined. Use this to
* learn what archs/platforms packager is targetting when you pass "all" as a value.
*/
afterFinalizePackageTargets?: FinalizePackageTargetsHookFunction[];
/**
* Functions to be called after Node module pruning has been applied to the application.
*
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ module.exports = async function packager (opts) {

copyFilter.populateIgnoredPaths(opts)

await hooks.promisifyHooks(opts.afterFinalizePackageTargets, [targets.createPlatformArchPairs(opts, platforms, archs).map(([platform, arch]) => ({ platform, arch }))])
const appPaths = await packageAllSpecifiedCombos(opts, archs, platforms)
// Remove falsy entries (e.g. skipped platforms)
return appPaths.filter(appPath => appPath)
Expand Down
31 changes: 22 additions & 9 deletions test/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@ const packager = require('..')
const test = require('ava')
const util = require('./_util')

async function hookTest (wantHookCalled, hookName, t, opts) {
async function hookTest (wantHookCalled, hookName, t, opts, validator) {
let hookCalled = false
opts.dir = util.fixtureSubdir('basic')
opts.electronVersion = config.version
opts.arch = 'ia32'
opts.platform = 'all'

opts[hookName] = [(buildPath, electronVersion, platform, arch, callback) => {
hookCalled = true
t.is(electronVersion, opts.electronVersion, `${hookName} electronVersion should be the same as the options object`)
t.is(arch, opts.arch, `${hookName} arch should be the same as the options object`)
callback()
}]
opts[hookName] = [validator
? (...args) => {
hookCalled = true
validator(t, ...args)
}
: (buildPath, electronVersion, platform, arch, callback) => {
hookCalled = true
t.is(electronVersion, opts.electronVersion, `${hookName} electronVersion should be the same as the options object`)
t.is(arch, opts.arch, `${hookName} arch should be the same as the options object`)
callback()
}]

// 2 packages will be built during this test
const finalPaths = await packager(opts)
Expand All @@ -28,12 +33,20 @@ async function hookTest (wantHookCalled, hookName, t, opts) {
t.deepEqual(exists, [true, true], 'Packages should be generated for both 32-bit platforms')
}

function createHookTest (hookName) {
return util.packagerTest(async (t, opts) => hookTest(true, hookName, t, opts))
function createHookTest (hookName, validator) {
return util.packagerTest(async (t, opts) => hookTest(true, hookName, t, opts, validator))
}

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 afterFinalizePackageTargets hook', createHookTest('afterFinalizePackageTargets', (t, targets, callback) => {
t.is(targets.length, 2, 'target list should have two items')
t.is(targets[0].arch, 'ia32')
t.is(targets[0].platform, 'linux')
t.is(targets[1].arch, 'ia32')
t.is(targets[1].platform, 'win32')
callback()
}))
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'))
Expand Down
4 changes: 4 additions & 0 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ function completeFunction(
callbackFn();
}

// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
function finalizePackageTargetsFunction(targets: { arch: string; platform: string }[]): void {}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function ignoreFunction(path: string): boolean {
return true;
Expand Down Expand Up @@ -90,6 +93,7 @@ await packager({
afterCopy: [completeFunction],
afterCopyExtraResources: [completeFunction],
afterExtract: [completeFunction],
afterFinalizePackageTargets: [finalizePackageTargetsFunction],
afterPrune: [completeFunction],
beforeAsar: [completeFunction],
beforeCopy: [completeFunction],
Expand Down

0 comments on commit fff1487

Please sign in to comment.