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