mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-29 22:44:45 -07:00
feat(cli): add gemini extensions list --output-format=json (#14479)
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp> Co-authored-by: Bryan Morgan <bryanmorgan@google.com>
This commit is contained in:
@@ -78,6 +78,17 @@ describe('extensions list command', () => {
|
|||||||
mockCwd.mockRestore();
|
mockCwd.mockRestore();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should output empty JSON array if no extensions are installed and output-format is json', async () => {
|
||||||
|
const mockCwd = vi.spyOn(process, 'cwd').mockReturnValue('/test/dir');
|
||||||
|
mockExtensionManager.prototype.loadExtensions = vi
|
||||||
|
.fn()
|
||||||
|
.mockResolvedValue([]);
|
||||||
|
await handleList({ outputFormat: 'json' });
|
||||||
|
|
||||||
|
expect(emitConsoleLog).toHaveBeenCalledWith('log', '[]');
|
||||||
|
mockCwd.mockRestore();
|
||||||
|
});
|
||||||
|
|
||||||
it('should list all installed extensions', async () => {
|
it('should list all installed extensions', async () => {
|
||||||
const mockCwd = vi.spyOn(process, 'cwd').mockReturnValue('/test/dir');
|
const mockCwd = vi.spyOn(process, 'cwd').mockReturnValue('/test/dir');
|
||||||
const extensions = [
|
const extensions = [
|
||||||
@@ -99,6 +110,24 @@ describe('extensions list command', () => {
|
|||||||
mockCwd.mockRestore();
|
mockCwd.mockRestore();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should list all installed extensions in JSON format', async () => {
|
||||||
|
const mockCwd = vi.spyOn(process, 'cwd').mockReturnValue('/test/dir');
|
||||||
|
const extensions = [
|
||||||
|
{ name: 'ext1', version: '1.0.0' },
|
||||||
|
{ name: 'ext2', version: '2.0.0' },
|
||||||
|
];
|
||||||
|
mockExtensionManager.prototype.loadExtensions = vi
|
||||||
|
.fn()
|
||||||
|
.mockResolvedValue(extensions);
|
||||||
|
await handleList({ outputFormat: 'json' });
|
||||||
|
|
||||||
|
expect(emitConsoleLog).toHaveBeenCalledWith(
|
||||||
|
'log',
|
||||||
|
JSON.stringify(extensions, null, 2),
|
||||||
|
);
|
||||||
|
mockCwd.mockRestore();
|
||||||
|
});
|
||||||
|
|
||||||
it('should log an error message and exit with code 1 when listing fails', async () => {
|
it('should log an error message and exit with code 1 when listing fails', async () => {
|
||||||
const mockProcessExit = vi
|
const mockProcessExit = vi
|
||||||
.spyOn(process, 'exit')
|
.spyOn(process, 'exit')
|
||||||
@@ -130,11 +159,35 @@ describe('extensions list command', () => {
|
|||||||
expect(command.describe).toBe('Lists installed extensions.');
|
expect(command.describe).toBe('Lists installed extensions.');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('handler should call handleList', async () => {
|
it('builder should have output-format option', () => {
|
||||||
|
const mockYargs = {
|
||||||
|
option: vi.fn().mockReturnThis(),
|
||||||
|
};
|
||||||
|
(
|
||||||
|
command.builder as unknown as (
|
||||||
|
yargs: typeof mockYargs,
|
||||||
|
) => typeof mockYargs
|
||||||
|
)(mockYargs);
|
||||||
|
expect(mockYargs.option).toHaveBeenCalledWith('output-format', {
|
||||||
|
alias: 'o',
|
||||||
|
type: 'string',
|
||||||
|
describe: 'The format of the CLI output.',
|
||||||
|
choices: ['text', 'json'],
|
||||||
|
default: 'text',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handler should call handleList with parsed arguments', async () => {
|
||||||
mockExtensionManager.prototype.loadExtensions = vi
|
mockExtensionManager.prototype.loadExtensions = vi
|
||||||
.fn()
|
.fn()
|
||||||
.mockResolvedValue([]);
|
.mockResolvedValue([]);
|
||||||
await (command.handler as () => Promise<void>)();
|
await (
|
||||||
|
command.handler as unknown as (args: {
|
||||||
|
'output-format': string;
|
||||||
|
}) => Promise<void>
|
||||||
|
)({
|
||||||
|
'output-format': 'json',
|
||||||
|
});
|
||||||
expect(mockExtensionManager.prototype.loadExtensions).toHaveBeenCalled();
|
expect(mockExtensionManager.prototype.loadExtensions).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import { loadSettings } from '../../config/settings.js';
|
|||||||
import { promptForSetting } from '../../config/extensions/extensionSettings.js';
|
import { promptForSetting } from '../../config/extensions/extensionSettings.js';
|
||||||
import { exitCli } from '../utils.js';
|
import { exitCli } from '../utils.js';
|
||||||
|
|
||||||
export async function handleList() {
|
export async function handleList(options?: { outputFormat?: 'text' | 'json' }) {
|
||||||
try {
|
try {
|
||||||
const workspaceDir = process.cwd();
|
const workspaceDir = process.cwd();
|
||||||
const extensionManager = new ExtensionManager({
|
const extensionManager = new ExtensionManager({
|
||||||
@@ -24,16 +24,25 @@ export async function handleList() {
|
|||||||
});
|
});
|
||||||
const extensions = await extensionManager.loadExtensions();
|
const extensions = await extensionManager.loadExtensions();
|
||||||
if (extensions.length === 0) {
|
if (extensions.length === 0) {
|
||||||
debugLogger.log('No extensions installed.');
|
if (options?.outputFormat === 'json') {
|
||||||
|
debugLogger.log('[]');
|
||||||
|
} else {
|
||||||
|
debugLogger.log('No extensions installed.');
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
debugLogger.log(
|
|
||||||
extensions
|
if (options?.outputFormat === 'json') {
|
||||||
.map((extension, _): string =>
|
debugLogger.log(JSON.stringify(extensions, null, 2));
|
||||||
extensionManager.toOutputString(extension),
|
} else {
|
||||||
)
|
debugLogger.log(
|
||||||
.join('\n\n'),
|
extensions
|
||||||
);
|
.map((extension, _): string =>
|
||||||
|
extensionManager.toOutputString(extension),
|
||||||
|
)
|
||||||
|
.join('\n\n'),
|
||||||
|
);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
debugLogger.error(getErrorMessage(error));
|
debugLogger.error(getErrorMessage(error));
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
@@ -43,9 +52,18 @@ export async function handleList() {
|
|||||||
export const listCommand: CommandModule = {
|
export const listCommand: CommandModule = {
|
||||||
command: 'list',
|
command: 'list',
|
||||||
describe: 'Lists installed extensions.',
|
describe: 'Lists installed extensions.',
|
||||||
builder: (yargs) => yargs,
|
builder: (yargs) =>
|
||||||
handler: async () => {
|
yargs.option('output-format', {
|
||||||
await handleList();
|
alias: 'o',
|
||||||
|
type: 'string',
|
||||||
|
describe: 'The format of the CLI output.',
|
||||||
|
choices: ['text', 'json'],
|
||||||
|
default: 'text',
|
||||||
|
}),
|
||||||
|
handler: async (argv) => {
|
||||||
|
await handleList({
|
||||||
|
outputFormat: argv['output-format'] as 'text' | 'json',
|
||||||
|
});
|
||||||
await exitCli();
|
await exitCli();
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user