2025-07-15 17:40:09 -04:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-09-10 08:09:09 -07:00
|
|
|
import {
|
|
|
|
|
updateExtensionByName,
|
|
|
|
|
updateAllUpdatableExtensions,
|
|
|
|
|
type ExtensionUpdateInfo,
|
|
|
|
|
} from '../../config/extension.js';
|
|
|
|
|
import { getErrorMessage } from '../../utils/errors.js';
|
|
|
|
|
import { MessageType } from '../types.js';
|
2025-07-20 16:57:34 -04:00
|
|
|
import {
|
|
|
|
|
type CommandContext,
|
|
|
|
|
type SlashCommand,
|
|
|
|
|
CommandKind,
|
|
|
|
|
} from './types.js';
|
2025-07-15 17:40:09 -04:00
|
|
|
|
2025-09-10 08:09:09 -07:00
|
|
|
async function listAction(context: CommandContext) {
|
|
|
|
|
context.ui.addItem(
|
|
|
|
|
{
|
2025-09-12 09:20:04 -07:00
|
|
|
type: MessageType.EXTENSIONS_LIST,
|
2025-09-10 08:09:09 -07:00
|
|
|
},
|
|
|
|
|
Date.now(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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[] = [];
|
2025-09-12 09:20:04 -07:00
|
|
|
|
|
|
|
|
if (!all && names?.length === 0) {
|
|
|
|
|
context.ui.addItem(
|
|
|
|
|
{
|
|
|
|
|
type: MessageType.ERROR,
|
|
|
|
|
text: 'Usage: /extensions update <extension-names>|--all',
|
|
|
|
|
},
|
|
|
|
|
Date.now(),
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 08:09:09 -07:00
|
|
|
try {
|
2025-09-12 09:20:04 -07:00
|
|
|
context.ui.setPendingItem({
|
|
|
|
|
type: MessageType.EXTENSIONS_LIST,
|
|
|
|
|
});
|
2025-09-10 08:09:09 -07:00
|
|
|
if (all) {
|
2025-09-12 09:20:04 -07:00
|
|
|
updateInfos = await updateAllUpdatableExtensions(
|
|
|
|
|
context.services.config!.getWorkingDir(),
|
|
|
|
|
context.services.config!.getExtensions(),
|
|
|
|
|
context.ui.extensionsUpdateState,
|
|
|
|
|
context.ui.setExtensionsUpdateState,
|
|
|
|
|
);
|
2025-09-10 08:09:09 -07:00
|
|
|
} else if (names?.length) {
|
|
|
|
|
for (const name of names) {
|
2025-09-12 09:20:04 -07:00
|
|
|
updateInfos.push(
|
|
|
|
|
await updateExtensionByName(
|
|
|
|
|
name,
|
|
|
|
|
context.services.config!.getWorkingDir(),
|
|
|
|
|
context.services.config!.getExtensions(),
|
|
|
|
|
(updateState) => {
|
|
|
|
|
const newState = new Map(context.ui.extensionsUpdateState);
|
|
|
|
|
newState.set(name, updateState);
|
|
|
|
|
context.ui.setExtensionsUpdateState(newState);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
2025-09-10 08:09:09 -07:00
|
|
|
}
|
2025-07-15 17:40:09 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-10 08:09:09 -07:00
|
|
|
if (updateInfos.length === 0) {
|
|
|
|
|
context.ui.addItem(
|
|
|
|
|
{
|
|
|
|
|
type: MessageType.INFO,
|
|
|
|
|
text: 'No extensions to update.',
|
|
|
|
|
},
|
|
|
|
|
Date.now(),
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-09-12 09:20:04 -07:00
|
|
|
} catch (error) {
|
2025-07-15 17:40:09 -04:00
|
|
|
context.ui.addItem(
|
|
|
|
|
{
|
2025-09-12 09:20:04 -07:00
|
|
|
type: MessageType.ERROR,
|
|
|
|
|
text: getErrorMessage(error),
|
2025-09-10 08:09:09 -07:00
|
|
|
},
|
|
|
|
|
Date.now(),
|
|
|
|
|
);
|
2025-09-12 09:20:04 -07:00
|
|
|
} finally {
|
2025-09-10 08:09:09 -07:00
|
|
|
context.ui.addItem(
|
|
|
|
|
{
|
2025-09-12 09:20:04 -07:00
|
|
|
type: MessageType.EXTENSIONS_LIST,
|
2025-07-15 17:40:09 -04:00
|
|
|
},
|
|
|
|
|
Date.now(),
|
|
|
|
|
);
|
2025-09-12 09:20:04 -07:00
|
|
|
context.ui.setPendingItem(null);
|
2025-09-10 08:09:09 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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),
|
2025-07-15 17:40:09 -04:00
|
|
|
};
|