mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-21 09:17:53 +00:00
feat: Add auto update functionality (#4686)
This commit is contained in:
@@ -50,7 +50,9 @@ describe('checkForUpdates', () => {
|
||||
name: 'test-package',
|
||||
version: '1.0.0',
|
||||
});
|
||||
updateNotifier.mockReturnValue({ update: null });
|
||||
updateNotifier.mockReturnValue({
|
||||
fetchInfo: vi.fn(async () => null),
|
||||
});
|
||||
const result = await checkForUpdates();
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
@@ -61,10 +63,12 @@ describe('checkForUpdates', () => {
|
||||
version: '1.0.0',
|
||||
});
|
||||
updateNotifier.mockReturnValue({
|
||||
update: { current: '1.0.0', latest: '1.1.0' },
|
||||
fetchInfo: vi.fn(async () => ({ current: '1.0.0', latest: '1.1.0' })),
|
||||
});
|
||||
|
||||
const result = await checkForUpdates();
|
||||
expect(result).toContain('1.0.0 → 1.1.0');
|
||||
expect(result?.message).toContain('1.0.0 → 1.1.0');
|
||||
expect(result?.update).toEqual({ current: '1.0.0', latest: '1.1.0' });
|
||||
});
|
||||
|
||||
it('should return null if the latest version is the same as the current version', async () => {
|
||||
@@ -73,7 +77,7 @@ describe('checkForUpdates', () => {
|
||||
version: '1.0.0',
|
||||
});
|
||||
updateNotifier.mockReturnValue({
|
||||
update: { current: '1.0.0', latest: '1.0.0' },
|
||||
fetchInfo: vi.fn(async () => ({ current: '1.0.0', latest: '1.0.0' })),
|
||||
});
|
||||
const result = await checkForUpdates();
|
||||
expect(result).toBeNull();
|
||||
@@ -85,7 +89,7 @@ describe('checkForUpdates', () => {
|
||||
version: '1.1.0',
|
||||
});
|
||||
updateNotifier.mockReturnValue({
|
||||
update: { current: '1.1.0', latest: '1.0.0' },
|
||||
fetchInfo: vi.fn(async () => ({ current: '1.0.0', latest: '0.09' })),
|
||||
});
|
||||
const result = await checkForUpdates();
|
||||
expect(result).toBeNull();
|
||||
|
||||
@@ -4,11 +4,16 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import updateNotifier from 'update-notifier';
|
||||
import updateNotifier, { UpdateInfo } from 'update-notifier';
|
||||
import semver from 'semver';
|
||||
import { getPackageJson } from '../../utils/package.js';
|
||||
|
||||
export async function checkForUpdates(): Promise<string | null> {
|
||||
export interface UpdateObject {
|
||||
message: string;
|
||||
update: UpdateInfo;
|
||||
}
|
||||
|
||||
export async function checkForUpdates(): Promise<UpdateObject | null> {
|
||||
try {
|
||||
// Skip update check when running from source (development mode)
|
||||
if (process.env.DEV === 'true') {
|
||||
@@ -30,11 +35,13 @@ export async function checkForUpdates(): Promise<string | null> {
|
||||
shouldNotifyInNpmScript: true,
|
||||
});
|
||||
|
||||
if (
|
||||
notifier.update &&
|
||||
semver.gt(notifier.update.latest, notifier.update.current)
|
||||
) {
|
||||
return `Gemini CLI update available! ${notifier.update.current} → ${notifier.update.latest}\nRun npm install -g ${packageJson.name} to update`;
|
||||
const updateInfo = await notifier.fetchInfo();
|
||||
|
||||
if (updateInfo && semver.gt(updateInfo.latest, updateInfo.current)) {
|
||||
return {
|
||||
message: `Gemini CLI update available! ${updateInfo.current} → ${updateInfo.latest}`,
|
||||
update: updateInfo,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user