Skip to content

Commit

Permalink
enable extra measure for removing "prebuilds"
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimiry committed Dec 9, 2024
1 parent dce28d7 commit 79cca6c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 63 deletions.
2 changes: 1 addition & 1 deletion electron-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
46 changes: 46 additions & 0 deletions scripts/electron-builder/hook-after-pack.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
if (!["appimage", "snap"].includes(targetName.toLocaleLowerCase())) {
await execShell(["chmod", ["4755", path.join(appOutDir, "chrome-sandbox")]]);
}
}

const resolveDistPrebuildsDirs = async (): Promise<string[]> => {
return fastGlob("./dist/**/app.asar.unpacked/node_modules/**/prebuilds", {onlyDirectories: true});
};

const hook: Required<Configuration>[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;
37 changes: 0 additions & 37 deletions scripts/electron-builder/hooks/afterPack/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../../../tsconfig.json",
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"importHelpers": true,
Expand All @@ -8,7 +8,7 @@
"outDir": "."
},
"include": [
"../../../../src/@types/**/*",
"../../src/@types/**/*",
"./**/*"
]
}
}
45 changes: 24 additions & 21 deletions webpack-configs/electron-builder-hooks.ts
Original file line number Diff line number Diff line change
@@ -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<Configuration[]> => {
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});
});
};

0 comments on commit 79cca6c

Please sign in to comment.