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

chore(suite-desktop-core): typescriptize electron builder hooks #17056

Merged
merged 1 commit into from
Feb 18, 2025
Merged
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
7 changes: 7 additions & 0 deletions packages/suite-desktop-core/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ export default [
{
ignores: ['**/playwright-report/', '**/test-results/'],
},
{
files: ['**/scripts/**'],
rules: {
'no-console': 'off',
'import/no-default-export': 'off',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

funnily enough, eslint tolerates CJS exports.default in a TS file, but errors at ES export default.

},
},
];
3 changes: 2 additions & 1 deletion packages/suite-desktop-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"homepage": "https://trezor.io/",
"main": "src/app.ts",
"scripts": {
"build:core": "yarn g:rimraf dist && TS_NODE_PROJECT=\"tsconfig.json\" yarn webpack --config ./webpack/core.webpack.config.ts",
"build:lib": "yarn g:rimraf ./lib && yarn g:tsc --build tsconfig.lib.json",
"build:core": "yarn build:lib && yarn g:rimraf dist && TS_NODE_PROJECT=\"tsconfig.json\" yarn webpack --config ./webpack/core.webpack.config.ts",
"type-check": "yarn g:tsc --build tsconfig.json",
"test:unit": "yarn g:jest",
"test:e2e": "yarn xvfb-maybe -- playwright test --config=./e2e/playwright.config.ts",
Expand Down
12 changes: 7 additions & 5 deletions packages/suite-desktop-core/scripts/notarize.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable no-console */
import { notarize } from '@electron/notarize';
import type { Hooks } from 'app-builder-lib';

const { notarize } = require('@electron/notarize');

exports.default = context => {
const notarizeAfterSignHook: Hooks['afterSign'] = context => {
Copy link
Contributor Author

@Lemonexe Lemonexe Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No type check ever happened on this "typescript" file.
TS would fail, because at the very least, TS requires (context: any) =>, but then it's no longer valid JS (electron-builder crashes)

const { electronPlatformName, appOutDir } = context;

if (electronPlatformName !== 'darwin') {
Expand All @@ -25,5 +24,8 @@ exports.default = context => {
appleId: process.env.APPLEID,
appleIdPassword: process.env.APPLEIDPASS,
teamId: process.env.APPLETEAMID,
});
// TODO fix: notarize tool is known to be working, but TS fails: no overload matches this call
} as any);
};

export default notarizeAfterSignHook;
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const { flipFuses, FuseV1Options, FuseVersion } = require('@electron/fuses');
const path = require('path');
import { FuseV1Options, FuseVersion, flipFuses } from '@electron/fuses';
import type { Hooks } from 'app-builder-lib';
import path from 'path';

// copied from https://github.com/electron-userland/electron-builder/blob/04be5699c664e6a93e093b820a16ad516355b5c7/packages/app-builder-lib/src/platformPackager.ts#L430-L434
const binaryExtensionByPlaformNameMap = {
darwin: '.app',
win32: '.exe',
linux: '',
};
} as const;

exports.default = async function afterPack(context) {
const afterPackHookSetElectronFuses: Hooks['afterPack'] = async context => {
const { electronPlatformName, appOutDir } = context;

/*
Expand Down Expand Up @@ -38,3 +39,5 @@ exports.default = async function afterPack(context) {

console.log('Successfully set electron fuses');
};

export default afterPackHookSetElectronFuses;
11 changes: 8 additions & 3 deletions packages/suite-desktop-core/scripts/sign-windows.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
/* eslint-disable */
exports.default = async function (configuration) {
import type { CustomWindowsSign } from 'app-builder-lib';

const signWindows: CustomWindowsSign = async configuration => {
// Check if IS_CODESIGN_BUILD is set and true
if (!process.env.IS_CODESIGN_BUILD || process.env.IS_CODESIGN_BUILD.toLowerCase() !== 'true') {
console.log('This is DEV build, not signing');

return;
}

// do not include passwords or other sensitive data in the file
// rather create environment variables with sensitive data
const CERTIFICATE_NAME = process.env.WINDOWS_SIGN_CERTIFICATE_NAME;
const TOKEN_PASSWORD = process.env.WINDOWS_SIGN_TOKEN_PASSWORD;

require('child_process').execSync(
await require('child_process').exec(
`java -jar ../suite-desktop-core/scripts/jsign-6.0.jar --keystore ../suite-desktop-core/scripts/hardwareToken.cfg --storepass '${TOKEN_PASSWORD}' --storetype PKCS11 --tsaurl http://timestamp.digicert.com --alias "${CERTIFICATE_NAME}" "${configuration.path}"`,
Copy link
Contributor Author

@Lemonexe Lemonexe Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fn was async (and has to stay, because TS requires the hook to return Promise), but without await, which failed eslint, hence eslint disable 🙈

👉 needs to be verified if changing execSync to exec doesn't break the signing with codesign build.
But should be ok. This is the code that invokes it. It awaits the Promise, so LGTM..

{
stdio: 'inherit',
},
);
};

export default signWindows;
7 changes: 6 additions & 1 deletion packages/suite-desktop-core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
"moduleResolution": "node",
"outDir": "libDev"
},
"include": ["src", "e2e", "**/*.json"],
"include": [
"src",
"e2e",
"scripts",
Copy link
Contributor Author

@Lemonexe Lemonexe Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This includes the scripts to type-check, which was not done before, but does not build them, that's what build:lib does (tsconfig.lib.json)

"**/*.json"
],
"references": [
{
"path": "../../suite-common/message-system"
Expand Down
12 changes: 12 additions & 0 deletions packages/suite-desktop-core/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.lib.json",
"compilerOptions": {
"outDir": "lib"
},
"include": ["./scripts"],
"references": [
{
"path": "../eslint"
}
]
}
6 changes: 3 additions & 3 deletions packages/suite-desktop/electron-builder-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ module.exports = {
target: ['nsis'],
signtoolOptions: {
publisherName: ['SatoshiLabs, s.r.o.', 'Trezor Company s.r.o.'],
sign: '../suite-desktop-core/scripts/sign-windows.ts',
sign: '../suite-desktop-core/lib/sign-windows.js',
},
},
linux: {
Expand Down Expand Up @@ -162,6 +162,6 @@ module.exports = {
category: 'Utility',
target: ['AppImage'],
},
afterPack: '../suite-desktop-core/scripts/setElectronFuses.js',
afterSign: '../suite-desktop-core/scripts/notarize.ts',
afterPack: '../suite-desktop-core/lib/setElectronFuses.js',
afterSign: '../suite-desktop-core/lib/notarize.js',
};
Loading