mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-02-01 22:48:03 +00:00
92 lines
2.4 KiB
TypeScript
92 lines
2.4 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2026 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
import type { ArgumentsCamelCase, CommandModule } from 'yargs';
|
|
import {
|
|
coreEvents,
|
|
ExitCodes,
|
|
getAdminErrorMessage,
|
|
} from '@google/gemini-cli-core';
|
|
import { runExitCleanup } from './utils/cleanup.js';
|
|
import type { MergedSettings } from './config/settings.js';
|
|
import process from 'node:process';
|
|
|
|
export interface DeferredCommand {
|
|
handler: (argv: ArgumentsCamelCase) => void | Promise<void>;
|
|
argv: ArgumentsCamelCase;
|
|
commandName: string;
|
|
}
|
|
|
|
let deferredCommand: DeferredCommand | undefined;
|
|
|
|
export function setDeferredCommand(command: DeferredCommand) {
|
|
deferredCommand = command;
|
|
}
|
|
|
|
export async function runDeferredCommand(settings: MergedSettings) {
|
|
if (!deferredCommand) {
|
|
return;
|
|
}
|
|
|
|
const adminSettings = settings.admin;
|
|
const commandName = deferredCommand.commandName;
|
|
|
|
if (commandName === 'mcp' && adminSettings?.mcp?.enabled === false) {
|
|
coreEvents.emitFeedback(
|
|
'error',
|
|
getAdminErrorMessage('MCP', undefined /* config */),
|
|
);
|
|
await runExitCleanup();
|
|
process.exit(ExitCodes.FATAL_CONFIG_ERROR);
|
|
}
|
|
|
|
if (
|
|
commandName === 'extensions' &&
|
|
adminSettings?.extensions?.enabled === false
|
|
) {
|
|
coreEvents.emitFeedback(
|
|
'error',
|
|
getAdminErrorMessage('Extensions', undefined /* config */),
|
|
);
|
|
await runExitCleanup();
|
|
process.exit(ExitCodes.FATAL_CONFIG_ERROR);
|
|
}
|
|
|
|
if (commandName === 'skills' && adminSettings?.skills?.enabled === false) {
|
|
coreEvents.emitFeedback(
|
|
'error',
|
|
getAdminErrorMessage('Agent skills', undefined /* config */),
|
|
);
|
|
await runExitCleanup();
|
|
process.exit(ExitCodes.FATAL_CONFIG_ERROR);
|
|
}
|
|
|
|
await deferredCommand.handler(deferredCommand.argv);
|
|
await runExitCleanup();
|
|
process.exit(ExitCodes.SUCCESS);
|
|
}
|
|
|
|
/**
|
|
* Wraps a command's handler to defer its execution.
|
|
* It stores the handler and arguments in a singleton `deferredCommand` variable.
|
|
*/
|
|
export function defer<T = object, U = object>(
|
|
commandModule: CommandModule<T, U>,
|
|
parentCommandName?: string,
|
|
): CommandModule<T, U> {
|
|
return {
|
|
...commandModule,
|
|
handler: (argv: ArgumentsCamelCase<U>) => {
|
|
setDeferredCommand({
|
|
handler: commandModule.handler as (
|
|
argv: ArgumentsCamelCase,
|
|
) => void | Promise<void>,
|
|
argv: argv as unknown as ArgumentsCamelCase,
|
|
commandName: parentCommandName || 'unknown',
|
|
});
|
|
},
|
|
};
|
|
}
|