Fixes #255 - Add getClaudeConfigDir() utility function that respects CLAUDE_CONFIG_DIR env var - Update all hardcoded ~/.claude paths to use the new utility - Add comprehensive tests for getClaudeConfigDir() - Maintain backward compatibility with default ~/.claude when env var is not set Files updated: - src/shared/claude-config-dir.ts (new utility) - src/shared/claude-config-dir.test.ts (tests) - src/hooks/claude-code-hooks/config.ts - src/hooks/claude-code-hooks/todo.ts - src/hooks/claude-code-hooks/transcript.ts - src/features/claude-code-command-loader/loader.ts - src/features/claude-code-agent-loader/loader.ts - src/features/claude-code-skill-loader/loader.ts - src/features/claude-code-mcp-loader/loader.ts - src/tools/session-manager/constants.ts - src/tools/slashcommand/tools.ts Co-authored-by: sisyphus-dev-ai <sisyphus-dev-ai@users.noreply.github.com>
87 lines
2.7 KiB
TypeScript
87 lines
2.7 KiB
TypeScript
import { existsSync, readdirSync, readFileSync } from "fs"
|
|
import { join } from "path"
|
|
import { parseFrontmatter } from "../../shared/frontmatter"
|
|
import { sanitizeModelField } from "../../shared/model-sanitizer"
|
|
import { resolveSymlink } from "../../shared/file-utils"
|
|
import { getClaudeConfigDir } from "../../shared"
|
|
import type { CommandDefinition } from "../claude-code-command-loader/types"
|
|
import type { SkillScope, SkillMetadata, LoadedSkillAsCommand } from "./types"
|
|
|
|
function loadSkillsFromDir(skillsDir: string, scope: SkillScope): LoadedSkillAsCommand[] {
|
|
if (!existsSync(skillsDir)) {
|
|
return []
|
|
}
|
|
|
|
const entries = readdirSync(skillsDir, { withFileTypes: true })
|
|
const skills: LoadedSkillAsCommand[] = []
|
|
|
|
for (const entry of entries) {
|
|
if (entry.name.startsWith(".")) continue
|
|
|
|
const skillPath = join(skillsDir, entry.name)
|
|
|
|
if (!entry.isDirectory() && !entry.isSymbolicLink()) continue
|
|
|
|
const resolvedPath = resolveSymlink(skillPath)
|
|
|
|
const skillMdPath = join(resolvedPath, "SKILL.md")
|
|
if (!existsSync(skillMdPath)) continue
|
|
|
|
try {
|
|
const content = readFileSync(skillMdPath, "utf-8")
|
|
const { data, body } = parseFrontmatter<SkillMetadata>(content)
|
|
|
|
const skillName = data.name || entry.name
|
|
const originalDescription = data.description || ""
|
|
const formattedDescription = `(${scope} - Skill) ${originalDescription}`
|
|
|
|
const wrappedTemplate = `<skill-instruction>
|
|
Base directory for this skill: ${resolvedPath}/
|
|
File references (@path) in this skill are relative to this directory.
|
|
|
|
${body.trim()}
|
|
</skill-instruction>
|
|
|
|
<user-request>
|
|
$ARGUMENTS
|
|
</user-request>`
|
|
|
|
const definition: CommandDefinition = {
|
|
name: skillName,
|
|
description: formattedDescription,
|
|
template: wrappedTemplate,
|
|
model: sanitizeModelField(data.model),
|
|
}
|
|
|
|
skills.push({
|
|
name: skillName,
|
|
path: resolvedPath,
|
|
definition,
|
|
scope,
|
|
})
|
|
} catch {
|
|
continue
|
|
}
|
|
}
|
|
|
|
return skills
|
|
}
|
|
|
|
export function loadUserSkillsAsCommands(): Record<string, CommandDefinition> {
|
|
const userSkillsDir = join(getClaudeConfigDir(), "skills")
|
|
const skills = loadSkillsFromDir(userSkillsDir, "user")
|
|
return skills.reduce((acc, skill) => {
|
|
acc[skill.name] = skill.definition
|
|
return acc
|
|
}, {} as Record<string, CommandDefinition>)
|
|
}
|
|
|
|
export function loadProjectSkillsAsCommands(): Record<string, CommandDefinition> {
|
|
const projectSkillsDir = join(process.cwd(), ".claude", "skills")
|
|
const skills = loadSkillsFromDir(projectSkillsDir, "project")
|
|
return skills.reduce((acc, skill) => {
|
|
acc[skill.name] = skill.definition
|
|
return acc
|
|
}, {} as Record<string, CommandDefinition>)
|
|
}
|