1+ #!/usr/bin/env node
2+
3+ import { execSync } from 'child_process' ;
4+ import { existsSync , rmSync , mkdirSync , cpSync , readFileSync } from 'fs' ;
5+ import { resolve , dirname } from 'path' ;
6+ import { fileURLToPath } from 'url'
7+ import http from 'http' ;
8+ import sirv from 'sirv' ;
9+
10+ const __filename = fileURLToPath ( import . meta. url ) ;
11+ const __dirname = dirname ( __filename ) ;
12+
13+ const commands = {
14+ build : ( ) => {
15+ console . log ( '🏗️ Starting Dory build...' ) ;
16+
17+ // Check if dory.json exists in current directory
18+ const currentDir = process . cwd ( ) ;
19+ const doryConfigPath = resolve ( currentDir , 'dory.json' ) ;
20+
21+ if ( ! existsSync ( doryConfigPath ) ) {
22+ console . error ( '❌ dory.json not found in current directory' ) ;
23+ process . exit ( 1 ) ;
24+ }
25+
26+ // Read and validate dory.json
27+ try {
28+ const config = JSON . parse ( readFileSync ( doryConfigPath , 'utf8' ) ) ;
29+ console . log ( `📋 Using config: ${ config . name || 'Unnamed project' } ` ) ;
30+ } catch ( error ) {
31+ console . error ( '❌ Invalid dory.json file:' , error . message ) ;
32+ process . exit ( 1 ) ;
33+ }
34+
35+ // Clear and prepare docs folder
36+ const docsDir = resolve ( currentDir , 'docs' ) ;
37+ if ( existsSync ( docsDir ) ) {
38+ console . log ( '🧹 Clearing docs folder...' ) ;
39+ rmSync ( docsDir , { recursive : true , force : true } ) ;
40+ }
41+ mkdirSync ( docsDir , { recursive : true } ) ;
42+
43+ // Copy dory.json to docs folder
44+ cpSync ( doryConfigPath , resolve ( docsDir , 'dory.json' ) ) ;
45+ console . log ( '📄 Copied dory.json to docs folder' ) ;
46+
47+ // Run the build command
48+ console . log ( '⚡ Running build command...' ) ;
49+ try {
50+ execSync ( 'npm run build' , { stdio : 'inherit' , cwd : __dirname } ) ;
51+ } catch ( error ) {
52+ console . error ( '❌ Build failed:' , error . message ) ;
53+ process . exit ( 1 ) ;
54+ }
55+
56+ // Check if dist folder was created
57+ const distDir = resolve ( __dirname , '..' , 'dist' ) ;
58+ if ( existsSync ( distDir ) ) {
59+ console . log ( '✅ Build completed successfully!' ) ;
60+ console . log ( `📦 Build output available in: ${ distDir } ` ) ;
61+ console . log ( '📋 Moving build output to current directory...' ) ;
62+ cpSync ( distDir , resolve ( currentDir , 'dist' ) , { recursive : true } ) ;
63+ console . log ( `📦 Build output moved to: ${ resolve ( currentDir , 'dist' ) } ` ) ;
64+ } else {
65+ console . log ( '⚠️ Build completed but no dist folder found' ) ;
66+ }
67+ rmSync ( distDir , { recursive : true , force : true } ) ;
68+ } ,
69+
70+ preview : ( ) => {
71+ console . log ( '👀 Starting Dory preview...' ) ;
72+ const currentDir = process . cwd ( ) ;
73+ const distDir = resolve ( currentDir , 'dist' ) ;
74+ const port = process . env . PORT || 3000 ;
75+
76+ const serve = sirv ( distDir , {
77+ dev : false ,
78+ single : true ,
79+ etag : true ,
80+ gzip : true ,
81+ brotli : true ,
82+ } ) ;
83+
84+ const server = http . createServer ( ( req , res ) => {
85+ serve ( req , res ) ;
86+ } ) ;
87+
88+ server . listen ( port , ( ) => {
89+ console . log ( `🚀 Serving Dory at http://localhost:${ port } ` ) ;
90+ } ) ;
91+ } ,
92+
93+ help : ( ) => {
94+ console . log ( `
95+ 🐟 Dory CLI - Documentation Build Tool
96+
97+ Usage:
98+ dory <command>
99+
100+ Commands:
101+ build Build the documentation site
102+ - Requires dory.json in current directory
103+ - Clears and copies to docs folder
104+ - Runs build command
105+ - Creates dist folder with build output
106+
107+ preview Preview the built documentation site
108+ - Requires dist folder (run build first)
109+ - Starts preview server
110+
111+ help Show this help message
112+
113+ Examples:
114+ dory build # Build the documentation
115+ dory preview # Preview the built site
116+ ` ) ;
117+ }
118+ } ;
119+
120+ // Parse command line arguments
121+ const command = process . argv [ 2 ] ;
122+
123+ if ( ! command || command === 'help' || command === '--help' || command === '-h' ) {
124+ commands . help ( ) ;
125+ } else if ( commands [ command ] ) {
126+ commands [ command ] ( ) ;
127+ } else {
128+ console . error ( `❌ Unknown command: ${ command } ` ) ;
129+ console . log ( 'Run "dory help" for usage information' ) ;
130+ process . exit ( 1 ) ;
131+ }
0 commit comments