diff --git a/electron-builder.yml b/electron-builder.yml index 41e05c7d2..28105ce66 100644 --- a/electron-builder.yml +++ b/electron-builder.yml @@ -26,7 +26,7 @@ asarUnpack: - './node_modules/keytar/**/*' - './node_modules/msgpackr-extract/**/*' - './node_modules/sodium-native/**/*' -afterPack: './scripts/electron-builder/hooks/afterPack/index.cjs' +afterPack: './scripts/electron-builder/hook-after-pack.cjs' mac: icon: ./app/assets/icons/mac/icon.icns diff --git a/package.json b/package.json index 6694f4038..c0f124ef4 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "build:web:browser-window:dev": "cross-env NODE_ENV=development npm-run-all build:web:browser-window", "build:web:search": "pnpm run webpack:shortcut -- --config ./webpack-configs/web/search-in-page-browser-view.ts", "build:web:search:dev": "cross-env NODE_ENV=development npm-run-all build:web:search", - "build:electron-builder-hooks:celanup": "rimraf ./scripts/electron-builder/hooks/afterPack/index.js", + "build:electron-builder-hooks:celanup": "rimraf ./scripts/electron-builder/*.cjs", "build:electron-builder-hooks:build": "pnpm run webpack:shortcut -- --config ./webpack-configs/electron-builder-hooks.ts", "build:electron-builder-hooks": "npm-run-all build:electron-builder-hooks:celanup build:electron-builder-hooks:build", "prepare-native-deps": "npm-run-all clean:prebuilds prepare:remove:prebuild-install scripts/prepare-native-deps", diff --git a/scripts/electron-builder/hook-after-pack.ts b/scripts/electron-builder/hook-after-pack.ts new file mode 100644 index 000000000..7ca5f2abd --- /dev/null +++ b/scripts/electron-builder/hook-after-pack.ts @@ -0,0 +1,46 @@ +import type {AfterPackContext, Configuration, Target} from "app-builder-lib"; +import fastGlob from "fast-glob"; +import path from "path"; + +import {CONSOLE_LOG, execShell} from "scripts/lib"; + +const hookName = "afterPack"; + +const printPrefix = `[hook: ${hookName}]`; + +async function applyChmodThing({appOutDir}: AfterPackContext, {name: targetName}: Target): Promise { + if (!["appimage", "snap"].includes(targetName.toLocaleLowerCase())) { + await execShell(["chmod", ["4755", path.join(appOutDir, "chrome-sandbox")]]); + } +} + +const resolveDistPrebuildsDirs = async (): Promise => { + return fastGlob("./dist/**/app.asar.unpacked/node_modules/**/prebuilds", {onlyDirectories: true}); +}; + +const hook: Required[typeof hookName] = async (ctx) => { + if (ctx.targets.length !== 1) throw new Error(`${printPrefix} only one target is allowed at a time`); + const [target] = ctx.targets; + if (!target) throw new Error("Target resolving failed"); + + CONSOLE_LOG(`${printPrefix} processing "${target.name}" target`); + + if (ctx.electronPlatformName.toLowerCase().startsWith("lin")) { + await applyChmodThing(ctx, target); + } + + { + let prebuildDirs = await resolveDistPrebuildsDirs(); + + for (const prebuildDir of await resolveDistPrebuildsDirs()) { + CONSOLE_LOG(`${printPrefix} removing "${prebuildDir}" directory`); + await execShell(["npx", ["rimraf", prebuildDir]]); + } + + if ((prebuildDirs = await resolveDistPrebuildsDirs()).length) { + throw new Error(`Failed to remove the following dirs: ${JSON.stringify(prebuildDirs, null, 2)}`); + } + } +}; + +export default hook; diff --git a/scripts/electron-builder/hooks/afterPack/index.ts b/scripts/electron-builder/hooks/afterPack/index.ts deleted file mode 100644 index 4438e2f39..000000000 --- a/scripts/electron-builder/hooks/afterPack/index.ts +++ /dev/null @@ -1,37 +0,0 @@ -import path from "path"; - -import {CONSOLE_LOG, execShell} from "scripts/lib"; - -const hookName = "afterPack"; - -const printPrefix = `[hook: ${hookName}]`; - -async function linux({targets, appOutDir}: import("app-builder-lib").AfterPackContext): Promise { - if (targets.length !== 1) { - throw new Error(`${printPrefix} Only one target is allowed at a time for Linux platform`); - } - - const [target] = targets; - - if (!target) { - throw new Error("Target resolving failed"); - } - - const {name: targetName} = target; - - if (!["appimage", "snap"].includes(targetName.toLocaleLowerCase())) { - await execShell(["chmod", ["4755", path.join(appOutDir, "chrome-sandbox")]]); - } -} - -const hook: Required[typeof hookName] = async (context) => { - const electronPlatformNameLoweredCase = context.electronPlatformName.toLowerCase(); - - CONSOLE_LOG(`${printPrefix} Processing ${JSON.stringify(context.targets.map(({name}) => name))} targets`); - - if (electronPlatformNameLoweredCase.startsWith("lin")) { - await linux(context); - } -}; - -export default hook; diff --git a/scripts/electron-builder/hooks/afterPack/tsconfig.json b/scripts/electron-builder/tsconfig.json similarity index 69% rename from scripts/electron-builder/hooks/afterPack/tsconfig.json rename to scripts/electron-builder/tsconfig.json index ecd6ce922..b8c4b6647 100644 --- a/scripts/electron-builder/hooks/afterPack/tsconfig.json +++ b/scripts/electron-builder/tsconfig.json @@ -1,5 +1,5 @@ { - "extends": "../../../../tsconfig.json", + "extends": "../../tsconfig.json", "compilerOptions": { "module": "CommonJS", "importHelpers": true, @@ -8,7 +8,7 @@ "outDir": "." }, "include": [ - "../../../../src/@types/**/*", + "../../src/@types/**/*", "./**/*" ] -} +} \ No newline at end of file diff --git a/webpack-configs/electron-builder-hooks.ts b/webpack-configs/electron-builder-hooks.ts index 50f2ba3c7..b290861b6 100644 --- a/webpack-configs/electron-builder-hooks.ts +++ b/webpack-configs/electron-builder-hooks.ts @@ -1,30 +1,33 @@ import {Configuration} from "webpack"; +import fastGlob from "fast-glob"; import nodeExternals from "webpack-node-externals"; import path from "path"; import {buildBaseConfig, rootRelativePath, typescriptLoaderRule} from "./lib"; +import {CONSOLE_LOG} from "scripts/lib"; +import {sanitizeFastGlobPattern} from "src/shared/util/sanitize"; -const hooksDir = (...value: string[]): string => { - return path.join(rootRelativePath("./scripts/electron-builder/hooks"), ...value); -}; - -// TODO scan folder automatically -const hooksToBuild = ["afterPack"] as const; +const hooksDir = rootRelativePath("./scripts/electron-builder"); +const tsConfigFile = path.join(hooksDir, "tsconfig.json"); -const configurations: Configuration[] = hooksToBuild.map((hookDirName) => { - const hookDir = path.join(hooksDir(hookDirName)); - const tsConfigFile = path.join(hookDir, "tsconfig.json"); +export default async (): Promise => { + const hookSrcFiles = await fastGlob( + sanitizeFastGlobPattern(`${hooksDir}/hook-*.ts`), + {deep: 1, onlyFiles: true, stats: false}, + ); - return buildBaseConfig({ - mode: "none", - devtool: false, - target: "node", - entry: {index: hookDir}, - output: {path: hookDir, libraryTarget: "commonjs2", libraryExport: "default", filename: "[name].cjs"}, - module: {rules: [typescriptLoaderRule({tsConfigFile})]}, - externals: [nodeExternals()], - resolve: {alias: {scripts: rootRelativePath("scripts")}}, - }, {tsConfigFile}); -}); + CONSOLE_LOG(`Delected hook src files: ${JSON.stringify(hookSrcFiles, null, 2)}`); -export default configurations; + return hookSrcFiles.map((hookSrcFile) => { + return buildBaseConfig({ + mode: "none", + devtool: false, + target: "node", + entry: {[path.basename(hookSrcFile, ".ts")]: hookSrcFile}, + output: {path: hooksDir, libraryTarget: "commonjs2", libraryExport: "default", filename: "[name].cjs"}, + module: {rules: [typescriptLoaderRule({tsConfigFile})]}, + externals: [nodeExternals()], + resolve: {alias: {scripts: rootRelativePath("scripts")}}, + }, {tsConfigFile}); + }); +};