-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstart.mjs
More file actions
77 lines (66 loc) · 2.23 KB
/
start.mjs
File metadata and controls
77 lines (66 loc) · 2.23 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
/**
* 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 'node:child_process';
import path from 'node:path';
import fs from 'node:fs';
import { createRequire } from 'module';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const require = createRequire(import.meta.url);
export function registerStartCommand(cli) {
cli.command('start [dir]', 'Start the production server')
.action((dir) => {
// 1. Resolve Next.js App directory
let nextAppDir;
try {
nextAppDir = path.dirname(require.resolve('@objectdocs/site/package.json'));
} catch (e) {
nextAppDir = path.resolve(__dirname, '../../../site');
}
// 2. Check config
let isStatic = false;
try {
const configPath = path.resolve(process.cwd(), 'content/docs.site.json');
if (fs.existsSync(configPath)) {
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
if (config.build?.output === 'export') {
isStatic = true;
}
}
} catch (e) {
// ignore
}
if (isStatic || (dir && dir !== 'out')) {
// Static Mode
const targetDir = dir ? path.resolve(process.cwd(), dir) : path.resolve(process.cwd(), 'out');
console.log(`Serving static site from: ${targetDir}`);
const child = spawn('npx', ['serve', targetDir], {
stdio: 'inherit',
shell: true
});
child.on('error', (err) => {
console.error('Failed to start server:', err);
});
} else {
// Dynamic Mode (Next.js start)
console.log('Starting Next.js production server...');
console.log(` Engine: ${nextAppDir}`);
const docsDir = path.resolve(process.cwd(), 'content/docs');
const env = {
...process.env,
DOCS_DIR: docsDir
};
const child = spawn('npm', ['start'], {
cwd: nextAppDir,
stdio: 'inherit',
env: env
});
}
});
}