Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: signWithHook supports one-time invocation. #30

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/sign-with-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,19 @@ function getHookFunction(options: InternalHookOptions): HookFunction {
export async function signWithHook(options: InternalSignOptions) {
hookFunction = getHookFunction(options);

for (const file of options.files) {
if (options.noIterateFiles) {
try {
await hookFunction(file);
await hookFunction(options);
} catch (error) {
log(`Error signing ${file}`, error);
log('Sign with hook failed.', error);
}
} else {
for (const file of options.files) {
try {
await hookFunction(file);
} catch (error) {
log(`Error signing ${file}`, error);
}
}
}
}
31 changes: 23 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,24 @@ export interface OptionalSignToolOptions {
signJavaScript?: boolean;
}

/**
* Custom function that is called sequentially for each file that needs to be signed.
*
* @param fileToSign Absolute path to the file to sign
*
* @category Utility
*/
export type HookFunction = (fileToSign: string) => void | Promise<void>;
export type HookFunction = {
/**
* Custom function that is called sequentially for each file that needs to be signed.
*
* @param fileToSign Absolute path to the file to sign
*
* @category Utility
*/
(fileToSign: string) : void | Promise<void>
/**
* Custom function that is called once,passed origin sign options.
*
* @param options Origin sign options with filtered files.
*
* @category Utility
*/
(options: InternalSignOptions) : void | Promise<void>
}

/**
* @category Utility
Expand All @@ -140,6 +150,11 @@ export interface OptionalHookOptions {
* `@electron/windows-sign` will not attempt to sign with SignTool if a custom hook is detected.
*/
hookModulePath?: string;
/**
* Do not iterate files which need to be signed. If true,
* it will call hookFunction one-time with origin sign options.
*/
noIterateFiles?: boolean;
}

export interface InternalHookOptions extends OptionalHookOptions {
Expand Down
23 changes: 18 additions & 5 deletions test/sign-with-hook.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ describe('sign with hook', async () => {
it('should call a hook function', async () => {
let hookCalled = false;

const hookFunction = (filePath: string) => {
const hookFunction = (filePath) => {
assert.equal(filePath, 'my/fake/file');
hookCalled = true;
};

await signWithHook({
appDirectory: '',
files: ['my/fake/file'],
hookFunction
});
Expand All @@ -24,7 +23,7 @@ describe('sign with hook', async () => {
it('should call a async hook function', async () => {
let hookCalled = false;

const hookFunction = async (filePath: string) => {
const hookFunction = async (filePath) => {
assert.equal(filePath, 'my/fake/file');

return new Promise<void>((resolve) => {
Expand All @@ -36,7 +35,6 @@ describe('sign with hook', async () => {
};

await signWithHook({
appDirectory: '',
files: ['my/fake/file'],
hookFunction
});
Expand All @@ -47,11 +45,26 @@ describe('sign with hook', async () => {
it('should call a hook module', async () => {
const fakeFile = `my/fake/file/${Date.now()}`;
await signWithHook({
appDirectory: '',
files: [fakeFile],
hookModulePath: './test/fixtures/hook-module.js'
});

assert.strictEqual(process.env.HOOK_MODULE_CALLED_WITH_FILE, fakeFile);
});

it('should call a hook function once when noIterateFiles is true', async () => {
const functionCalled: string[] = [];

const hookFunction = (options) => {
functionCalled.push(options.files);
};

await signWithHook({
files: ['my/fake/file1', 'my/fake/file2'],
hookFunction,
noIterateFiles: true
});

assert.deepStrictEqual(functionCalled, [['my/fake/file1', 'my/fake/file2']]);
});
});