TidGi-Desktop/scripts/afterPack.ts
lin onetwo a931058f17
chore: upgrade Electron 39->41 + forge 7.10->7.11 + fix native ABI (#692)
* chore: upgrade Electron 39->41, forge 7.10->7.11, fix native ABI and preload naming

- electron: 39.2.3 -> 41.1.1
- @electron-forge/*: 7.10.2 -> 7.11.1
- better-sqlite3: 12.4.5 -> 12.8.0 (rebuild against Electron 41 ABI 145)
- electron-unhandled: 5.0.0 -> 4.0.1 (v5 uses top-level await, breaks CJS main build)
- vite.preload.config.ts: emit preload.js (not index.js) to avoid collision with main
- viteEntry.ts: getPreloadPath() -> preload.js, renderer path unchanged
- package.json main: .vite/build/main.js (matches forge lib output)
- package.json: strip UTF-8 BOM that broke Volta manifest parsing

* fix: resolve AbortSignal realm mismatch in Electron 41 for streaming tests

In Electron 41, ELECTRON_RUN_AS_NODE mode uses Chromium-based fetch which
requires Blink's AbortSignal. Node.js AbortSignal fails the instanceof check.

Fixes:
- vitest.config.ts: set features/** to node environment via environmentMatchGlobs
  so HTTP-only tests use Node.js fetch (not Chromium fetch)
- setup-vitest.ts: guard all document/window access behind typeof document check
  so setup is safe in both jsdom and node environments
- callProviderAPI.ts: no change needed (signal issue is env-specific)

* ci: increase test timeout to 60min, fix find -o with xargs rm

- timeout-minutes: 25 -> 60 (packaging ~22min + unit tests ~1min + e2e needed)
- fix 'find ... -o ... | xargs rm' -> 'find ... \( -o \) ... -exec rm -rf {} +'
  (the unparenthesized -o with xargs caused 'rm: invalid option -- o' in CI)

* fix: copy tiddlywiki/core-server to packaged output

TiddlyWiki 5.4.0-prerelease introduced core-server/ directory which contains
commander.js (module-type: global). Without it, \.Commander is undefined
when load-modules.js startup runs \.Commander.initCommands(), crashing the
wiki worker with 'Cannot read properties of undefined (reading initCommands)'.

core-server is loaded by boot.js via loadPluginFolder(\.boot.coreServerPath),
which silently returns null when the directory is missing, so the error was
non-obvious and only manifested when actually starting a wiki workspace.

* perf: only screenshot on failure, revert timeout to 25min

AfterStep was capturing a screenshot after every step (~1191 of 1446 steps).
Each screenshot requires IPC round-trip: getFirstWebContentsView  capturePage
 serialize PNG  transfer buffer  fs.writeFile. On CI this costs ~200-400ms
per step, totaling ~6 minutes of pure screenshot overhead.

Now screenshots are only taken for FAILED steps, saving ~6 minutes on CI.
This brings the total test time well within the 25-minute budget.

* fix: flaky tests + merge scenarios to save 8 Electron launches

- crossWindowSync: wait for tiddler to be saved to disk via watch-fs before opening second window
- tidgiMiniWindowWorkspace: add retry backoff to 'should not see elements' step
- tidgiMiniWindow: add explicit window switch after addTiddler
- Merge 5 filesystemPlugin scenarios into 1 (save 4 launches)
- Merge 2 defaultWiki scenarios into 1 (save 1 launch)
- Merge 2 preference scenarios into 1 (save 1 launch)
- Merge smoke + logging into 1 (save 1 launch)
- Merge 2 tiddler scenarios into 1 (save 1 launch)
- Total: 65  57 scenarios (8 fewer Electron launches)

* fix: address CI failures - use IPC for mini window toggle, split talkWithAI clicks

- tidgiMiniWindow: use direct IPC toggle instead of keyboard shortcut after addTiddler
  to avoid race condition between TW syncer and keyboard event dispatch
- talkWithAI: split multi-click step into individual clicks so each gets its own
  25s timeout budget on CI

* fix: await showWindow() in openTidgiMiniWindow to prevent race condition

The two showWindow() calls were using void (fire-and-forget), meaning
toggleTidgiMiniWindow returned before the window was actually shown.
On CI/Xvfb, the test checked isVisible() before show() completed.

* perf: merge scenarios + split CI steps for faster E2E

- Merge 3 defaultWiki scenarios into 1 (save 2 app launches)
- Merge 2 scheduledTask scenarios into 1 (save 1 app launch)
- Split CI test step into unit/prepare/e2e for visibility
- Add pnpm store cache to CI
- Fix viteEntry.ts: remove UTF-8 BOM, update stale JSDoc comment
- Set E2E timeout to 22min (was 25min in single step)

* fix: address Copilot review - main window lookup and comment clarity

- tidgiMiniWindow.ts: use index.html URL pattern to find main window
  (consistent with other step defs, avoids matching preferences window)
- ui.ts: clarify networkidle timeout comment

* fix: use IPC toggle for tidgiMiniWindow sync scenario to avoid flaky keyboard shortcut on CI

---------

Co-authored-by: CI Auto <cidevel@tiddlygit.local>
2026-04-02 21:45:47 +08:00

106 lines
4.3 KiB
TypeScript

/**
* Copy necessary dependencies after packaging
* Based on https://ganeshrvel.medium.com/electron-builder-afterpack-configuration-5c2c986be665
* Adapted for electron forge https://github.com/electron-userland/electron-forge/issues/2248
*/
import fs from 'fs-extra';
import path from 'path';
/**
* Running afterPack hook
* Note: This must be a non-async function that accepts a callback for Electron Packager compatibility
* @param buildPath /var/folders/qj/7j0zx32d0l75zmnrl1w3m3b80000gn/T/electron-packager/darwin-x64/TidGi-darwin-x64/Electron.app/Contents/Resources/app
* @param electronVersion 12.0.6
* @param platform darwin / win32 (even on win11 x64)
* @param arch x64
* @param callback Callback to signal completion
*/
export default (
buildPath: string,
_electronVersion: string,
platform: string,
_arch: string,
callback: () => void,
): void => {
const cwd = path.resolve(buildPath, '..');
const projectRoot = path.resolve(__dirname, '..');
console.log('Copy npm packages with node-worker dependencies with binary (dugite) or __filename usages (tiddlywiki), which cannot be prepared properly by webpack');
if (['production', 'test'].includes(process.env.NODE_ENV ?? '')) {
console.log('Copying tiddlywiki dependency to dist');
const sourceNodeModulesFolder = path.resolve(projectRoot, 'node_modules');
fs.cpSync(
path.join(sourceNodeModulesFolder, 'zx'),
path.join(cwd, 'node_modules', 'zx'),
{ dereference: true, recursive: true },
);
const packagePathsToCopyDereferenced: string[][] = [
['tiddlywiki', 'package.json'],
['tiddlywiki', 'boot'],
['tiddlywiki', 'core'],
// core-server: introduced in TiddlyWiki 5.4.0, contains Commander module ($tw.Commander) required by load-modules startup
['tiddlywiki', 'core-server'],
// only copy plugins that is used in src/services/wiki/wikiWorker/startNodeJSWiki.ts, other plugins can be installed via JSON from online plugin library
['tiddlywiki', 'plugins', 'linonetwo'],
['tiddlywiki', 'plugins', 'tiddlywiki', 'filesystem'],
['tiddlywiki', 'plugins', 'tiddlywiki', 'tiddlyweb'],
['tiddlywiki', 'tiddlywiki.js'],
// node binary
['better-sqlite3', 'build', 'Release', 'better_sqlite3.node'],
// nsfw native module
['nsfw', 'build', 'Release', 'nsfw.node'],
// Refer to `node_modules\sqlite-vec\index.cjs` for latest file names
// sqlite-vec: copy main entry files and platform-specific binary
['sqlite-vec', 'package.json'],
['sqlite-vec', 'index.cjs'],
[`sqlite-vec-${process.platform === 'win32' ? 'windows' : process.platform}-${process.arch}`],
];
// macOS only: copy app-path binary for finding apps
if (platform === 'darwin') {
packagePathsToCopyDereferenced.push(['app-path', 'main']);
}
console.log('Copying packagePathsToCopyDereferenced');
for (const packagePathInNodeModules of packagePathsToCopyDereferenced) {
// some binary may not exist in other platforms, so allow failing here.
try {
fs.copySync(
path.resolve(sourceNodeModulesFolder, ...packagePathInNodeModules),
path.resolve(cwd, 'node_modules', ...packagePathInNodeModules),
{ dereference: true },
);
} catch (error) {
// some binary may not exist in other platforms, so allow failing here.
const errorMessage = error instanceof Error ? error.message : String(error);
console.error(
`Error copying ${packagePathInNodeModules.join('/')} to dist, in afterPack.ts: ${errorMessage}`,
);
}
}
console.log('Copy dugite');
// it has things like `git/bin/libexec/git-core/git-add` link to `git/bin/libexec/git-core/git`, to reduce size, so can't use `dereference: true, recursive: true` here.
fs.copySync(
path.join(sourceNodeModulesFolder, 'dugite'),
path.join(cwd, 'node_modules', 'dugite'),
{ dereference: false },
);
if (platform === 'win32') {
console.log('Copy registry-js (Windows only)');
// registry-js has native binary that is loaded using relative path (../../build/Release/registry.node)
fs.copySync(
path.join(sourceNodeModulesFolder, 'registry-js'),
path.join(cwd, 'node_modules', 'registry-js'),
{ dereference: true },
);
}
}
/** complete this hook */
callback();
};