refactor: extract shared utilities (isMarkdownFile, isPlainObject, resolveSymlink) (#33)

This commit is contained in:
Junho Yeo
2025-12-13 14:23:04 +09:00
committed by GitHub
parent 60d9513d3a
commit 1323443c85
9 changed files with 42 additions and 49 deletions

View File

@@ -1,9 +1,10 @@
import { tool } from "@opencode-ai/plugin"
import { existsSync, readdirSync, lstatSync, readlinkSync, readFileSync } from "fs"
import { existsSync, readdirSync, readFileSync } from "fs"
import { homedir } from "os"
import { join, resolve, basename } from "path"
import { join, basename } from "path"
import { z } from "zod/v4"
import { parseFrontmatter, resolveCommandsInText } from "../../shared"
import { resolveSymlink } from "../../shared/file-utils"
import { SkillFrontmatterSchema } from "./types"
import type { SkillScope, SkillMetadata, SkillInfo, LoadedSkill, SkillFrontmatter } from "./types"
@@ -37,15 +38,7 @@ function discoverSkillsFromDir(
const skillPath = join(skillsDir, entry.name)
if (entry.isDirectory() || entry.isSymbolicLink()) {
let resolvedPath = skillPath
try {
const stats = lstatSync(skillPath, { throwIfNoEntry: false })
if (stats?.isSymbolicLink()) {
resolvedPath = resolve(skillPath, "..", readlinkSync(skillPath))
}
} catch {
continue
}
const resolvedPath = resolveSymlink(skillPath)
const skillMdPath = join(resolvedPath, "SKILL.md")
if (!existsSync(skillMdPath)) continue
@@ -83,18 +76,6 @@ const skillListForDescription = availableSkills
.map((s) => `- ${s.name}: ${s.description} (${s.scope})`)
.join("\n")
function resolveSymlink(skillPath: string): string {
try {
const stats = lstatSync(skillPath, { throwIfNoEntry: false })
if (stats?.isSymbolicLink()) {
return resolve(skillPath, "..", readlinkSync(skillPath))
}
return skillPath
} catch {
return skillPath
}
}
async function parseSkillMd(skillPath: string): Promise<SkillInfo | null> {
const resolvedPath = resolveSymlink(skillPath)
const skillMdPath = join(resolvedPath, "SKILL.md")

View File

@@ -3,6 +3,7 @@ import { existsSync, readdirSync, readFileSync } from "fs"
import { homedir } from "os"
import { join, basename, dirname } from "path"
import { parseFrontmatter, resolveCommandsInText, resolveFileReferencesInText, sanitizeModelField } from "../../shared"
import { isMarkdownFile } from "../../shared/file-utils"
import type { CommandScope, CommandMetadata, CommandInfo } from "./types"
function discoverCommandsFromDir(commandsDir: string, scope: CommandScope): CommandInfo[] {
@@ -14,9 +15,7 @@ function discoverCommandsFromDir(commandsDir: string, scope: CommandScope): Comm
const commands: CommandInfo[] = []
for (const entry of entries) {
if (entry.name.startsWith(".")) continue
if (!entry.name.endsWith(".md")) continue
if (!entry.isFile()) continue
if (!isMarkdownFile(entry)) continue
const commandPath = join(commandsDir, entry.name)
const commandName = basename(entry.name, ".md")