|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from 'fs'; |
| 4 | +import path from 'path'; |
| 5 | +import http from 'http'; |
| 6 | +import { fileURLToPath } from 'url'; |
| 7 | + |
| 8 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 9 | + |
| 10 | +async function fetchJson(url) { |
| 11 | + return new Promise((resolve, reject) => { |
| 12 | + http.get(url, (res) => { |
| 13 | + let data = ''; |
| 14 | + res.on('data', (chunk) => { |
| 15 | + data += chunk; |
| 16 | + }); |
| 17 | + res.on('end', () => { |
| 18 | + try { |
| 19 | + resolve(JSON.parse(data)); |
| 20 | + } catch (e) { |
| 21 | + reject(e); |
| 22 | + } |
| 23 | + }); |
| 24 | + }).on('error', reject); |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +function ensureDirectoryExists(dir) { |
| 29 | + if (!fs.existsSync(dir)) { |
| 30 | + fs.mkdirSync(dir, { recursive: true }); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function updateOrCreateOpenCodeJson(modeData, modeId) { |
| 35 | + const currentDir = process.cwd(); |
| 36 | + const openCodePath = path.join(currentDir, 'opencode.json'); |
| 37 | + |
| 38 | + let openCodeConfig = { |
| 39 | + "$schema": "https://opencode.ai/config.json", |
| 40 | + "instructions": [], |
| 41 | + "mcp": {}, |
| 42 | + "provider": {}, |
| 43 | + "mode": {} |
| 44 | + }; |
| 45 | + |
| 46 | + if (fs.existsSync(openCodePath)) { |
| 47 | + try { |
| 48 | + openCodeConfig = JSON.parse(fs.readFileSync(openCodePath, 'utf8')); |
| 49 | + } catch (e) { |
| 50 | + console.warn('⚠️ Could not parse existing opencode.json, creating new one'); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (!openCodeConfig.mcp) openCodeConfig.mcp = {}; |
| 55 | + if (!openCodeConfig.mode) openCodeConfig.mode = {}; |
| 56 | + |
| 57 | + if (modeData.opencode_config && modeData.opencode_config.mode) { |
| 58 | + Object.entries(modeData.opencode_config.mode).forEach(([key, config]) => { |
| 59 | + const updatedConfig = { ...config }; |
| 60 | + |
| 61 | + if (updatedConfig.prompt) { |
| 62 | + updatedConfig.prompt = `{file:./.opencode/mode/${key}/${key}.mode.md}`; |
| 63 | + } |
| 64 | + |
| 65 | + if (updatedConfig.instructions && Array.isArray(updatedConfig.instructions)) { |
| 66 | + updatedConfig.instructions = updatedConfig.instructions.map(instruction => { |
| 67 | + const filename = instruction.replace('./', ''); |
| 68 | + return `./.opencode/mode/${key}/${filename}`; |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + openCodeConfig.mode[key] = updatedConfig; |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + fs.writeFileSync(openCodePath, JSON.stringify(openCodeConfig, null, '\t')); |
| 77 | +} |
| 78 | + |
| 79 | +async function installMode(modeId) { |
| 80 | + try { |
| 81 | + console.log(`📦 Installing mode: ${modeId}`); |
| 82 | + |
| 83 | + const url = `https://openmodes.dev/mode/${modeId}`; |
| 84 | + const modeData = await fetchJson(url); |
| 85 | + |
| 86 | + // Remove URL keys from MCP configurations |
| 87 | + if (modeData.opencode_config && modeData.opencode_config.mode) { |
| 88 | + Object.values(modeData.opencode_config.mode).forEach(modeConfig => { |
| 89 | + if (modeConfig.mcp) { |
| 90 | + Object.values(modeConfig.mcp).forEach(mcpConfig => { |
| 91 | + delete mcpConfig.url; |
| 92 | + }); |
| 93 | + } |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + const currentDir = process.cwd(); |
| 98 | + const modeDir = path.join(currentDir, '.opencode', 'mode', modeId); |
| 99 | + ensureDirectoryExists(modeDir); |
| 100 | + |
| 101 | + // Write mode files |
| 102 | + if (modeData.mode_prompt) { |
| 103 | + const promptPath = path.join(modeDir, `${modeId}.mode.md`); |
| 104 | + fs.writeFileSync(promptPath, modeData.mode_prompt); |
| 105 | + } |
| 106 | + |
| 107 | + if (modeData.context_instructions && Array.isArray(modeData.context_instructions)) { |
| 108 | + modeData.context_instructions.forEach((instruction) => { |
| 109 | + const filename = `${instruction.title.toLowerCase()}.instructions.md`; |
| 110 | + const instructionPath = path.join(modeDir, filename); |
| 111 | + fs.writeFileSync(instructionPath, instruction.content); |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + updateOrCreateOpenCodeJson(modeData, modeId); |
| 116 | + |
| 117 | + console.log(`✅ Successfully installed mode "${modeId}"`); |
| 118 | + |
| 119 | + } catch (error) { |
| 120 | + console.error(`❌ Error installing mode "${modeId}":`, error.message); |
| 121 | + process.exit(1); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +function removeMode(modeId) { |
| 126 | + try { |
| 127 | + console.log(`🗑️ Removing mode: ${modeId}`); |
| 128 | + |
| 129 | + const currentDir = process.cwd(); |
| 130 | + const modeDir = path.join(currentDir, '.opencode', 'mode', modeId); |
| 131 | + const openCodePath = path.join(currentDir, 'opencode.json'); |
| 132 | + |
| 133 | + if (fs.existsSync(modeDir)) { |
| 134 | + fs.rmSync(modeDir, { recursive: true, force: true }); |
| 135 | + } |
| 136 | + |
| 137 | + if (fs.existsSync(openCodePath)) { |
| 138 | + try { |
| 139 | + const openCodeConfig = JSON.parse(fs.readFileSync(openCodePath, 'utf8')); |
| 140 | + |
| 141 | + if (openCodeConfig.mode && openCodeConfig.mode[modeId]) { |
| 142 | + delete openCodeConfig.mode[modeId]; |
| 143 | + fs.writeFileSync(openCodePath, JSON.stringify(openCodeConfig, null, '\t')); |
| 144 | + } |
| 145 | + } catch (e) { |
| 146 | + console.error('⚠️ Error updating opencode.json:', e.message); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + console.log(`✅ Successfully removed mode "${modeId}"`); |
| 151 | + |
| 152 | + } catch (error) { |
| 153 | + console.error(`❌ Error removing mode "${modeId}":`, error.message); |
| 154 | + process.exit(1); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +const args = process.argv.slice(2); |
| 159 | +const command = args[0]; |
| 160 | +const modeId = args[1]; |
| 161 | + |
| 162 | +if (command === 'install' && modeId) { |
| 163 | + installMode(modeId); |
| 164 | +} else if (command === 'remove' && modeId) { |
| 165 | + removeMode(modeId); |
| 166 | +} else { |
| 167 | + console.log('Usage: openmodes <command> <mode-id>'); |
| 168 | + console.log(''); |
| 169 | + console.log('Commands:'); |
| 170 | + console.log(' install <mode-id> Install a mode from openmodes.dev'); |
| 171 | + console.log(' remove <mode-id> Remove an installed mode'); |
| 172 | + console.log(''); |
| 173 | + console.log('Examples:'); |
| 174 | + console.log(' npx openmodes install archie'); |
| 175 | + console.log(' npx openmodes remove archie'); |
| 176 | + process.exit(1); |
| 177 | +} |
0 commit comments