|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// Copyright (c) JFrog Ltd. 2026 |
| 4 | +// Licensed under the Apache License, Version 2.0 |
| 5 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + |
| 7 | +import { promises as fs } from "node:fs"; |
| 8 | +import path from "node:path"; |
| 9 | +import process from "node:process"; |
| 10 | + |
| 11 | +const repoRoot = process.cwd(); |
| 12 | +const errors = []; |
| 13 | +const warnings = []; |
| 14 | + |
| 15 | +const pluginNamePattern = /^[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?$/; |
| 16 | + |
| 17 | +function addError(message) { |
| 18 | + errors.push(message); |
| 19 | +} |
| 20 | + |
| 21 | +function addWarning(message) { |
| 22 | + warnings.push(message); |
| 23 | +} |
| 24 | + |
| 25 | +async function pathExists(targetPath) { |
| 26 | + try { |
| 27 | + await fs.access(targetPath); |
| 28 | + return true; |
| 29 | + } catch { |
| 30 | + return false; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +async function readJsonFile(filePath, context) { |
| 35 | + let raw; |
| 36 | + try { |
| 37 | + raw = await fs.readFile(filePath, "utf8"); |
| 38 | + } catch { |
| 39 | + addError(`${context} is missing: ${filePath}`); |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + try { |
| 44 | + return JSON.parse(raw); |
| 45 | + } catch (error) { |
| 46 | + addError(`${context} contains invalid JSON (${filePath}): ${error.message}`); |
| 47 | + return null; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function normalizeNewlines(content) { |
| 52 | + return content.replace(/\r\n/g, "\n"); |
| 53 | +} |
| 54 | + |
| 55 | +function extractFrontmatterBlock(content) { |
| 56 | + const normalized = normalizeNewlines(content); |
| 57 | + if (!normalized.startsWith("---\n")) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + const closingIndex = normalized.indexOf("\n---\n", 4); |
| 61 | + if (closingIndex === -1) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + return normalized.slice(4, closingIndex); |
| 65 | +} |
| 66 | + |
| 67 | +async function validateSkillFile(filePath, pluginName) { |
| 68 | + const content = await fs.readFile(filePath, "utf8"); |
| 69 | + const relativeFile = path.relative(repoRoot, filePath); |
| 70 | + const block = extractFrontmatterBlock(content); |
| 71 | + if (!block) { |
| 72 | + addError(`${pluginName}: skill missing YAML frontmatter: ${relativeFile}`); |
| 73 | + return; |
| 74 | + } |
| 75 | + if (!/^name:\s+/m.test(block)) { |
| 76 | + addError(`${pluginName}: skill missing "name" in frontmatter: ${relativeFile}`); |
| 77 | + } |
| 78 | + if (!/^description:\s+/m.test(block)) { |
| 79 | + addError(`${pluginName}: skill missing "description" in frontmatter: ${relativeFile}`); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +async function validateSkills(pluginDir, pluginName) { |
| 84 | + const skillsDir = path.join(pluginDir, "skills"); |
| 85 | + if (!(await pathExists(skillsDir))) { |
| 86 | + addWarning(`${pluginName}: no skills/ directory`); |
| 87 | + return; |
| 88 | + } |
| 89 | + const entries = await fs.readdir(skillsDir, { withFileTypes: true }); |
| 90 | + let foundSkill = false; |
| 91 | + for (const entry of entries) { |
| 92 | + if (!entry.isDirectory()) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + const skillMd = path.join(skillsDir, entry.name, "SKILL.md"); |
| 96 | + if (await pathExists(skillMd)) { |
| 97 | + foundSkill = true; |
| 98 | + await validateSkillFile(skillMd, pluginName); |
| 99 | + } |
| 100 | + } |
| 101 | + if (!foundSkill) { |
| 102 | + addError(`${pluginName}: no skills/*/SKILL.md found under ${path.relative(repoRoot, skillsDir)}`); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +async function main() { |
| 107 | + const manifestPath = path.join(repoRoot, ".claude-plugin", "plugin.json"); |
| 108 | + const pluginManifest = await readJsonFile(manifestPath, "Plugin manifest (.claude-plugin/plugin.json)"); |
| 109 | + if (!pluginManifest) { |
| 110 | + summarizeAndExit(); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + const pluginName = pluginManifest.name || "plugin"; |
| 115 | + if (typeof pluginManifest.name !== "string" || !pluginNamePattern.test(pluginManifest.name)) { |
| 116 | + addError( |
| 117 | + `"name" in plugin.json must be lowercase and use only alphanumerics, hyphens, and periods.` |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + await validateSkills(repoRoot, pluginName); |
| 122 | + |
| 123 | + summarizeAndExit(); |
| 124 | +} |
| 125 | + |
| 126 | +function summarizeAndExit() { |
| 127 | + if (warnings.length > 0) { |
| 128 | + console.log("Warnings:"); |
| 129 | + for (const warning of warnings) { |
| 130 | + console.log(`- ${warning}`); |
| 131 | + } |
| 132 | + console.log(""); |
| 133 | + } |
| 134 | + |
| 135 | + if (errors.length > 0) { |
| 136 | + console.error("Validation failed:"); |
| 137 | + for (const error of errors) { |
| 138 | + console.error(`- ${error}`); |
| 139 | + } |
| 140 | + process.exit(1); |
| 141 | + } |
| 142 | + |
| 143 | + console.log("Validation passed."); |
| 144 | +} |
| 145 | + |
| 146 | +await main(); |
0 commit comments