|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { mkdir, writeFile, readFile } from 'fs/promises'; |
| 4 | +import { join, dirname } from 'path'; |
| 5 | +import { fileURLToPath } from 'url'; |
| 6 | +import { spawnSync } from 'child_process'; |
| 7 | + |
| 8 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 9 | +// Templates are at the package root /templates/ (two levels up from dist/cli/) |
| 10 | +const templateDir = join(__dirname, '..', '..', 'templates'); |
| 11 | + |
| 12 | +async function main() { |
| 13 | + const projectName = process.argv[2]; |
| 14 | + |
| 15 | + if (!projectName) { |
| 16 | + console.error('Usage: create-docs-mcp <project-name>'); |
| 17 | + console.error(''); |
| 18 | + console.error('Example:'); |
| 19 | + console.error(' npx create-docs-mcp my-api-docs'); |
| 20 | + process.exit(1); |
| 21 | + } |
| 22 | + |
| 23 | + if (!/^[a-z0-9-]+$/.test(projectName)) { |
| 24 | + console.error('Error: Project name can only contain lowercase letters, numbers, and hyphens'); |
| 25 | + process.exit(1); |
| 26 | + } |
| 27 | + |
| 28 | + const projectDir = join(process.cwd(), projectName); |
| 29 | + const srcDir = join(projectDir, 'src'); |
| 30 | + |
| 31 | + console.log(`Creating docs MCP server: ${projectName}`); |
| 32 | + |
| 33 | + try { |
| 34 | + await mkdir(projectDir); |
| 35 | + await mkdir(srcDir); |
| 36 | + |
| 37 | + // Read and process templates |
| 38 | + const templates: Array<{ src: string; dest: string }> = [ |
| 39 | + { src: 'index.ts.template', dest: join(srcDir, 'index.ts') }, |
| 40 | + { src: 'package.json.template', dest: join(projectDir, 'package.json') }, |
| 41 | + { src: 'tsconfig.json.template', dest: join(projectDir, 'tsconfig.json') }, |
| 42 | + { src: 'env.example.template', dest: join(projectDir, '.env.example') }, |
| 43 | + { src: 'README.md.template', dest: join(projectDir, 'README.md') }, |
| 44 | + ]; |
| 45 | + |
| 46 | + for (const t of templates) { |
| 47 | + let content = await readFile(join(templateDir, t.src), 'utf-8'); |
| 48 | + content = content.replace(/\{\{PROJECT_NAME\}\}/g, projectName); |
| 49 | + await writeFile(t.dest, content); |
| 50 | + } |
| 51 | + |
| 52 | + // Create .gitignore |
| 53 | + await writeFile( |
| 54 | + join(projectDir, '.gitignore'), |
| 55 | + 'node_modules\ndist\n.env\n*.log\n.DS_Store\n' |
| 56 | + ); |
| 57 | + |
| 58 | + // Init git |
| 59 | + console.log('Initializing git repository...'); |
| 60 | + spawnSync('git', ['init'], { cwd: projectDir, stdio: 'inherit', shell: true }); |
| 61 | + |
| 62 | + // Install deps |
| 63 | + console.log('Installing dependencies...'); |
| 64 | + const install = spawnSync('npm', ['install'], { |
| 65 | + cwd: projectDir, |
| 66 | + stdio: 'inherit', |
| 67 | + shell: true, |
| 68 | + }); |
| 69 | + |
| 70 | + if (install.status !== 0) { |
| 71 | + console.error('Warning: npm install failed. Run it manually after setup.'); |
| 72 | + } else { |
| 73 | + // Build |
| 74 | + console.log('Building project...'); |
| 75 | + spawnSync('npx', ['tsc'], { cwd: projectDir, stdio: 'inherit', shell: true }); |
| 76 | + } |
| 77 | + |
| 78 | + console.log(` |
| 79 | +Done! Your docs MCP server is ready. |
| 80 | +
|
| 81 | +Next steps: |
| 82 | + cd ${projectName} |
| 83 | + cp .env.example .env |
| 84 | + # Edit .env with your documentation site URL |
| 85 | + npm run build |
| 86 | + npm start |
| 87 | +
|
| 88 | +Add to Claude Code: |
| 89 | + claude mcp add ${projectName} -- node $(pwd)/${projectName}/dist/index.js |
| 90 | +`); |
| 91 | + } catch (error: any) { |
| 92 | + if (error.code === 'EEXIST') { |
| 93 | + console.error(`Error: Directory "${projectName}" already exists`); |
| 94 | + } else { |
| 95 | + console.error('Error creating project:', error.message); |
| 96 | + } |
| 97 | + process.exit(1); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +main(); |
0 commit comments