Skip to content

Commit e911224

Browse files
Copilothuangyiirene
andcommitted
Add init command and update dev/build/start to use content/.objectdocs
Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
1 parent bc70ac7 commit e911224

File tree

7 files changed

+119
-21
lines changed

7 files changed

+119
-21
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ next-env.d.ts
3434

3535
# fumadocs
3636
.source
37-
.next
37+
.next
38+
39+
# objectdocs
40+
content/.objectdocs

examples/starter/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ pnpm-debug.log*
3434
# TypeScript
3535
*.tsbuildinfo
3636
next-env.d.ts
37+
38+
# ObjectDocs
39+
content/.objectdocs

packages/cli/bin/cli.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99

1010
import { cac } from 'cac';
1111
import 'dotenv/config';
12+
import { registerInitCommand } from '../src/commands/init.mjs';
1213
import { registerTranslateCommand } from '../src/commands/translate.mjs';
1314
import { registerDevCommand } from '../src/commands/dev.mjs';
1415
import { registerBuildCommand } from '../src/commands/build.mjs';
1516
import { registerStartCommand } from '../src/commands/start.mjs';
1617

1718
const cli = cac('objectdocs');
1819

20+
registerInitCommand(cli);
1921
registerTranslateCommand(cli);
2022
registerDevCommand(cli);
2123
registerBuildCommand(cli);

packages/cli/src/commands/build.mjs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ export function registerBuildCommand(cli) {
2323
// 1. Resolve user's docs directory
2424
const docsDir = dir ? path.resolve(process.cwd(), dir) : path.resolve(process.cwd(), 'content/docs');
2525

26-
// 2. Resolve the Next.js App directory
27-
let nextAppDir;
28-
try {
29-
nextAppDir = path.dirname(require.resolve('@objectdocs/site/package.json'));
30-
} catch (e) {
31-
// Fallback for local development
32-
nextAppDir = path.resolve(__dirname, '../../../site');
26+
// 2. Resolve the Next.js App directory - use local .objectdocs first
27+
let nextAppDir = path.resolve(process.cwd(), 'content/.objectdocs');
28+
29+
if (!fs.existsSync(nextAppDir)) {
30+
console.log('⚠️ ObjectDocs site not found at content/.objectdocs');
31+
console.log(' Run "objectdocs init" first to initialize the site.\n');
32+
process.exit(1);
3333
}
3434

3535
// Copy user config and assets to nextAppDir

packages/cli/src/commands/dev.mjs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ export function registerDevCommand(cli) {
2424
// 1. Resolve user's docs directory (Absolute path)
2525
const docsDir = dir ? path.resolve(process.cwd(), dir) : path.resolve(process.cwd(), 'content/docs');
2626

27-
// 2. Resolve the Next.js App directory
28-
let nextAppDir;
29-
try {
30-
nextAppDir = path.dirname(require.resolve('@objectdocs/site/package.json'));
31-
} catch (e) {
32-
// Fallback for local development
33-
nextAppDir = path.resolve(__dirname, '../../../site');
27+
// 2. Resolve the Next.js App directory - use local .objectdocs first
28+
let nextAppDir = path.resolve(process.cwd(), 'content/.objectdocs');
29+
30+
if (!fs.existsSync(nextAppDir)) {
31+
console.log('⚠️ ObjectDocs site not found at content/.objectdocs');
32+
console.log(' Run "objectdocs init" first to initialize the site.\n');
33+
process.exit(1);
3434
}
3535

3636
console.log(`Starting docs server...`);

packages/cli/src/commands/init.mjs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
// Install dependencies in the target directory
69+
console.log('📦 Installing dependencies...\n');
70+
71+
const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
72+
const installProcess = spawn(npmCmd, ['install'], {
73+
cwd: targetDir,
74+
stdio: 'inherit'
75+
});
76+
77+
installProcess.on('close', (code) => {
78+
if (code === 0) {
79+
console.log('\n✅ Dependencies installed successfully!');
80+
console.log('\n🎉 ObjectDocs initialized! You can now run:');
81+
console.log(' pnpm dev - Start development server');
82+
console.log(' pnpm build - Build for production\n');
83+
} else {
84+
console.error('\n❌ Failed to install dependencies');
85+
process.exit(code);
86+
}
87+
});
88+
});
89+
}

packages/cli/src/commands/start.mjs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ const require = createRequire(import.meta.url);
1919
export function registerStartCommand(cli) {
2020
cli.command('start [dir]', 'Start the production server')
2121
.action((dir) => {
22-
// 1. Resolve Next.js App directory
23-
let nextAppDir;
24-
try {
25-
nextAppDir = path.dirname(require.resolve('@objectdocs/site/package.json'));
26-
} catch (e) {
27-
nextAppDir = path.resolve(__dirname, '../../../site');
22+
// 1. Resolve Next.js App directory - use local .objectdocs first
23+
let nextAppDir = path.resolve(process.cwd(), 'content/.objectdocs');
24+
25+
if (!fs.existsSync(nextAppDir)) {
26+
console.log('⚠️ ObjectDocs site not found at content/.objectdocs');
27+
console.log(' Run "objectdocs init" first to initialize the site.\n');
28+
process.exit(1);
2829
}
2930

3031
// 2. Check config

0 commit comments

Comments
 (0)