|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Package the hackmd-cli skill into a .skill file (zip format) |
| 4 | + * |
| 5 | + * Usage: node scripts/package-skill.mjs |
| 6 | + */ |
| 7 | + |
| 8 | +import {execSync} from 'node:child_process'; |
| 9 | +import fs from 'node:fs'; |
| 10 | +import path from 'node:path'; |
| 11 | +import {fileURLToPath} from 'node:url'; |
| 12 | + |
| 13 | +const __filename = fileURLToPath(import.meta.url); |
| 14 | +const __dirname = path.dirname(__filename); |
| 15 | + |
| 16 | +const SKILL_DIR = path.join(__dirname, '..', 'hackmd-cli'); |
| 17 | +const SKILL_MD = path.join(SKILL_DIR, 'SKILL.md'); |
| 18 | +const OUTPUT_FILE = path.join(__dirname, '..', 'hackmd-cli.skill'); |
| 19 | + |
| 20 | +function validateSkill() { |
| 21 | + // Check SKILL.md exists |
| 22 | + if (!fs.existsSync(SKILL_MD)) { |
| 23 | + throw new Error('SKILL.md not found in hackmd-cli/'); |
| 24 | + } |
| 25 | + |
| 26 | + const content = fs.readFileSync(SKILL_MD, 'utf8'); |
| 27 | + |
| 28 | + // Check frontmatter exists |
| 29 | + if (!content.startsWith('---')) { |
| 30 | + throw new Error('No YAML frontmatter found'); |
| 31 | + } |
| 32 | + |
| 33 | + // Extract frontmatter |
| 34 | + const match = content.match(/^---\n([\s\S]*?)\n---/); |
| 35 | + if (!match) { |
| 36 | + throw new Error('Invalid frontmatter format'); |
| 37 | + } |
| 38 | + |
| 39 | + const frontmatter = match[1]; |
| 40 | + |
| 41 | + // Simple YAML parsing for required fields |
| 42 | + const nameMatch = frontmatter.match(/^name:\s*(.+)$/m); |
| 43 | + const descMatch = frontmatter.match(/^description:\s*(.+)$/m); |
| 44 | + |
| 45 | + if (!nameMatch) { |
| 46 | + throw new Error("Missing 'name' in frontmatter"); |
| 47 | + } |
| 48 | + |
| 49 | + if (!descMatch) { |
| 50 | + throw new Error("Missing 'description' in frontmatter"); |
| 51 | + } |
| 52 | + |
| 53 | + const name = nameMatch[1].trim(); |
| 54 | + const description = descMatch[1].trim(); |
| 55 | + |
| 56 | + // Validate name format (hyphen-case) |
| 57 | + if (!/^[a-z0-9-]+$/.test(name)) { |
| 58 | + throw new Error(`Name '${name}' should be hyphen-case (lowercase letters, digits, and hyphens only)`); |
| 59 | + } |
| 60 | + |
| 61 | + if (name.startsWith('-') || name.endsWith('-') || name.includes('--')) { |
| 62 | + throw new Error(`Name '${name}' cannot start/end with hyphen or contain consecutive hyphens`); |
| 63 | + } |
| 64 | + |
| 65 | + if (name.length > 64) { |
| 66 | + throw new Error(`Name is too long (${name.length} characters). Maximum is 64 characters.`); |
| 67 | + } |
| 68 | + |
| 69 | + // Validate description |
| 70 | + if (description.includes('<') || description.includes('>')) { |
| 71 | + throw new Error('Description cannot contain angle brackets (< or >)'); |
| 72 | + } |
| 73 | + |
| 74 | + if (description.length > 1024) { |
| 75 | + throw new Error(`Description is too long (${description.length} characters). Maximum is 1024 characters.`); |
| 76 | + } |
| 77 | + |
| 78 | + return {description, name}; |
| 79 | +} |
| 80 | + |
| 81 | +function packageSkill() { |
| 82 | + console.log('📦 Packaging skill: hackmd-cli\n'); |
| 83 | + |
| 84 | + // Validate |
| 85 | + console.log('🔍 Validating skill...'); |
| 86 | + try { |
| 87 | + validateSkill(); |
| 88 | + console.log('✅ Skill is valid!\n'); |
| 89 | + } catch (error) { |
| 90 | + console.error(`❌ Validation failed: ${error.message}`); |
| 91 | + throw error; |
| 92 | + } |
| 93 | + |
| 94 | + // Remove existing .skill file |
| 95 | + if (fs.existsSync(OUTPUT_FILE)) { |
| 96 | + fs.unlinkSync(OUTPUT_FILE); |
| 97 | + } |
| 98 | + |
| 99 | + // Create zip file using system zip command |
| 100 | + try { |
| 101 | + // Get all files in skill directory |
| 102 | + const files = getAllFiles(SKILL_DIR); |
| 103 | + |
| 104 | + if (files.length === 0) { |
| 105 | + throw new Error('No files found in skill directory'); |
| 106 | + } |
| 107 | + |
| 108 | + // Use zip command from parent directory to maintain folder structure |
| 109 | + const parentDir = path.dirname(SKILL_DIR); |
| 110 | + const skillDirName = path.basename(SKILL_DIR); |
| 111 | + |
| 112 | + execSync(`zip -r "${OUTPUT_FILE}" "${skillDirName}"`, { |
| 113 | + cwd: parentDir, |
| 114 | + stdio: 'pipe', |
| 115 | + }); |
| 116 | + |
| 117 | + // List what was added |
| 118 | + for (const file of files) { |
| 119 | + const relative = path.relative(parentDir, file); |
| 120 | + console.log(` Added: ${relative}`); |
| 121 | + } |
| 122 | + |
| 123 | + console.log(`\n✅ Successfully packaged skill to: ${OUTPUT_FILE}`); |
| 124 | + } catch (error) { |
| 125 | + console.error(`❌ Error creating .skill file: ${error.message}`); |
| 126 | + throw error; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +function getAllFiles(dir) { |
| 131 | + const files = []; |
| 132 | + const entries = fs.readdirSync(dir, {withFileTypes: true}); |
| 133 | + |
| 134 | + for (const entry of entries) { |
| 135 | + const fullPath = path.join(dir, entry.name); |
| 136 | + if (entry.isDirectory()) { |
| 137 | + files.push(...getAllFiles(fullPath)); |
| 138 | + } else { |
| 139 | + files.push(fullPath); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return files; |
| 144 | +} |
| 145 | + |
| 146 | +try { |
| 147 | + packageSkill(); |
| 148 | +} catch (error) { |
| 149 | + process.exitCode = 1; |
| 150 | +} |
0 commit comments