-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdev.mjs
More file actions
105 lines (90 loc) · 3.47 KB
/
dev.mjs
File metadata and controls
105 lines (90 loc) · 3.47 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
/**
* 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 registerDevCommand(cli) {
cli
.command('dev [dir]', 'Start development server')
.option('--port <port>', 'Port to listen on', { default: 7777 })
.action(async (dir, options) => {
// 1. Resolve user's docs directory (Absolute path)
const docsDir = dir ? path.resolve(process.cwd(), dir) : path.resolve(process.cwd(), 'content/docs');
// 2. Resolve the Next.js App directory - use local .objectdocs first
let nextAppDir = path.resolve(process.cwd(), 'content/.objectdocs');
if (!fs.existsSync(nextAppDir)) {
console.log('⚠️ ObjectDocs site not found at content/.objectdocs');
console.log(' Run "objectdocs init" first to initialize the site.\n');
process.exit(1);
}
console.log(`Starting docs server...`);
console.log(` Engine: ${nextAppDir}`);
console.log(` Content: ${docsDir}`);
const env = {
...process.env,
DOCS_DIR: docsDir,
PORT: options.port
};
const nextCmd = 'npm';
const args = ['run', 'dev', '--', '-p', options.port];
let child;
let isRestarting = false;
let debounceTimer;
const startServer = () => {
// Sync config and assets before starting
const userConfigPath = path.resolve(process.cwd(), 'content/docs.site.json');
if (fs.existsSync(userConfigPath)) {
fs.cpSync(userConfigPath, path.join(nextAppDir, 'docs.site.json'));
}
const userPublicPath = path.resolve(process.cwd(), 'public');
if (fs.existsSync(userPublicPath)) {
const targetPublicDir = path.join(nextAppDir, 'public');
if (!fs.existsSync(targetPublicDir)) {
fs.mkdirSync(targetPublicDir, { recursive: true });
}
fs.cpSync(userPublicPath, targetPublicDir, { recursive: true, force: true });
}
child = spawn(nextCmd, args, {
stdio: 'inherit',
env,
cwd: nextAppDir // CRITICAL: Run in the Next.js app directory
});
child.on('close', (code) => {
if (isRestarting) {
isRestarting = false;
startServer();
} else {
// Only exit if we are not restarting.
// Null code means killed by signal (like our kill() call), but we handle that via flag.
process.exit(code || 0);
}
});
};
startServer();
// Watch for config changes
const configFile = path.resolve(process.cwd(), 'content/docs.site.json');
if (fs.existsSync(configFile)) {
console.log(`Watching config: ${configFile}`);
fs.watch(configFile, (eventType) => {
if (eventType === 'change') {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
console.log('\nConfig changed. Restarting server...');
isRestarting = true;
child.kill();
}, 500);
}
});
}
});
}