Files
gemini-cli/packages/cli/src/commands/extensions/link.ts
T

90 lines
2.6 KiB
TypeScript
Raw Normal View History

2025-09-02 10:15:42 -07:00
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { CommandModule } from 'yargs';
import chalk from 'chalk';
import {
debugLogger,
type ExtensionInstallMetadata,
} from '@google/gemini-cli-core';
2025-09-02 10:15:42 -07:00
import { getErrorMessage } from '../../utils/errors.js';
2025-11-26 19:22:26 +00:00
import {
INSTALL_WARNING_MESSAGE,
requestConsentNonInteractive,
} from '../../config/extensions/consent.js';
import { ExtensionManager } from '../../config/extension-manager.js';
import { loadSettings } from '../../config/settings.js';
import { promptForSetting } from '../../config/extensions/extensionSettings.js';
import { exitCli } from '../utils.js';
2025-09-02 10:15:42 -07:00
interface InstallArgs {
path: string;
2025-11-26 19:22:26 +00:00
consent?: boolean;
2025-09-02 10:15:42 -07:00
}
export async function handleLink(args: InstallArgs) {
try {
const installMetadata: ExtensionInstallMetadata = {
source: args.path,
type: 'link',
};
2025-11-26 19:22:26 +00:00
const requestConsent = args.consent
? () => Promise.resolve(true)
: requestConsentNonInteractive;
if (args.consent) {
debugLogger.log('You have consented to the following:');
debugLogger.log(INSTALL_WARNING_MESSAGE);
}
const workspaceDir = process.cwd();
const extensionManager = new ExtensionManager({
workspaceDir,
2025-11-26 19:22:26 +00:00
requestConsent,
requestSetting: promptForSetting,
settings: loadSettings(workspaceDir).merged,
});
await extensionManager.loadExtensions();
const extension =
await extensionManager.installOrUpdateExtension(installMetadata);
debugLogger.log(
chalk.green(
`Extension "${extension.name}" linked successfully and enabled.`,
),
2025-09-02 10:15:42 -07:00
);
} catch (error) {
debugLogger.error(getErrorMessage(error));
2025-09-02 10:15:42 -07:00
process.exit(1);
}
}
export const linkCommand: CommandModule = {
command: 'link <path>',
describe:
'Links an extension from a local path. Updates made to the local path will always be reflected.',
builder: (yargs) =>
yargs
.positional('path', {
describe: 'The name of the extension to link.',
type: 'string',
})
2025-11-26 19:22:26 +00:00
.option('consent', {
describe:
'Acknowledge the security risks of installing an extension and skip the confirmation prompt.',
type: 'boolean',
default: false,
})
2025-09-02 10:15:42 -07:00
.check((_) => true),
handler: async (argv) => {
await handleLink({
2026-02-10 00:10:15 +00:00
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
2025-09-02 10:15:42 -07:00
path: argv['path'] as string,
2026-02-10 00:10:15 +00:00
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
2025-11-26 19:22:26 +00:00
consent: argv['consent'] as boolean | undefined,
2025-09-02 10:15:42 -07:00
});
await exitCli();
2025-09-02 10:15:42 -07:00
},
};