-
Notifications
You must be signed in to change notification settings - Fork 936
Expand file tree
/
Copy pathwait-for-electron-build.mjs
More file actions
122 lines (103 loc) · 3.59 KB
/
wait-for-electron-build.mjs
File metadata and controls
122 lines (103 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import fs from 'node:fs';
import http from 'node:http';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { rendererDevUrl } from './renderer-dev-config.mjs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const rootDir = path.resolve(__dirname, '..');
export const defaultRequiredFiles = [
path.join(rootDir, 'dist/main/main.cjs'),
path.join(rootDir, 'dist/preload/preload.cjs'),
];
export const defaultRendererUrl = rendererDevUrl;
export const defaultMaxWaitMs = 180000;
export const defaultPollIntervalMs = 500;
export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
export const readMtimeMs = (file) => {
try {
return fs.statSync(file).mtimeMs;
} catch (error) {
// Missing file is an expected signal ("not built yet"); anything else
// (permission denied, IO error, ...) should surface instead of being
// silently swallowed into a stale-build state.
if (error && error.code === 'ENOENT') return null;
throw new Error(
`wait-for-electron-build: failed to stat ${file}: ${
error instanceof Error ? error.message : String(error)
}`,
{ cause: error },
);
}
};
/**
* Build a "has this dev cycle produced a fresh build?" checker.
*
* The checker snapshots each required file's mtime at creation time, then
* on every call it returns true only when every file exists AND either was
* absent in the snapshot or now has a strictly newer mtime. This avoids
* treating stale dist artifacts from a previous `pnpm dev` run as "fresh".
*/
export const createFreshBuildChecker = (files, readMtime = readMtimeMs) => {
const initialMtimes = new Map(files.map((file) => [file, readMtime(file)]));
return () =>
files.every((file) => {
const current = readMtime(file);
if (current === null) {
return false;
}
const initial = initialMtimes.get(file);
return initial === null || current > initial;
});
};
export const checkRendererReady = (url) =>
new Promise((resolve) => {
const request = http.get(url, (response) => {
response.resume();
resolve(response.statusCode === 200);
});
request.on('error', () => resolve(false));
request.setTimeout(1000, () => {
request.destroy();
resolve(false);
});
});
/**
* Poll until the required build outputs are fresh AND the renderer dev
* server is serving a 200, or until `maxWaitMs` elapses. Dependencies are
* injectable so the loop can be unit-tested with a virtual clock.
*/
export const waitForBuild = async ({
requiredFiles = defaultRequiredFiles,
rendererUrl = defaultRendererUrl,
maxWaitMs = defaultMaxWaitMs,
pollIntervalMs = defaultPollIntervalMs,
readMtime = readMtimeMs,
isRendererReady = () => checkRendererReady(rendererUrl),
now = () => Date.now(),
delay = sleep,
} = {}) => {
const hasFreshBuild = createFreshBuildChecker(requiredFiles, readMtime);
const startedAt = now();
while (now() - startedAt < maxWaitMs) {
if (hasFreshBuild() && (await isRendererReady())) {
return true;
}
await delay(pollIntervalMs);
}
return false;
};
const isDirectInvocation =
process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href;
if (isDirectInvocation) {
console.log('Waiting for Midscene Studio shell build output...');
const ready = await waitForBuild();
if (ready) {
console.log('Midscene Studio shell build is ready.');
process.exit(0);
}
console.error(
'Timed out waiting for the Midscene Studio shell build to finish.',
);
process.exit(1);
}