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

137 lines
3.9 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { CommandModule } from 'yargs';
import {
debugLogger,
type ExtensionInstallMetadata,
} from '@google/gemini-cli-core';
2025-08-26 14:36:55 +00:00
import { getErrorMessage } from '../../utils/errors.js';
import { stat } from 'node:fs/promises';
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-08-26 14:36:55 +00:00
interface InstallArgs {
source: string;
ref?: string;
autoUpdate?: boolean;
2025-10-08 14:26:12 -07:00
allowPreRelease?: boolean;
2025-10-16 11:01:17 -04:00
consent?: boolean;
}
export async function handleInstall(args: InstallArgs) {
try {
let installMetadata: ExtensionInstallMetadata;
const { source } = args;
if (
source.startsWith('http://') ||
source.startsWith('https://') ||
source.startsWith('git@') ||
source.startsWith('sso://')
) {
installMetadata = {
source,
type: 'git',
ref: args.ref,
autoUpdate: args.autoUpdate,
2025-10-08 14:26:12 -07:00
allowPreRelease: args.allowPreRelease,
};
} else {
if (args.ref || args.autoUpdate) {
throw new Error(
'--ref and --auto-update are not applicable for local extensions.',
);
}
try {
await stat(source);
installMetadata = {
source,
type: 'local',
};
} catch {
throw new Error('Install source not found.');
}
}
2025-10-16 11:01:17 -04: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);
2025-10-16 11:01:17 -04:00
}
const workspaceDir = process.cwd();
const extensionManager = new ExtensionManager({
workspaceDir,
2025-10-16 11:01:17 -04:00
requestConsent,
requestSetting: promptForSetting,
settings: loadSettings(workspaceDir).merged,
});
await extensionManager.loadExtensions();
const extension =
await extensionManager.installOrUpdateExtension(installMetadata);
debugLogger.log(
`Extension "${extension.name}" installed successfully and enabled.`,
);
} catch (error) {
debugLogger.error(getErrorMessage(error));
process.exit(1);
}
}
export const installCommand: CommandModule = {
2025-10-08 14:26:12 -07:00
command: 'install <source> [--auto-update] [--pre-release]',
2025-09-08 21:59:45 -07:00
describe: 'Installs an extension from a git repository URL or a local path.',
builder: (yargs) =>
yargs
.positional('source', {
describe: 'The github URL or local path of the extension to install.',
type: 'string',
demandOption: true,
})
.option('ref', {
describe: 'The git ref to install from.',
type: 'string',
})
.option('auto-update', {
describe: 'Enable auto-update for this extension.',
type: 'boolean',
})
2025-10-08 14:26:12 -07:00
.option('pre-release', {
describe: 'Enable pre-release versions for this extension.',
type: 'boolean',
})
2025-10-16 11:01:17 -04:00
.option('consent', {
describe:
'Acknowledge the security risks of installing an extension and skip the confirmation prompt.',
type: 'boolean',
default: false,
})
.check((argv) => {
if (!argv.source) {
throw new Error('The source argument must be provided.');
}
return true;
}),
handler: async (argv) => {
await handleInstall({
source: argv['source'] as string,
ref: argv['ref'] as string | undefined,
autoUpdate: argv['auto-update'] as boolean | undefined,
2025-10-08 14:26:12 -07:00
allowPreRelease: argv['pre-release'] as boolean | undefined,
2025-10-16 11:01:17 -04:00
consent: argv['consent'] as boolean | undefined,
});
await exitCli();
},
};