-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.mjs
More file actions
106 lines (92 loc) · 3.82 KB
/
build.mjs
File metadata and controls
106 lines (92 loc) · 3.82 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
/**
* 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 registerBuildCommand(cli) {
cli
.command('build [dir]', 'Build static documentation site')
.action(async (dir, options) => {
// 1. Resolve user's docs directory
const docsDir = dir ? path.resolve(process.cwd(), dir) : path.resolve(process.cwd(), 'content/docs');
// 2. Resolve the Next.js App directory
let nextAppDir;
try {
nextAppDir = path.dirname(require.resolve('@objectdocs/site/package.json'));
} catch (e) {
// Fallback for local development
nextAppDir = path.resolve(__dirname, '../../../site');
}
// Copy user config and assets to nextAppDir
const userConfigPath = path.resolve(process.cwd(), 'content/docs.site.json');
if (fs.existsSync(userConfigPath)) {
console.log(` Copying config from ${userConfigPath}`);
fs.cpSync(userConfigPath, path.join(nextAppDir, 'docs.site.json'));
}
const userPublicPath = path.resolve(process.cwd(), 'public');
if (fs.existsSync(userPublicPath)) {
console.log(` Copying public assets from ${userPublicPath}`);
const targetPublicDir = path.join(nextAppDir, 'public');
if (!fs.existsSync(targetPublicDir)) {
fs.mkdirSync(targetPublicDir, { recursive: true });
}
fs.cpSync(userPublicPath, targetPublicDir, { recursive: true, force: true });
}
console.log(`Building docs site...`);
console.log(` Engine: ${nextAppDir}`);
console.log(` Content: ${docsDir}`);
const env = {
...process.env,
DOCS_DIR: docsDir
};
const nextCmd = 'npm';
const args = ['run', 'build'];
const child = spawn(nextCmd, args, {
stdio: 'inherit',
env,
cwd: nextAppDir // CRITICAL: Run in the Next.js app directory
});
child.on('close', (code) => {
if (code === 0) {
// Copy output to project root
const src = path.join(nextAppDir, 'out');
const dest = path.join(process.cwd(), 'out');
if (fs.existsSync(src)) {
console.log(`\nMoving build output to ${dest}...`);
if (fs.existsSync(dest)) {
fs.rmSync(dest, { recursive: true, force: true });
}
fs.cpSync(src, dest, { recursive: true });
console.log(`Build successfully output to: ${dest}`);
} else {
// Check for .next directory (dynamic build)
const srcNext = path.join(nextAppDir, '.next');
const destNext = path.join(process.cwd(), '.next');
if (fs.existsSync(srcNext) && srcNext !== destNext) {
console.log(`\nLinking .next build output to ${destNext}...`);
if (fs.existsSync(destNext)) {
fs.rmSync(destNext, { recursive: true, force: true });
}
// Use symlink instead of copy to preserve internal symlinks in .next (pnpm support)
fs.symlinkSync(srcNext, destNext, 'dir');
console.log(`Build successfully linked to: ${destNext}`);
} else {
console.log(`\nNo 'out' directory generated in ${src}.`);
console.log(`This is expected if 'output: export' is disabled.`);
}
}
}
process.exit(code);
});
});
}