-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.mjs
More file actions
168 lines (144 loc) · 6.03 KB
/
init.mjs
File metadata and controls
168 lines (144 loc) · 6.03 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* 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 contentDir = path.resolve(process.cwd(), 'content');
const targetDir = path.resolve(contentDir, '.objectdocs');
const contentPackageJsonPath = path.resolve(contentDir, 'package.json');
// 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;
}
// Create content directory if it doesn't exist
if (!fs.existsSync(contentDir)) {
fs.mkdirSync(contentDir, { recursive: true });
console.log(`📁 Created content directory\n`);
}
// 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');
// Initialize or update content/package.json
let packageJson = {};
if (fs.existsSync(contentPackageJsonPath)) {
console.log('📝 Updating existing content/package.json\n');
packageJson = JSON.parse(fs.readFileSync(contentPackageJsonPath, 'utf-8'));
} else {
console.log('📝 Creating content/package.json\n');
packageJson = {
name: 'objectdocs-content',
version: '0.1.0',
private: true,
description: 'ObjectDocs documentation content'
};
}
// Ensure scripts section exists
if (!packageJson.scripts) {
packageJson.scripts = {};
}
// Add/update ObjectDocs scripts
packageJson.scripts = {
...packageJson.scripts,
dev: 'cd .objectdocs && npm run dev',
build: 'cd .objectdocs && npm run build',
start: 'cd .objectdocs && npm run start'
};
// Write package.json
fs.writeFileSync(
contentPackageJsonPath,
JSON.stringify(packageJson, null, 2) + '\n'
);
console.log('✅ content/package.json configured\n');
// Add to .gitignore
const gitignorePath = path.resolve(process.cwd(), '.gitignore');
const gitignoreEntries = ['content/.objectdocs', 'content/node_modules'];
try {
let gitignoreContent = '';
if (fs.existsSync(gitignorePath)) {
gitignoreContent = fs.readFileSync(gitignorePath, 'utf-8');
}
const lines = gitignoreContent.split('\n').map(line => line.trim());
const entriesToAdd = gitignoreEntries.filter(entry => !lines.includes(entry));
if (entriesToAdd.length > 0) {
const separator = gitignoreContent.trim() ? '\n\n' : '';
const newContent = `${gitignoreContent.trim()}${separator}# ObjectDocs\n${entriesToAdd.join('\n')}\n`;
fs.writeFileSync(gitignorePath, newContent);
console.log(`📝 Added ${entriesToAdd.join(', ')} to .gitignore\n`);
}
} catch (e) {
console.warn('⚠️ Could not update .gitignore:', e.message);
}
// Install dependencies in the target directory
console.log('📦 Installing dependencies in content/.objectdocs...\n');
const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const installProcess = spawn(npmCmd, ['install', '--legacy-peer-deps'], {
cwd: targetDir,
stdio: 'inherit'
});
// Wait for the install process to complete
await new Promise((resolve, reject) => {
installProcess.on('close', (code) => {
if (code === 0) {
console.log('\n✅ Dependencies installed successfully!');
console.log('\n🎉 ObjectDocs initialized! You can now run:');
console.log(' cd content && npm run dev - Start development server');
console.log(' cd content && npm run build - Build for production\n');
console.log('Or if you have npm scripts in your root package.json:');
console.log(' npm run dev - Start development server');
console.log(' npm run build - Build for production\n');
resolve();
} else {
console.error('\n❌ Failed to install dependencies');
reject(new Error(`Install failed with code ${code}`));
}
});
installProcess.on('error', (err) => {
console.error('\n❌ Failed to start install process:', err.message);
reject(err);
});
});
});
}