mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-03-17 09:30:58 -07:00
refactor(core): Move getPackageJson utility to core package (#12224)
This commit is contained in:
1
package-lock.json
generated
1
package-lock.json
generated
@@ -17868,6 +17868,7 @@
|
||||
"mnemonist": "^0.40.3",
|
||||
"open": "^10.1.2",
|
||||
"picomatch": "^4.0.1",
|
||||
"read-package-up": "^11.0.0",
|
||||
"shell-quote": "^1.8.3",
|
||||
"simple-git": "^3.28.0",
|
||||
"strip-ansi": "^7.1.0",
|
||||
|
||||
@@ -4,19 +4,25 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type { SandboxConfig } from '@google/gemini-cli-core';
|
||||
import { FatalSandboxError } from '@google/gemini-cli-core';
|
||||
import {
|
||||
getPackageJson,
|
||||
type SandboxConfig,
|
||||
FatalSandboxError,
|
||||
} from '@google/gemini-cli-core';
|
||||
import commandExists from 'command-exists';
|
||||
import * as os from 'node:os';
|
||||
import { getPackageJson } from '../utils/package.js';
|
||||
import type { Settings } from './settings.js';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import path from 'node:path';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
// This is a stripped-down version of the CliArgs interface from config.ts
|
||||
// to avoid circular dependencies.
|
||||
interface SandboxCliArgs {
|
||||
sandbox?: boolean | string;
|
||||
}
|
||||
|
||||
const VALID_SANDBOX_COMMANDS: ReadonlyArray<SandboxConfig['command']> = [
|
||||
'docker',
|
||||
'podman',
|
||||
@@ -94,7 +100,7 @@ export async function loadSandboxConfig(
|
||||
const sandboxOption = argv.sandbox ?? settings.tools?.sandbox;
|
||||
const command = getSandboxCommand(sandboxOption);
|
||||
|
||||
const packageJson = await getPackageJson();
|
||||
const packageJson = await getPackageJson(__dirname);
|
||||
const image =
|
||||
process.env['GEMINI_SANDBOX_IMAGE'] ?? packageJson?.config?.sandboxImageUri;
|
||||
|
||||
|
||||
@@ -9,8 +9,12 @@ import { checkForUpdates } from './updateCheck.js';
|
||||
import type { LoadedSettings } from '../../config/settings.js';
|
||||
|
||||
const getPackageJson = vi.hoisted(() => vi.fn());
|
||||
vi.mock('../../utils/package.js', () => ({
|
||||
const debugLogger = vi.hoisted(() => ({
|
||||
warn: vi.fn(),
|
||||
}));
|
||||
vi.mock('@google/gemini-cli-core', () => ({
|
||||
getPackageJson,
|
||||
debugLogger,
|
||||
}));
|
||||
|
||||
const latestVersion = vi.hoisted(() => vi.fn());
|
||||
|
||||
@@ -6,9 +6,13 @@
|
||||
|
||||
import latestVersion from 'latest-version';
|
||||
import semver from 'semver';
|
||||
import { getPackageJson } from '../../utils/package.js';
|
||||
import { getPackageJson, debugLogger } from '@google/gemini-cli-core';
|
||||
import type { LoadedSettings } from '../../config/settings.js';
|
||||
import { debugLogger } from '@google/gemini-cli-core';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import path from 'node:path';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
export const FETCH_TIMEOUT_MS = 2000;
|
||||
|
||||
@@ -54,7 +58,7 @@ export async function checkForUpdates(
|
||||
if (process.env['DEV'] === 'true') {
|
||||
return null;
|
||||
}
|
||||
const packageJson = await getPackageJson();
|
||||
const packageJson = await getPackageJson(__dirname);
|
||||
if (!packageJson || !packageJson.name || !packageJson.version) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import {
|
||||
readPackageUp,
|
||||
type PackageJson as BasePackageJson,
|
||||
} from 'read-package-up';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import path from 'node:path';
|
||||
|
||||
export type PackageJson = BasePackageJson & {
|
||||
config?: {
|
||||
sandboxImageUri?: string;
|
||||
};
|
||||
};
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
let packageJson: PackageJson | undefined;
|
||||
|
||||
export async function getPackageJson(): Promise<PackageJson | undefined> {
|
||||
if (packageJson) {
|
||||
return packageJson;
|
||||
}
|
||||
|
||||
const result = await readPackageUp({ cwd: __dirname });
|
||||
if (!result) {
|
||||
// TODO: Maybe bubble this up as an error.
|
||||
return;
|
||||
}
|
||||
|
||||
packageJson = result.packageJson;
|
||||
return packageJson;
|
||||
}
|
||||
@@ -4,9 +4,14 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { getPackageJson } from './package.js';
|
||||
import { getPackageJson } from '@google/gemini-cli-core';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import path from 'node:path';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
export async function getCliVersion(): Promise<string> {
|
||||
const pkgJson = await getPackageJson();
|
||||
const pkgJson = await getPackageJson(__dirname);
|
||||
return process.env['CLI_VERSION'] || pkgJson?.version || 'unknown';
|
||||
}
|
||||
|
||||
@@ -58,6 +58,7 @@
|
||||
"mnemonist": "^0.40.3",
|
||||
"open": "^10.1.2",
|
||||
"picomatch": "^4.0.1",
|
||||
"read-package-up": "^11.0.0",
|
||||
"shell-quote": "^1.8.3",
|
||||
"simple-git": "^3.28.0",
|
||||
"strip-ansi": "^7.1.0",
|
||||
|
||||
@@ -68,6 +68,7 @@ export * from './utils/thoughtUtils.js';
|
||||
export * from './utils/debugLogger.js';
|
||||
export * from './utils/events.js';
|
||||
export * from './utils/extensionLoader.js';
|
||||
export * from './utils/package.js';
|
||||
|
||||
// Export services
|
||||
export * from './services/fileDiscoveryService.js';
|
||||
|
||||
28
packages/core/src/utils/package.ts
Normal file
28
packages/core/src/utils/package.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import {
|
||||
readPackageUp,
|
||||
type PackageJson as BasePackageJson,
|
||||
} from 'read-package-up';
|
||||
|
||||
export type PackageJson = BasePackageJson & {
|
||||
config?: {
|
||||
sandboxImageUri?: string;
|
||||
};
|
||||
};
|
||||
|
||||
export async function getPackageJson(
|
||||
cwd: string,
|
||||
): Promise<PackageJson | undefined> {
|
||||
const result = await readPackageUp({ cwd });
|
||||
if (!result) {
|
||||
// TODO: Maybe bubble this up as an error.
|
||||
return;
|
||||
}
|
||||
|
||||
return result.packageJson;
|
||||
}
|
||||
Reference in New Issue
Block a user