UNPKG

ccgo

Version:

Simple Claude Code launcher with config management and environment variable injection

120 lines 4.44 kB
"use strict"; /** * Claude Code 初始化工具 * 用于设置 ~/.claude.json 配置文件,解决首次启动时的 API 连接问题 */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.isClaudeConfigInitialized = isClaudeConfigInitialized; exports.initializeClaudeConfig = initializeClaudeConfig; exports.ensureClaudeConfigInitialized = ensureClaudeConfigInitialized; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const os = __importStar(require("os")); /** * 获取 Claude 配置文件路径 */ function getClaudeConfigPath() { return path.join(os.homedir(), '.claude.json'); } /** * 检查 Claude 配置是否已初始化 * @returns 如果配置已存在且 hasCompletedOnboarding 为 true,返回 true */ function isClaudeConfigInitialized() { const configPath = getClaudeConfigPath(); try { if (!fs.existsSync(configPath)) { return false; } const content = fs.readFileSync(configPath, 'utf-8'); const config = JSON.parse(content); // 检查是否已完成引导流程 return config.hasCompletedOnboarding === true; } catch (error) { // 如果文件损坏或解析失败,视为未初始化 return false; } } /** * 初始化 Claude 配置文件 * 设置 hasCompletedOnboarding: true,避免首次启动时的 API 连接问题 * * @description * Claude Code 首次启动时会执行 onboarding 流程,在这个流程中会尝试连接官方 API (api.anthropic.com) * 进行验证,这发生在读取环境变量之前。通过设置 hasCompletedOnboarding: true, * 可以跳过这个流程,直接使用注入的环境变量(ANTHROPIC_API_KEY 和 ANTHROPIC_BASE_URL)。 * * @returns 返回是否执行了初始化操作 */ function initializeClaudeConfig() { const configPath = getClaudeConfigPath(); try { let config = {}; // 如果配置文件已存在,读取现有配置 if (fs.existsSync(configPath)) { const content = fs.readFileSync(configPath, 'utf-8'); config = JSON.parse(content); // 如果已经配置正确,无需重复操作 if (config.hasCompletedOnboarding === true) { return false; } } // 设置 hasCompletedOnboarding: true config.hasCompletedOnboarding = true; // 写入配置文件 fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8'); return true; } catch (error) { // 初始化失败不应阻塞启动流程,只记录错误 console.error('Warning: Failed to initialize Claude config:', error); return false; } } /** * 确保 Claude 配置已初始化 * 如果未初始化,则自动执行初始化 * * @returns 返回是否执行了初始化操作 */ function ensureClaudeConfigInitialized() { if (isClaudeConfigInitialized()) { return false; } return initializeClaudeConfig(); } //# sourceMappingURL=claude-init.js.map