mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-12 12:54:07 -07:00
Extensions update command (#8100)
This commit is contained in:
@@ -4,16 +4,42 @@
|
|||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { describe, it, expect } from 'vitest';
|
import {
|
||||||
import { extensionsCommand } from './extensionsCommand.js';
|
updateAllUpdatableExtensions,
|
||||||
import { type CommandContext } from './types.js';
|
updateExtensionByName,
|
||||||
|
} from '../../config/extension.js';
|
||||||
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
|
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
|
||||||
import { MessageType } from '../types.js';
|
import { MessageType } from '../types.js';
|
||||||
|
import { extensionsCommand } from './extensionsCommand.js';
|
||||||
|
import { type CommandContext } from './types.js';
|
||||||
|
import {
|
||||||
|
describe,
|
||||||
|
it,
|
||||||
|
expect,
|
||||||
|
vi,
|
||||||
|
beforeEach,
|
||||||
|
type MockedFunction,
|
||||||
|
} from 'vitest';
|
||||||
|
|
||||||
|
vi.mock('../../config/extension.js', () => ({
|
||||||
|
updateExtensionByName: vi.fn(),
|
||||||
|
updateAllUpdatableExtensions: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
const mockUpdateExtensionByName = updateExtensionByName as MockedFunction<
|
||||||
|
typeof updateExtensionByName
|
||||||
|
>;
|
||||||
|
|
||||||
|
const mockUpdateAllUpdatableExtensions =
|
||||||
|
updateAllUpdatableExtensions as MockedFunction<
|
||||||
|
typeof updateAllUpdatableExtensions
|
||||||
|
>;
|
||||||
|
|
||||||
describe('extensionsCommand', () => {
|
describe('extensionsCommand', () => {
|
||||||
let mockContext: CommandContext;
|
let mockContext: CommandContext;
|
||||||
|
|
||||||
it('should display "No active extensions." when none are found', async () => {
|
beforeEach(() => {
|
||||||
|
vi.resetAllMocks();
|
||||||
mockContext = createMockCommandContext({
|
mockContext = createMockCommandContext({
|
||||||
services: {
|
services: {
|
||||||
config: {
|
config: {
|
||||||
@@ -21,7 +47,10 @@ describe('extensionsCommand', () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('list', () => {
|
||||||
|
it('should display "No active extensions." when none are found', async () => {
|
||||||
if (!extensionsCommand.action) throw new Error('Action not defined');
|
if (!extensionsCommand.action) throw new Error('Action not defined');
|
||||||
await extensionsCommand.action(mockContext, '');
|
await extensionsCommand.action(mockContext, '');
|
||||||
|
|
||||||
@@ -65,3 +94,135 @@ describe('extensionsCommand', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('update', () => {
|
||||||
|
const updateAction = extensionsCommand.subCommands?.find(
|
||||||
|
(cmd) => cmd.name === 'update',
|
||||||
|
)?.action;
|
||||||
|
|
||||||
|
if (!updateAction) {
|
||||||
|
throw new Error('Update action not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
it('should show usage if no args are provided', async () => {
|
||||||
|
await updateAction(mockContext, '');
|
||||||
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||||
|
{
|
||||||
|
type: MessageType.ERROR,
|
||||||
|
text: 'Usage: /extensions update <extension-names>|--all',
|
||||||
|
},
|
||||||
|
expect.any(Number),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should inform user if there are no extensions to update with --all', async () => {
|
||||||
|
mockUpdateAllUpdatableExtensions.mockResolvedValue([]);
|
||||||
|
await updateAction(mockContext, '--all');
|
||||||
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||||
|
{
|
||||||
|
type: MessageType.INFO,
|
||||||
|
text: 'No extensions to update.',
|
||||||
|
},
|
||||||
|
expect.any(Number),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should update all extensions with --all', async () => {
|
||||||
|
mockUpdateAllUpdatableExtensions.mockResolvedValue([
|
||||||
|
{
|
||||||
|
name: 'ext-one',
|
||||||
|
originalVersion: '1.0.0',
|
||||||
|
updatedVersion: '1.0.1',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'ext-two',
|
||||||
|
originalVersion: '2.0.0',
|
||||||
|
updatedVersion: '2.0.1',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
await updateAction(mockContext, '--all');
|
||||||
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||||
|
{
|
||||||
|
type: MessageType.INFO,
|
||||||
|
text:
|
||||||
|
'Extension "ext-one" successfully updated: 1.0.0 → 1.0.1.\n' +
|
||||||
|
'Extension "ext-two" successfully updated: 2.0.0 → 2.0.1.\n' +
|
||||||
|
'Restart gemini-cli to see the changes.',
|
||||||
|
},
|
||||||
|
expect.any(Number),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle errors when updating all extensions', async () => {
|
||||||
|
mockUpdateAllUpdatableExtensions.mockRejectedValue(
|
||||||
|
new Error('Something went wrong'),
|
||||||
|
);
|
||||||
|
await updateAction(mockContext, '--all');
|
||||||
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||||
|
{
|
||||||
|
type: MessageType.ERROR,
|
||||||
|
text: 'Something went wrong',
|
||||||
|
},
|
||||||
|
expect.any(Number),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should update a single extension by name', async () => {
|
||||||
|
mockUpdateExtensionByName.mockResolvedValue({
|
||||||
|
name: 'ext-one',
|
||||||
|
originalVersion: '1.0.0',
|
||||||
|
updatedVersion: '1.0.1',
|
||||||
|
});
|
||||||
|
await updateAction(mockContext, 'ext-one');
|
||||||
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||||
|
{
|
||||||
|
type: MessageType.INFO,
|
||||||
|
text:
|
||||||
|
'Extension "ext-one" successfully updated: 1.0.0 → 1.0.1.\n' +
|
||||||
|
'Restart gemini-cli to see the changes.',
|
||||||
|
},
|
||||||
|
expect.any(Number),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle errors when updating a single extension', async () => {
|
||||||
|
mockUpdateExtensionByName.mockRejectedValue(
|
||||||
|
new Error('Extension not found'),
|
||||||
|
);
|
||||||
|
await updateAction(mockContext, 'ext-one');
|
||||||
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||||
|
{
|
||||||
|
type: MessageType.ERROR,
|
||||||
|
text: 'Extension not found',
|
||||||
|
},
|
||||||
|
expect.any(Number),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should update multiple extensions by name', async () => {
|
||||||
|
mockUpdateExtensionByName
|
||||||
|
.mockResolvedValueOnce({
|
||||||
|
name: 'ext-one',
|
||||||
|
originalVersion: '1.0.0',
|
||||||
|
updatedVersion: '1.0.1',
|
||||||
|
})
|
||||||
|
.mockResolvedValueOnce({
|
||||||
|
name: 'ext-two',
|
||||||
|
originalVersion: '2.0.0',
|
||||||
|
updatedVersion: '2.0.1',
|
||||||
|
});
|
||||||
|
await updateAction(mockContext, 'ext-one ext-two');
|
||||||
|
expect(mockUpdateExtensionByName).toHaveBeenCalledTimes(2);
|
||||||
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||||
|
{
|
||||||
|
type: MessageType.INFO,
|
||||||
|
text:
|
||||||
|
'Extension "ext-one" successfully updated: 1.0.0 → 1.0.1.\n' +
|
||||||
|
'Extension "ext-two" successfully updated: 2.0.0 → 2.0.1.\n' +
|
||||||
|
'Restart gemini-cli to see the changes.',
|
||||||
|
},
|
||||||
|
expect.any(Number),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -4,18 +4,20 @@
|
|||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import {
|
||||||
|
updateExtensionByName,
|
||||||
|
updateAllUpdatableExtensions,
|
||||||
|
type ExtensionUpdateInfo,
|
||||||
|
} from '../../config/extension.js';
|
||||||
|
import { getErrorMessage } from '../../utils/errors.js';
|
||||||
|
import { MessageType } from '../types.js';
|
||||||
import {
|
import {
|
||||||
type CommandContext,
|
type CommandContext,
|
||||||
type SlashCommand,
|
type SlashCommand,
|
||||||
CommandKind,
|
CommandKind,
|
||||||
} from './types.js';
|
} from './types.js';
|
||||||
import { MessageType } from '../types.js';
|
|
||||||
|
|
||||||
export const extensionsCommand: SlashCommand = {
|
async function listAction(context: CommandContext) {
|
||||||
name: 'extensions',
|
|
||||||
description: 'list active extensions',
|
|
||||||
kind: CommandKind.BUILT_IN,
|
|
||||||
action: async (context: CommandContext): Promise<void> => {
|
|
||||||
const activeExtensions = context.services.config
|
const activeExtensions = context.services.config
|
||||||
?.getExtensions()
|
?.getExtensions()
|
||||||
.filter((ext) => ext.isActive);
|
.filter((ext) => ext.isActive);
|
||||||
@@ -42,5 +44,91 @@ export const extensionsCommand: SlashCommand = {
|
|||||||
},
|
},
|
||||||
Date.now(),
|
Date.now(),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateOutput = (info: ExtensionUpdateInfo) =>
|
||||||
|
`Extension "${info.name}" successfully updated: ${info.originalVersion} → ${info.updatedVersion}.`;
|
||||||
|
|
||||||
|
async function updateAction(context: CommandContext, args: string) {
|
||||||
|
const updateArgs = args.split(' ').filter((value) => value.length > 0);
|
||||||
|
const all = updateArgs.length === 1 && updateArgs[0] === '--all';
|
||||||
|
const names = all ? undefined : updateArgs;
|
||||||
|
let updateInfos: ExtensionUpdateInfo[] = [];
|
||||||
|
try {
|
||||||
|
if (all) {
|
||||||
|
updateInfos = await updateAllUpdatableExtensions();
|
||||||
|
} else if (names?.length) {
|
||||||
|
for (const name of names) {
|
||||||
|
updateInfos.push(await updateExtensionByName(name));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
context.ui.addItem(
|
||||||
|
{
|
||||||
|
type: MessageType.ERROR,
|
||||||
|
text: 'Usage: /extensions update <extension-names>|--all',
|
||||||
},
|
},
|
||||||
|
Date.now(),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter to the actually updated ones.
|
||||||
|
updateInfos = updateInfos.filter(
|
||||||
|
(info) => info.originalVersion !== info.updatedVersion,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (updateInfos.length === 0) {
|
||||||
|
context.ui.addItem(
|
||||||
|
{
|
||||||
|
type: MessageType.INFO,
|
||||||
|
text: 'No extensions to update.',
|
||||||
|
},
|
||||||
|
Date.now(),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
context.ui.addItem(
|
||||||
|
{
|
||||||
|
type: MessageType.INFO,
|
||||||
|
text: [
|
||||||
|
...updateInfos.map((info) => updateOutput(info)),
|
||||||
|
'Restart gemini-cli to see the changes.',
|
||||||
|
].join('\n'),
|
||||||
|
},
|
||||||
|
Date.now(),
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
context.ui.addItem(
|
||||||
|
{
|
||||||
|
type: MessageType.ERROR,
|
||||||
|
text: getErrorMessage(error),
|
||||||
|
},
|
||||||
|
Date.now(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const listExtensionsCommand: SlashCommand = {
|
||||||
|
name: 'list',
|
||||||
|
description: 'List active extensions',
|
||||||
|
kind: CommandKind.BUILT_IN,
|
||||||
|
action: listAction,
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateExtensionsCommand: SlashCommand = {
|
||||||
|
name: 'update',
|
||||||
|
description: 'Update extensions. Usage: update <extension-names>|--all',
|
||||||
|
kind: CommandKind.BUILT_IN,
|
||||||
|
action: updateAction,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const extensionsCommand: SlashCommand = {
|
||||||
|
name: 'extensions',
|
||||||
|
description: 'Manage extensions',
|
||||||
|
kind: CommandKind.BUILT_IN,
|
||||||
|
subCommands: [listExtensionsCommand, updateExtensionsCommand],
|
||||||
|
action: (context, args) =>
|
||||||
|
// Default to list if no subcommand is provided
|
||||||
|
listExtensionsCommand.action!(context, args),
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user