Skip to content

Commit 7da91ec

Browse files
authored
fix(agent-tars): autoUpdater lack of latest-mac.yml (#341)
1 parent d298fdd commit 7da91ec

File tree

7 files changed

+143
-13
lines changed

7 files changed

+143
-13
lines changed

apps/agent-tars/forge.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { rimraf, rimrafSync } from 'rimraf';
1212
import {
1313
getModuleRoot,
1414
getExternalPkgsDependencies,
15+
hooks,
1516
} from '@common/electron-build';
1617

1718
import pkg from './package.json';
@@ -229,6 +230,11 @@ const config: ForgeConfig = {
229230
}),
230231
]),
231232
],
233+
hooks: {
234+
postMake: async (forgeConfig, makeResults) => {
235+
return await hooks.postMake?.(forgeConfig, makeResults);
236+
},
237+
},
232238
};
233239

234240
export default config;

apps/agent-tars/src/main/utils/updateApp.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@ import {
99
export class AppUpdater {
1010
autoUpdater: ElectronAppUpdater = autoUpdater;
1111
constructor(mainWindow: BrowserWindow) {
12-
autoUpdater.logger = logger;
13-
autoUpdater.autoDownload = true;
14-
autoUpdater.forceDevUpdateConfig = !app.isPackaged;
12+
if (app.isPackaged) {
13+
autoUpdater.logger = logger;
14+
autoUpdater.autoDownload = true;
1515

16-
autoUpdater.on('error', (error) => {
17-
logger.error('Update_Error', error);
18-
mainWindow.webContents.send('main:error', error);
19-
});
16+
autoUpdater.on('error', (error) => {
17+
logger.error('Update_Error', error);
18+
mainWindow.webContents.send('main:error', error);
19+
});
2020

21-
autoUpdater.on('update-available', (releaseInfo: UpdateInfo) => {
22-
logger.info('new version', releaseInfo);
23-
mainWindow.webContents.send('app-update-available', releaseInfo);
24-
});
21+
autoUpdater.on('update-available', (releaseInfo: UpdateInfo) => {
22+
logger.info('new version', releaseInfo);
23+
mainWindow.webContents.send('app-update-available', releaseInfo);
24+
});
2525

26-
this.autoUpdater = autoUpdater;
26+
this.autoUpdater = autoUpdater;
2727

28-
this.autoUpdater.checkForUpdatesAndNotify();
28+
this.autoUpdater.checkForUpdatesAndNotify();
29+
}
2930
}
3031
}

packages/common/electron-build/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@common/configs": "workspace:*"
4343
},
4444
"devDependencies": {
45+
"@electron-forge/shared-types": "^7.8.0",
4546
"tsx": "^4.19.2",
4647
"@rslib/core": "^0.5.4",
4748
"typescript": "^5.7.2",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { type ForgeHookMap } from '@electron-forge/shared-types';
2+
import { postMake } from './postMake';
3+
4+
export const hooks: ForgeHookMap = {
5+
postMake,
6+
};
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* Copyright (c) 2025 Bytedance, Inc. and its affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
import crypto from 'node:crypto';
6+
import fs from 'node:fs';
7+
import path from 'node:path';
8+
import yaml from 'js-yaml';
9+
import type { ForgeHookMap } from '@electron-forge/shared-types';
10+
11+
const artifactRegex = /.*\.(?:exe|dmg|AppImage|zip)$/;
12+
const platformNamesMap = {
13+
darwin: 'macos',
14+
linux: 'linux',
15+
win32: 'windows',
16+
};
17+
const updateYmlMap = {
18+
darwin: 'latest-mac.yml',
19+
linux: 'latest-linux.yml',
20+
win32: 'latest.yml',
21+
};
22+
23+
export const postMake: ForgeHookMap['postMake'] = async (
24+
_forgeConfig,
25+
makeResults,
26+
) => {
27+
const yml: {
28+
version?: string;
29+
files: {
30+
url: string;
31+
sha512: string;
32+
size: number;
33+
}[];
34+
releaseDate?: string;
35+
} = {
36+
version: makeResults[0]?.packageJSON?.version,
37+
files: [],
38+
};
39+
40+
makeResults = makeResults.map((result) => {
41+
result.artifacts = result.artifacts.map((artifact) => {
42+
if (artifactRegex.test(artifact)) {
43+
try {
44+
const fileData = fs.readFileSync(artifact);
45+
const hash = crypto
46+
.createHash('sha512')
47+
.update(fileData)
48+
.digest('base64');
49+
const { size } = fs.statSync(artifact);
50+
51+
yml.files.push({
52+
url: path.basename(artifact),
53+
sha512: hash,
54+
size,
55+
});
56+
} catch {
57+
console.error(`Failed to hash ${artifact}`);
58+
}
59+
return artifact;
60+
} else {
61+
return artifact;
62+
}
63+
});
64+
return result;
65+
});
66+
yml.releaseDate = new Date().toISOString();
67+
68+
const firstResult = makeResults[0];
69+
if (!firstResult?.artifacts?.[0] || !firstResult.platform) {
70+
throw new Error('Missing required artifact or platform information');
71+
}
72+
73+
const ymlPath = `${path.dirname(firstResult.artifacts[0])}/${
74+
updateYmlMap[firstResult.platform as keyof typeof updateYmlMap]
75+
}`;
76+
77+
const ymlStr = yaml.dump(yml, {
78+
lineWidth: -1,
79+
});
80+
fs.writeFileSync(ymlPath, ymlStr);
81+
82+
makeResults.push({
83+
artifacts: [ymlPath],
84+
platform: makeResults[0].platform,
85+
arch: makeResults[0].arch,
86+
packageJSON: makeResults[0].packageJSON,
87+
});
88+
89+
return makeResults;
90+
};

packages/common/electron-build/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export {
66
getModuleRoot,
77
getExternalPkgsDependencies,
88
} from './getPackageDependencies';
9+
export { hooks } from './hooks/index';

pnpm-lock.yaml

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)