md-review 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/md-review.js +19 -38
- package/package.json +1 -1
- package/server/index.js +22 -1
package/bin/md-review.js
CHANGED
|
@@ -37,8 +37,7 @@ const args = mri(process.argv.slice(2), {
|
|
|
37
37
|
v: 'version'
|
|
38
38
|
},
|
|
39
39
|
default: {
|
|
40
|
-
port: '
|
|
41
|
-
'api-port': '3030',
|
|
40
|
+
port: '3030',
|
|
42
41
|
open: true
|
|
43
42
|
},
|
|
44
43
|
boolean: ['help', 'version', 'open']
|
|
@@ -54,8 +53,7 @@ Usage:
|
|
|
54
53
|
md-review <file> [options] Preview a specific markdown file (.md or .markdown)
|
|
55
54
|
|
|
56
55
|
Options:
|
|
57
|
-
-p, --port <port>
|
|
58
|
-
--api-port <port> API server port (default: 3030)
|
|
56
|
+
-p, --port <port> Server port (default: 3030)
|
|
59
57
|
--no-open Do not open browser automatically
|
|
60
58
|
-h, --help Show this help message
|
|
61
59
|
-v, --version Show version number
|
|
@@ -76,12 +74,10 @@ if (args.version) {
|
|
|
76
74
|
|
|
77
75
|
const file = args._[0];
|
|
78
76
|
const port = validatePort(args.port, 'port');
|
|
79
|
-
const apiPort = validatePort(args['api-port'], 'api-port');
|
|
80
77
|
const shouldOpen = args.open;
|
|
81
78
|
|
|
82
79
|
// Set environment variables
|
|
83
|
-
process.env.API_PORT =
|
|
84
|
-
process.env.VITE_PORT = port;
|
|
80
|
+
process.env.API_PORT = port;
|
|
85
81
|
|
|
86
82
|
// If file is specified, validate it
|
|
87
83
|
if (file) {
|
|
@@ -106,66 +102,51 @@ if (file) {
|
|
|
106
102
|
}
|
|
107
103
|
|
|
108
104
|
console.log('Starting md-review...');
|
|
109
|
-
console.log(`
|
|
110
|
-
console.log(` Vite Port: ${port}`);
|
|
105
|
+
console.log(` Port: ${port}`);
|
|
111
106
|
|
|
112
|
-
// Start
|
|
113
|
-
const
|
|
107
|
+
// Start server
|
|
108
|
+
const serverProcess = spawn('node', ['server/index.js'], {
|
|
114
109
|
cwd: packageRoot,
|
|
115
110
|
stdio: ['inherit', 'pipe', 'inherit'],
|
|
116
111
|
env: process.env
|
|
117
112
|
});
|
|
118
113
|
|
|
119
|
-
let viteProcess = null;
|
|
120
114
|
let serverReady = false;
|
|
121
115
|
|
|
122
|
-
// Wait for
|
|
123
|
-
|
|
116
|
+
// Wait for server to be ready before opening browser
|
|
117
|
+
serverProcess.stdout.on('data', async (data) => {
|
|
124
118
|
process.stdout.write(data);
|
|
125
119
|
const output = data.toString();
|
|
126
120
|
|
|
127
121
|
if (!serverReady && output.includes(SERVER_READY_MESSAGE)) {
|
|
128
122
|
serverReady = true;
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
'--port', port,
|
|
135
|
-
...(shouldOpen ? ['--open'] : [])
|
|
136
|
-
], {
|
|
137
|
-
cwd: packageRoot,
|
|
138
|
-
stdio: 'inherit',
|
|
139
|
-
env: process.env
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
viteProcess.on('error', (err) => {
|
|
143
|
-
console.error('Vite server error:', err.message);
|
|
144
|
-
});
|
|
123
|
+
|
|
124
|
+
if (shouldOpen) {
|
|
125
|
+
const openModule = await import('open');
|
|
126
|
+
openModule.default(`http://localhost:${port}`);
|
|
127
|
+
}
|
|
145
128
|
}
|
|
146
129
|
});
|
|
147
130
|
|
|
148
131
|
// Handle graceful shutdown
|
|
149
132
|
const shutdown = () => {
|
|
150
133
|
console.log('\nShutting down...');
|
|
151
|
-
|
|
152
|
-
viteProcess?.kill('SIGINT');
|
|
134
|
+
serverProcess.kill('SIGINT');
|
|
153
135
|
process.exit(0);
|
|
154
136
|
};
|
|
155
137
|
|
|
156
138
|
process.on('SIGINT', shutdown);
|
|
157
139
|
process.on('SIGTERM', shutdown);
|
|
158
140
|
|
|
159
|
-
// Handle
|
|
160
|
-
|
|
141
|
+
// Handle server exit
|
|
142
|
+
serverProcess.on('exit', (code) => {
|
|
161
143
|
if (code !== 0 && code !== null) {
|
|
162
|
-
console.error(`
|
|
144
|
+
console.error(`Server exited with code ${code}`);
|
|
163
145
|
}
|
|
164
|
-
viteProcess?.kill('SIGINT');
|
|
165
146
|
process.exit(code || 0);
|
|
166
147
|
});
|
|
167
148
|
|
|
168
|
-
|
|
169
|
-
console.error('Failed to start
|
|
149
|
+
serverProcess.on('error', (err) => {
|
|
150
|
+
console.error('Failed to start server:', err.message);
|
|
170
151
|
process.exit(1);
|
|
171
152
|
});
|
package/package.json
CHANGED
package/server/index.js
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
// server/index.js
|
|
2
2
|
import { Hono } from 'hono';
|
|
3
3
|
import { serve } from '@hono/node-server';
|
|
4
|
+
import { serveStatic } from '@hono/node-server/serve-static';
|
|
4
5
|
import { readFile, readdir, stat } from 'fs/promises';
|
|
5
|
-
import { basename, join, relative, resolve } from 'path';
|
|
6
|
+
import { basename, join, relative, resolve, dirname } from 'path';
|
|
7
|
+
import { fileURLToPath } from 'url';
|
|
8
|
+
|
|
9
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
10
|
+
const __dirname = dirname(__filename);
|
|
11
|
+
const packageRoot = resolve(__dirname, '..');
|
|
12
|
+
const distDir = resolve(packageRoot, 'dist');
|
|
6
13
|
|
|
7
14
|
// Port validation function
|
|
8
15
|
function validatePort(value) {
|
|
@@ -142,6 +149,20 @@ app.get('/api/markdown/:path{.+}', async (c) => {
|
|
|
142
149
|
}
|
|
143
150
|
});
|
|
144
151
|
|
|
152
|
+
// Serve static files from dist directory (for production/CLI mode)
|
|
153
|
+
app.use('/*', serveStatic({ root: relative(process.cwd(), distDir) || '.' }));
|
|
154
|
+
|
|
155
|
+
// Fallback to index.html for SPA routing
|
|
156
|
+
app.get('*', async (c) => {
|
|
157
|
+
try {
|
|
158
|
+
const indexPath = resolve(distDir, 'index.html');
|
|
159
|
+
const html = await readFile(indexPath, 'utf-8');
|
|
160
|
+
return c.html(html);
|
|
161
|
+
} catch (err) {
|
|
162
|
+
return c.text('Not found', 404);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
|
|
145
166
|
const SERVER_READY_MESSAGE = 'md-review server started';
|
|
146
167
|
|
|
147
168
|
startServer(app, PORT).then((actualPort) => {
|