|
| 1 | +/** |
| 2 | + * ObjectDocs |
| 3 | + * Copyright (c) 2026-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { spawn } from 'child_process'; |
| 10 | +import path from 'path'; |
| 11 | +import fs from 'fs'; |
| 12 | +import { fileURLToPath } from 'url'; |
| 13 | +import { createRequire } from 'module'; |
| 14 | + |
| 15 | +const __filename = fileURLToPath(import.meta.url); |
| 16 | +const __dirname = path.dirname(__filename); |
| 17 | +const require = createRequire(import.meta.url); |
| 18 | + |
| 19 | +export function registerInitCommand(cli) { |
| 20 | + cli |
| 21 | + .command('init', 'Initialize ObjectDocs site in content/.objectdocs') |
| 22 | + .action(async (options) => { |
| 23 | + console.log('Initializing ObjectDocs...\n'); |
| 24 | + |
| 25 | + const targetDir = path.resolve(process.cwd(), 'content/.objectdocs'); |
| 26 | + |
| 27 | + // Check if already initialized |
| 28 | + if (fs.existsSync(targetDir)) { |
| 29 | + console.log(`⚠️ ObjectDocs already initialized at ${targetDir}`); |
| 30 | + console.log(' Delete the directory if you want to reinitialize.\n'); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + // Resolve the site package directory |
| 35 | + let siteDir; |
| 36 | + try { |
| 37 | + siteDir = path.dirname(require.resolve('@objectdocs/site/package.json')); |
| 38 | + } catch (e) { |
| 39 | + // Fallback for local development |
| 40 | + siteDir = path.resolve(__dirname, '../../../site'); |
| 41 | + } |
| 42 | + |
| 43 | + console.log(`📦 Copying site from: ${siteDir}`); |
| 44 | + console.log(`📁 Target directory: ${targetDir}\n`); |
| 45 | + |
| 46 | + // Create target directory |
| 47 | + fs.mkdirSync(targetDir, { recursive: true }); |
| 48 | + |
| 49 | + // Copy site files to target directory |
| 50 | + fs.cpSync(siteDir, targetDir, { |
| 51 | + recursive: true, |
| 52 | + filter: (source) => { |
| 53 | + const basename = path.basename(source); |
| 54 | + // Skip node_modules, .next, and other build artifacts |
| 55 | + if (basename === 'node_modules' || |
| 56 | + basename === '.next' || |
| 57 | + basename === 'out' || |
| 58 | + basename === '.turbo' || |
| 59 | + basename === 'dist') { |
| 60 | + return false; |
| 61 | + } |
| 62 | + return true; |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + console.log('✅ ObjectDocs site copied successfully!\n'); |
| 67 | + |
| 68 | + // Add to .gitignore |
| 69 | + const gitignorePath = path.resolve(process.cwd(), '.gitignore'); |
| 70 | + const gitignoreEntry = 'content/.objectdocs'; |
| 71 | + |
| 72 | + try { |
| 73 | + let gitignoreContent = ''; |
| 74 | + if (fs.existsSync(gitignorePath)) { |
| 75 | + gitignoreContent = fs.readFileSync(gitignorePath, 'utf-8'); |
| 76 | + } |
| 77 | + |
| 78 | + // Check if the entry already exists (as a complete line) |
| 79 | + const lines = gitignoreContent.split('\n').map(line => line.trim()); |
| 80 | + if (!lines.includes(gitignoreEntry)) { |
| 81 | + // Add the entry with a comment |
| 82 | + const separator = gitignoreContent.trim() ? '\n\n' : ''; |
| 83 | + const newContent = `${gitignoreContent.trim()}${separator}# ObjectDocs\n${gitignoreEntry}\n`; |
| 84 | + fs.writeFileSync(gitignorePath, newContent); |
| 85 | + console.log('📝 Added content/.objectdocs to .gitignore\n'); |
| 86 | + } |
| 87 | + } catch (e) { |
| 88 | + console.warn('⚠️ Could not update .gitignore:', e.message); |
| 89 | + } |
| 90 | + |
| 91 | + // Install dependencies in the target directory |
| 92 | + console.log('📦 Installing dependencies...\n'); |
| 93 | + |
| 94 | + const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm'; |
| 95 | + const installProcess = spawn(npmCmd, ['install', '--legacy-peer-deps'], { |
| 96 | + cwd: targetDir, |
| 97 | + stdio: 'inherit' |
| 98 | + }); |
| 99 | + |
| 100 | + installProcess.on('close', (code) => { |
| 101 | + if (code === 0) { |
| 102 | + console.log('\n✅ Dependencies installed successfully!'); |
| 103 | + console.log('\n🎉 ObjectDocs initialized! You can now run:'); |
| 104 | + console.log(' pnpm dev - Start development server'); |
| 105 | + console.log(' pnpm build - Build for production\n'); |
| 106 | + } else { |
| 107 | + console.error('\n❌ Failed to install dependencies'); |
| 108 | + process.exit(code); |
| 109 | + } |
| 110 | + }); |
| 111 | + }); |
| 112 | +} |
0 commit comments