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,47 +47,182 @@ describe('extensionsCommand', () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!extensionsCommand.action) throw new Error('Action not defined');
|
|
||||||
await extensionsCommand.action(mockContext, '');
|
|
||||||
|
|
||||||
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
|
||||||
{
|
|
||||||
type: MessageType.INFO,
|
|
||||||
text: 'No active extensions.',
|
|
||||||
},
|
|
||||||
expect.any(Number),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should list active extensions when they are found', async () => {
|
describe('list', () => {
|
||||||
const mockExtensions = [
|
it('should display "No active extensions." when none are found', async () => {
|
||||||
{ name: 'ext-one', version: '1.0.0', isActive: true },
|
if (!extensionsCommand.action) throw new Error('Action not defined');
|
||||||
{ name: 'ext-two', version: '2.1.0', isActive: true },
|
await extensionsCommand.action(mockContext, '');
|
||||||
{ name: 'ext-three', version: '3.0.0', isActive: false },
|
|
||||||
];
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||||
mockContext = createMockCommandContext({
|
{
|
||||||
services: {
|
type: MessageType.INFO,
|
||||||
config: {
|
text: 'No active extensions.',
|
||||||
getExtensions: () => mockExtensions,
|
|
||||||
},
|
},
|
||||||
},
|
expect.any(Number),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!extensionsCommand.action) throw new Error('Action not defined');
|
it('should list active extensions when they are found', async () => {
|
||||||
await extensionsCommand.action(mockContext, '');
|
const mockExtensions = [
|
||||||
|
{ name: 'ext-one', version: '1.0.0', isActive: true },
|
||||||
|
{ name: 'ext-two', version: '2.1.0', isActive: true },
|
||||||
|
{ name: 'ext-three', version: '3.0.0', isActive: false },
|
||||||
|
];
|
||||||
|
mockContext = createMockCommandContext({
|
||||||
|
services: {
|
||||||
|
config: {
|
||||||
|
getExtensions: () => mockExtensions,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const expectedMessage =
|
if (!extensionsCommand.action) throw new Error('Action not defined');
|
||||||
'Active extensions:\n\n' +
|
await extensionsCommand.action(mockContext, '');
|
||||||
` - \u001b[36mext-one (v1.0.0)\u001b[0m\n` +
|
|
||||||
` - \u001b[36mext-two (v2.1.0)\u001b[0m\n`;
|
|
||||||
|
|
||||||
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
const expectedMessage =
|
||||||
{
|
'Active extensions:\n\n' +
|
||||||
type: MessageType.INFO,
|
` - \u001b[36mext-one (v1.0.0)\u001b[0m\n` +
|
||||||
text: expectedMessage,
|
` - \u001b[36mext-two (v2.1.0)\u001b[0m\n`;
|
||||||
},
|
|
||||||
expect.any(Number),
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
||||||
);
|
{
|
||||||
|
type: MessageType.INFO,
|
||||||
|
text: expectedMessage,
|
||||||
|
},
|
||||||
|
expect.any(Number),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
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,43 +4,131 @@
|
|||||||
* 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',
|
const activeExtensions = context.services.config
|
||||||
description: 'list active extensions',
|
?.getExtensions()
|
||||||
kind: CommandKind.BUILT_IN,
|
.filter((ext) => ext.isActive);
|
||||||
action: async (context: CommandContext): Promise<void> => {
|
if (!activeExtensions || activeExtensions.length === 0) {
|
||||||
const activeExtensions = context.services.config
|
context.ui.addItem(
|
||||||
?.getExtensions()
|
{
|
||||||
.filter((ext) => ext.isActive);
|
type: MessageType.INFO,
|
||||||
if (!activeExtensions || activeExtensions.length === 0) {
|
text: 'No active extensions.',
|
||||||
|
},
|
||||||
|
Date.now(),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const extensionLines = activeExtensions.map(
|
||||||
|
(ext) => ` - \u001b[36m${ext.name} (v${ext.version})\u001b[0m`,
|
||||||
|
);
|
||||||
|
const message = `Active extensions:\n\n${extensionLines.join('\n')}\n`;
|
||||||
|
|
||||||
|
context.ui.addItem(
|
||||||
|
{
|
||||||
|
type: MessageType.INFO,
|
||||||
|
text: message,
|
||||||
|
},
|
||||||
|
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(
|
context.ui.addItem(
|
||||||
{
|
{
|
||||||
type: MessageType.INFO,
|
type: MessageType.ERROR,
|
||||||
text: 'No active extensions.',
|
text: 'Usage: /extensions update <extension-names>|--all',
|
||||||
},
|
},
|
||||||
Date.now(),
|
Date.now(),
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const extensionLines = activeExtensions.map(
|
// Filter to the actually updated ones.
|
||||||
(ext) => ` - \u001b[36m${ext.name} (v${ext.version})\u001b[0m`,
|
updateInfos = updateInfos.filter(
|
||||||
|
(info) => info.originalVersion !== info.updatedVersion,
|
||||||
);
|
);
|
||||||
const message = `Active extensions:\n\n${extensionLines.join('\n')}\n`;
|
|
||||||
|
if (updateInfos.length === 0) {
|
||||||
|
context.ui.addItem(
|
||||||
|
{
|
||||||
|
type: MessageType.INFO,
|
||||||
|
text: 'No extensions to update.',
|
||||||
|
},
|
||||||
|
Date.now(),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
context.ui.addItem(
|
context.ui.addItem(
|
||||||
{
|
{
|
||||||
type: MessageType.INFO,
|
type: MessageType.INFO,
|
||||||
text: message,
|
text: [
|
||||||
|
...updateInfos.map((info) => updateOutput(info)),
|
||||||
|
'Restart gemini-cli to see the changes.',
|
||||||
|
].join('\n'),
|
||||||
},
|
},
|
||||||
Date.now(),
|
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