feat: add detect-tools utility and Gemini sync with tests
This commit is contained in:
76
src/sync/gemini.ts
Normal file
76
src/sync/gemini.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import fs from "fs/promises"
|
||||
import path from "path"
|
||||
import type { ClaudeHomeConfig } from "../parsers/claude-home"
|
||||
import type { ClaudeMcpServer } from "../types/claude"
|
||||
import { forceSymlink, isValidSkillName } from "../utils/symlink"
|
||||
|
||||
type GeminiMcpServer = {
|
||||
command?: string
|
||||
args?: string[]
|
||||
url?: string
|
||||
env?: Record<string, string>
|
||||
headers?: Record<string, string>
|
||||
}
|
||||
|
||||
export async function syncToGemini(
|
||||
config: ClaudeHomeConfig,
|
||||
outputRoot: string,
|
||||
): Promise<void> {
|
||||
const skillsDir = path.join(outputRoot, "skills")
|
||||
await fs.mkdir(skillsDir, { recursive: true })
|
||||
|
||||
for (const skill of config.skills) {
|
||||
if (!isValidSkillName(skill.name)) {
|
||||
console.warn(`Skipping skill with invalid name: ${skill.name}`)
|
||||
continue
|
||||
}
|
||||
const target = path.join(skillsDir, skill.name)
|
||||
await forceSymlink(skill.sourceDir, target)
|
||||
}
|
||||
|
||||
if (Object.keys(config.mcpServers).length > 0) {
|
||||
const settingsPath = path.join(outputRoot, "settings.json")
|
||||
const existing = await readJsonSafe(settingsPath)
|
||||
const converted = convertMcpForGemini(config.mcpServers)
|
||||
const existingMcp =
|
||||
existing.mcpServers && typeof existing.mcpServers === "object"
|
||||
? (existing.mcpServers as Record<string, unknown>)
|
||||
: {}
|
||||
const merged = {
|
||||
...existing,
|
||||
mcpServers: { ...existingMcp, ...converted },
|
||||
}
|
||||
await fs.writeFile(settingsPath, JSON.stringify(merged, null, 2), { mode: 0o600 })
|
||||
}
|
||||
}
|
||||
|
||||
async function readJsonSafe(filePath: string): Promise<Record<string, unknown>> {
|
||||
try {
|
||||
const content = await fs.readFile(filePath, "utf-8")
|
||||
return JSON.parse(content) as Record<string, unknown>
|
||||
} catch (err) {
|
||||
if ((err as NodeJS.ErrnoException).code === "ENOENT") {
|
||||
return {}
|
||||
}
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
function convertMcpForGemini(
|
||||
servers: Record<string, ClaudeMcpServer>,
|
||||
): Record<string, GeminiMcpServer> {
|
||||
const result: Record<string, GeminiMcpServer> = {}
|
||||
for (const [name, server] of Object.entries(servers)) {
|
||||
const entry: GeminiMcpServer = {}
|
||||
if (server.command) {
|
||||
entry.command = server.command
|
||||
if (server.args && server.args.length > 0) entry.args = server.args
|
||||
if (server.env && Object.keys(server.env).length > 0) entry.env = server.env
|
||||
} else if (server.url) {
|
||||
entry.url = server.url
|
||||
if (server.headers && Object.keys(server.headers).length > 0) entry.headers = server.headers
|
||||
}
|
||||
result[name] = entry
|
||||
}
|
||||
return result
|
||||
}
|
||||
46
src/utils/detect-tools.ts
Normal file
46
src/utils/detect-tools.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import os from "os"
|
||||
import path from "path"
|
||||
import { pathExists } from "./files"
|
||||
|
||||
export type DetectedTool = {
|
||||
name: string
|
||||
detected: boolean
|
||||
reason: string
|
||||
}
|
||||
|
||||
export async function detectInstalledTools(
|
||||
home: string = os.homedir(),
|
||||
cwd: string = process.cwd(),
|
||||
): Promise<DetectedTool[]> {
|
||||
const checks: Array<{ name: string; paths: string[] }> = [
|
||||
{ name: "opencode", paths: [path.join(home, ".config", "opencode"), path.join(cwd, ".opencode")] },
|
||||
{ name: "codex", paths: [path.join(home, ".codex")] },
|
||||
{ name: "droid", paths: [path.join(home, ".factory")] },
|
||||
{ name: "cursor", paths: [path.join(cwd, ".cursor"), path.join(home, ".cursor")] },
|
||||
{ name: "pi", paths: [path.join(home, ".pi")] },
|
||||
{ name: "gemini", paths: [path.join(cwd, ".gemini"), path.join(home, ".gemini")] },
|
||||
]
|
||||
|
||||
const results: DetectedTool[] = []
|
||||
for (const check of checks) {
|
||||
let detected = false
|
||||
let reason = "not found"
|
||||
for (const p of check.paths) {
|
||||
if (await pathExists(p)) {
|
||||
detected = true
|
||||
reason = `found ${p}`
|
||||
break
|
||||
}
|
||||
}
|
||||
results.push({ name: check.name, detected, reason })
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
export async function getDetectedTargetNames(
|
||||
home: string = os.homedir(),
|
||||
cwd: string = process.cwd(),
|
||||
): Promise<string[]> {
|
||||
const tools = await detectInstalledTools(home, cwd)
|
||||
return tools.filter((t) => t.detected).map((t) => t.name)
|
||||
}
|
||||
Reference in New Issue
Block a user