Skip to content

Commit 9015c20

Browse files
committed
refactor: narrow legacy patch helper API
1 parent 0bf3ab4 commit 9015c20

1 file changed

Lines changed: 55 additions & 88 deletions

File tree

scripts/prepare-resources.mjs

Lines changed: 55 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -209,56 +209,43 @@ const patchMonacoCssNestingWarnings = async (dashboardDir) => {
209209
}
210210
};
211211

212-
const patchLegacyDesktopBridgeArtifacts = async (dashboardDir) => {
213-
const patchFile = async (filePath, transform, patchLabel, options = {}) => {
214-
const {
215-
warnOnNoChange = false,
216-
failOnNoChange = false,
217-
isModern = null,
218-
requireFile = false,
219-
} = options;
220-
221-
if (!existsSync(filePath)) {
222-
if (requireFile) {
223-
throw new Error(
224-
`[prepare-resources] Missing required file for ${patchLabel}: ${path.relative(projectRoot, filePath)}`,
225-
);
226-
}
227-
return;
228-
}
212+
const patchRequiredLegacyFile = async ({ filePath, transform, patchLabel, isAlreadyModern }) => {
213+
if (!existsSync(filePath)) {
214+
throw new Error(
215+
`[prepare-resources] Missing required file for ${patchLabel}: ${path.relative(projectRoot, filePath)}`,
216+
);
217+
}
229218

230-
const source = await readFile(filePath, 'utf8');
231-
const patched = transform(source);
219+
const source = await readFile(filePath, 'utf8');
220+
const patched = transform(source);
232221

233-
if (isModern && !isModern(patched)) {
234-
throw new Error(
235-
`[prepare-resources] ${patchLabel} failed invariant check in ${path.relative(projectRoot, filePath)}`,
236-
);
237-
}
222+
// Invariant: transformed content must be modern/compatible.
223+
if (!isAlreadyModern(patched)) {
224+
throw new Error(
225+
`[prepare-resources] ${patchLabel} failed invariant check in ${path.relative(projectRoot, filePath)}`,
226+
);
227+
}
238228

239-
if (patched !== source) {
240-
await writeFile(filePath, patched, 'utf8');
241-
console.log(
242-
`[prepare-resources] Patched ${patchLabel} in ${path.relative(projectRoot, filePath)}`,
243-
);
244-
return;
245-
}
229+
if (patched !== source) {
230+
await writeFile(filePath, patched, 'utf8');
231+
console.log(
232+
`[prepare-resources] Patched ${patchLabel} in ${path.relative(projectRoot, filePath)}`,
233+
);
234+
return;
235+
}
246236

247-
const allowNoChange = isModern ? isModern(source) : false;
248-
if (failOnNoChange && !allowNoChange) {
249-
throw new Error(
250-
`[prepare-resources] ${patchLabel} did not match expected legacy pattern in ${path.relative(projectRoot, filePath)}`,
251-
);
252-
}
237+
if (!isAlreadyModern(source)) {
238+
throw new Error(
239+
`[prepare-resources] ${patchLabel} did not match expected legacy pattern in ${path.relative(projectRoot, filePath)}`,
240+
);
241+
}
253242

254-
if (warnOnNoChange) {
255-
const suffix = allowNoChange ? ' (already compatible)' : '';
256-
console.warn(
257-
`[prepare-resources] WARN: No changes applied for ${patchLabel} in ${path.relative(projectRoot, filePath)}${suffix}`,
258-
);
259-
}
260-
};
243+
console.warn(
244+
`[prepare-resources] WARN: No changes applied for ${patchLabel} in ${path.relative(projectRoot, filePath)} (already compatible)`,
245+
);
246+
};
261247

248+
const patchLegacyDesktopBridgeArtifacts = async (dashboardDir) => {
262249
const hasModernTrayRestartGuard = (source) =>
263250
/if\s*\(\s*!desktopBridge\?\.onTrayRestartBackend\s*\)\s*\{/.test(source);
264251
const hasModernDesktopBridgeTypes = (source) =>
@@ -271,25 +258,20 @@ const patchLegacyDesktopBridgeArtifacts = async (dashboardDir) => {
271258
const hasModernRestartCapabilityGuard = (source) =>
272259
source.includes('const hasDesktopRestartCapability =');
273260

274-
await patchFile(
275-
path.join(dashboardDir, 'src', 'App.vue'),
276-
(source) =>
261+
await patchRequiredLegacyFile({
262+
filePath: path.join(dashboardDir, 'src', 'App.vue'),
263+
transform: (source) =>
277264
source.replace(
278265
/if\s*\(\s*!desktopBridge\?\.isElectron\s*\|\|\s*!desktopBridge\.onTrayRestartBackend\s*\)\s*\{/,
279266
'if (!desktopBridge?.onTrayRestartBackend) {',
280267
),
281-
'tray restart desktop guard',
282-
{
283-
warnOnNoChange: true,
284-
failOnNoChange: true,
285-
isModern: hasModernTrayRestartGuard,
286-
requireFile: true,
287-
},
288-
);
268+
patchLabel: 'tray restart desktop guard',
269+
isAlreadyModern: hasModernTrayRestartGuard,
270+
});
289271

290-
await patchFile(
291-
path.join(dashboardDir, 'src', 'types', 'electron-bridge.d.ts'),
292-
(source) => {
272+
await patchRequiredLegacyFile({
273+
filePath: path.join(dashboardDir, 'src', 'types', 'electron-bridge.d.ts'),
274+
transform: (source) => {
293275
let patched = source;
294276
patched = patched.replace(/^\s+isElectron:\s*boolean;\n/m, ' isDesktop: boolean;\n');
295277
patched = patched.replace(
@@ -298,18 +280,13 @@ const patchLegacyDesktopBridgeArtifacts = async (dashboardDir) => {
298280
);
299281
return patched;
300282
},
301-
'desktop bridge type definitions',
302-
{
303-
warnOnNoChange: true,
304-
failOnNoChange: true,
305-
isModern: hasModernDesktopBridgeTypes,
306-
requireFile: true,
307-
},
308-
);
283+
patchLabel: 'desktop bridge type definitions',
284+
isAlreadyModern: hasModernDesktopBridgeTypes,
285+
});
309286

310-
await patchFile(
311-
path.join(dashboardDir, 'src', 'layouts', 'full', 'vertical-header', 'VerticalHeader.vue'),
312-
(source) => {
287+
await patchRequiredLegacyFile({
288+
filePath: path.join(dashboardDir, 'src', 'layouts', 'full', 'vertical-header', 'VerticalHeader.vue'),
289+
transform: (source) => {
313290
let patched = source.replaceAll(/\bisElectronApp\b/g, 'isDesktopReleaseMode');
314291
patched = patched.replace(
315292
/typeof window !== 'undefined'\s*&&\s*!!window\.astrbotDesktop\?\.isElectron/,
@@ -321,18 +298,13 @@ const patchLegacyDesktopBridgeArtifacts = async (dashboardDir) => {
321298
);
322299
return patched;
323300
},
324-
'desktop update mode guards',
325-
{
326-
warnOnNoChange: true,
327-
failOnNoChange: true,
328-
isModern: (source) => !hasLegacyDesktopReleaseGuards(source),
329-
requireFile: true,
330-
},
331-
);
301+
patchLabel: 'desktop update mode guards',
302+
isAlreadyModern: (source) => !hasLegacyDesktopReleaseGuards(source),
303+
});
332304

333-
await patchFile(
334-
path.join(dashboardDir, 'src', 'utils', 'restartAstrBot.ts'),
335-
(source) => {
305+
await patchRequiredLegacyFile({
306+
filePath: path.join(dashboardDir, 'src', 'utils', 'restartAstrBot.ts'),
307+
transform: (source) => {
336308
if (source.includes('const hasDesktopRestartCapability =')) {
337309
return source;
338310
}
@@ -355,14 +327,9 @@ const patchLegacyDesktopBridgeArtifacts = async (dashboardDir) => {
355327
if (hasDesktopRestartCapability && isDesktopRuntime) {`,
356328
);
357329
},
358-
'desktop restart capability guard',
359-
{
360-
warnOnNoChange: true,
361-
failOnNoChange: true,
362-
isModern: hasModernRestartCapabilityGuard,
363-
requireFile: true,
364-
},
365-
);
330+
patchLabel: 'desktop restart capability guard',
331+
isAlreadyModern: hasModernRestartCapabilityGuard,
332+
});
366333
};
367334

368335
const readAstrbotVersionFromPyproject = async (sourceDir) => {

0 commit comments

Comments
 (0)