|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Validates that skill metadata descriptions are present and under the |
| 5 | + * GitHub Copilot supported limit. |
| 6 | + */ |
| 7 | + |
| 8 | +const fs = require('fs'); |
| 9 | +const path = require('path'); |
| 10 | + |
| 11 | +const ROOT = path.resolve(__dirname, '..'); |
| 12 | +const MAX_DESCRIPTION_LENGTH = 1024; |
| 13 | + |
| 14 | +function walkFiles(directory) { |
| 15 | + if (!fs.existsSync(directory)) return []; |
| 16 | + |
| 17 | + const entries = fs.readdirSync(directory, { withFileTypes: true }); |
| 18 | + const files = []; |
| 19 | + |
| 20 | + for (const entry of entries) { |
| 21 | + if (entry.name === '.git' || entry.name === 'node_modules') continue; |
| 22 | + |
| 23 | + const entryPath = path.join(directory, entry.name); |
| 24 | + if (entry.isDirectory()) { |
| 25 | + files.push(...walkFiles(entryPath)); |
| 26 | + } else { |
| 27 | + files.push(entryPath); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + return files; |
| 32 | +} |
| 33 | + |
| 34 | +function isSkillMetadataFile(filePath) { |
| 35 | + const normalized = path.relative(ROOT, filePath).split(path.sep).join('/'); |
| 36 | + return ( |
| 37 | + normalized.includes('/skills/') && |
| 38 | + (normalized.endsWith('/SKILL.md') || normalized.endsWith('/SKILL.template.md')) |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +function getFrontmatter(content) { |
| 43 | + const match = content.match(/^---[ \t]*\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/); |
| 44 | + if (!match) return null; |
| 45 | + |
| 46 | + const startLine = content.slice(0, match.index).split(/\r?\n/).length; |
| 47 | + return { |
| 48 | + body: match[1], |
| 49 | + startLine, |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +function countIndent(line) { |
| 54 | + const match = line.match(/^[ \t]*/); |
| 55 | + return match ? match[0].length : 0; |
| 56 | +} |
| 57 | + |
| 58 | +function stripBlockIndent(lines) { |
| 59 | + const indents = lines |
| 60 | + .filter((line) => line.trim() !== '') |
| 61 | + .map((line) => countIndent(line)); |
| 62 | + const indent = indents.length > 0 ? Math.min(...indents) : 0; |
| 63 | + |
| 64 | + return lines.map((line) => { |
| 65 | + if (line.trim() === '') return ''; |
| 66 | + return line.slice(Math.min(indent, countIndent(line))); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +function foldBlockLines(lines) { |
| 71 | + let value = ''; |
| 72 | + |
| 73 | + for (let index = 0; index < lines.length; index += 1) { |
| 74 | + const line = lines[index]; |
| 75 | + if (index > 0) { |
| 76 | + const previousLine = lines[index - 1]; |
| 77 | + value += previousLine === '' || line === '' ? '\n' : ' '; |
| 78 | + } |
| 79 | + value += line; |
| 80 | + } |
| 81 | + |
| 82 | + return value; |
| 83 | +} |
| 84 | + |
| 85 | +function unquoteInlineValue(value) { |
| 86 | + const trimmed = value.trim(); |
| 87 | + if (trimmed.length < 2) return trimmed; |
| 88 | + |
| 89 | + const quote = trimmed[0]; |
| 90 | + if ((quote !== '"' && quote !== "'") || trimmed[trimmed.length - 1] !== quote) { |
| 91 | + return trimmed; |
| 92 | + } |
| 93 | + |
| 94 | + const unquoted = trimmed.slice(1, -1); |
| 95 | + if (quote === "'") { |
| 96 | + return unquoted.replace(/''/g, "'"); |
| 97 | + } |
| 98 | + |
| 99 | + return unquoted |
| 100 | + .replace(/\\"/g, '"') |
| 101 | + .replace(/\\n/g, '\n') |
| 102 | + .replace(/\\t/g, '\t') |
| 103 | + .replace(/\\\\/g, '\\'); |
| 104 | +} |
| 105 | + |
| 106 | +function parseDescription(frontmatter) { |
| 107 | + const lines = frontmatter.body.split(/\r?\n/); |
| 108 | + |
| 109 | + for (let index = 0; index < lines.length; index += 1) { |
| 110 | + const line = lines[index]; |
| 111 | + const match = line.match(/^description\s*:\s*(.*)$/); |
| 112 | + if (!match) continue; |
| 113 | + |
| 114 | + const rawValue = match[1].trim(); |
| 115 | + const lineNumber = frontmatter.startLine + index + 1; |
| 116 | + const blockMatch = rawValue.match(/^([>|])/); |
| 117 | + |
| 118 | + if (!blockMatch) { |
| 119 | + return { |
| 120 | + value: unquoteInlineValue(rawValue), |
| 121 | + lineNumber, |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + const blockLines = []; |
| 126 | + const parentIndent = countIndent(line); |
| 127 | + const explicitIndentMatch = rawValue.match(/^[>|]([1-9])/); |
| 128 | + let blockIndent = explicitIndentMatch |
| 129 | + ? parentIndent + Number(explicitIndentMatch[1]) |
| 130 | + : null; |
| 131 | + |
| 132 | + for (let blockIndex = index + 1; blockIndex < lines.length; blockIndex += 1) { |
| 133 | + const blockLine = lines[blockIndex]; |
| 134 | + if (blockLine.trim() !== '') { |
| 135 | + const lineIndent = countIndent(blockLine); |
| 136 | + if (blockIndent === null) { |
| 137 | + if (lineIndent <= parentIndent) break; |
| 138 | + blockIndent = lineIndent; |
| 139 | + } else if (lineIndent < blockIndent) { |
| 140 | + break; |
| 141 | + } |
| 142 | + } |
| 143 | + blockLines.push(blockLine); |
| 144 | + } |
| 145 | + |
| 146 | + const strippedLines = stripBlockIndent(blockLines); |
| 147 | + const value = |
| 148 | + blockMatch[1] === '|' |
| 149 | + ? strippedLines.join('\n').trimEnd() |
| 150 | + : foldBlockLines(strippedLines).trimEnd(); |
| 151 | + |
| 152 | + return { |
| 153 | + value, |
| 154 | + lineNumber, |
| 155 | + }; |
| 156 | + } |
| 157 | + |
| 158 | + return null; |
| 159 | +} |
| 160 | + |
| 161 | +const skillFiles = walkFiles(ROOT).filter(isSkillMetadataFile).sort(); |
| 162 | +const errors = []; |
| 163 | + |
| 164 | +for (const filePath of skillFiles) { |
| 165 | + const relativePath = path.relative(ROOT, filePath); |
| 166 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 167 | + const frontmatter = getFrontmatter(content); |
| 168 | + |
| 169 | + if (!frontmatter) { |
| 170 | + errors.push(`${relativePath}: missing YAML frontmatter`); |
| 171 | + continue; |
| 172 | + } |
| 173 | + |
| 174 | + const description = parseDescription(frontmatter); |
| 175 | + if (!description) { |
| 176 | + errors.push(`${relativePath}: missing description in YAML frontmatter`); |
| 177 | + continue; |
| 178 | + } |
| 179 | + |
| 180 | + const descriptionLength = Array.from(description.value).length; |
| 181 | + if (descriptionLength >= MAX_DESCRIPTION_LENGTH) { |
| 182 | + errors.push( |
| 183 | + `${relativePath}:${description.lineNumber}: description is ${descriptionLength} characters; ` + |
| 184 | + `must be fewer than ${MAX_DESCRIPTION_LENGTH} characters because GitHub Copilot ` + |
| 185 | + `does not support longer skill descriptions` |
| 186 | + ); |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +if (errors.length > 0) { |
| 191 | + console.log('Found invalid skill descriptions:'); |
| 192 | + for (const error of errors) { |
| 193 | + console.log(`- ${error}`); |
| 194 | + } |
| 195 | + process.exit(1); |
| 196 | +} |
| 197 | + |
| 198 | +console.log( |
| 199 | + `Validated ${skillFiles.length} skill metadata file(s); all descriptions are under ${MAX_DESCRIPTION_LENGTH} characters.` |
| 200 | +); |
0 commit comments