doc-render-sdk 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/doc-render-sdk.js +113 -68
- package/package.json +1 -1
package/bin/doc-render-sdk.js
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* Doc SDK CLI 工具
|
|
5
5
|
*/
|
|
6
|
-
|
|
7
6
|
const { program } = require('commander');
|
|
8
7
|
const path = require('path');
|
|
9
8
|
const fs = require('fs');
|
|
@@ -49,7 +48,7 @@ program
|
|
|
49
48
|
.description('启动开发服务器')
|
|
50
49
|
.option('-p, --port <port>', '端口号', '8080')
|
|
51
50
|
.option('-h, --host <host>', '主机地址', 'localhost')
|
|
52
|
-
.action((options) => {
|
|
51
|
+
.action(async (options) => {
|
|
53
52
|
console.log('🚀 启动开发服务器...');
|
|
54
53
|
|
|
55
54
|
const configPath = findConfig();
|
|
@@ -58,7 +57,7 @@ program
|
|
|
58
57
|
process.exit(1);
|
|
59
58
|
}
|
|
60
59
|
|
|
61
|
-
startDevServer(configPath, options);
|
|
60
|
+
await startDevServer(configPath, options);
|
|
62
61
|
});
|
|
63
62
|
|
|
64
63
|
// 构建项目
|
|
@@ -66,7 +65,7 @@ program
|
|
|
66
65
|
.command('build')
|
|
67
66
|
.description('构建文档站点')
|
|
68
67
|
.option('-o, --output <dir>', '输出目录', 'dist')
|
|
69
|
-
.action((options) => {
|
|
68
|
+
.action(async (options) => {
|
|
70
69
|
console.log('📦 构建文档站点...');
|
|
71
70
|
|
|
72
71
|
const configPath = findConfig();
|
|
@@ -75,7 +74,7 @@ program
|
|
|
75
74
|
process.exit(1);
|
|
76
75
|
}
|
|
77
76
|
|
|
78
|
-
buildProject(configPath, options);
|
|
77
|
+
await buildProject(configPath, options);
|
|
79
78
|
});
|
|
80
79
|
|
|
81
80
|
|
|
@@ -105,19 +104,18 @@ async function createProject(projectDir, template) {
|
|
|
105
104
|
version: '1.0.0',
|
|
106
105
|
description: 'Documentation site built with Doc SDK',
|
|
107
106
|
main: 'index.js',
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
107
|
+
scripts: {
|
|
108
|
+
dev: 'doc-render-sdk dev',
|
|
109
|
+
build: 'doc-render-sdk build',
|
|
110
|
+
preview: 'doc-render-sdk preview'
|
|
111
|
+
},
|
|
112
|
+
dependencies: {
|
|
113
|
+
'doc-render-sdk': sdkVersion
|
|
114
|
+
},
|
|
115
|
+
devDependencies: {
|
|
116
|
+
vite: '^5.0.0',
|
|
117
|
+
'@vitejs/plugin-react': '^3.1.0'
|
|
118
|
+
}
|
|
121
119
|
};
|
|
122
120
|
|
|
123
121
|
fs.writeFileSync(
|
|
@@ -223,6 +221,7 @@ docSdk.render('#app');
|
|
|
223
221
|
</head>
|
|
224
222
|
<body>
|
|
225
223
|
<div id="app"></div>
|
|
224
|
+
<script src="index.js"></script>
|
|
226
225
|
</body>
|
|
227
226
|
</html>`;
|
|
228
227
|
|
|
@@ -278,71 +277,117 @@ function findConfig() {
|
|
|
278
277
|
* 启动开发服务器
|
|
279
278
|
*/
|
|
280
279
|
function startDevServer(configPath, options) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
280
|
+
// Use Vite Node API to create a dev server via createViteServer
|
|
281
|
+
(async () => {
|
|
282
|
+
try {
|
|
283
|
+
const server = await createViteServer({
|
|
284
|
+
root: process.cwd(),
|
|
285
|
+
server: {
|
|
286
|
+
port: Number(options.port) || 8080,
|
|
287
|
+
host: options.host || 'localhost'
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
await server.listen();
|
|
292
|
+
server.printUrls();
|
|
293
|
+
} catch (err) {
|
|
294
|
+
console.error('❌ 启动 Vite 开发服务器失败:', err);
|
|
295
|
+
process.exit(1);
|
|
296
|
+
}
|
|
297
|
+
})();
|
|
295
298
|
}
|
|
296
299
|
|
|
297
300
|
/**
|
|
298
301
|
* 构建项目
|
|
299
302
|
*/
|
|
300
303
|
function buildProject(configPath, options) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
304
|
+
// Use Vite build API with plugin-react
|
|
305
|
+
(async () => {
|
|
306
|
+
try {
|
|
307
|
+
const { build } = require('vite');
|
|
308
|
+
const outDir = options.output || 'dist';
|
|
309
|
+
|
|
310
|
+
console.log('📦 running vite build...');
|
|
311
|
+
|
|
312
|
+
await build({
|
|
313
|
+
root: process.cwd(),
|
|
314
|
+
build: {
|
|
315
|
+
outDir
|
|
316
|
+
},
|
|
317
|
+
plugins: createVitePlugins()
|
|
318
|
+
});
|
|
310
319
|
|
|
311
|
-
child.on('close', (code) => {
|
|
312
|
-
fs.unlinkSync(configFile);
|
|
313
|
-
if (code === 0) {
|
|
314
320
|
console.log('✅ 构建完成!');
|
|
321
|
+
} catch (err) {
|
|
322
|
+
console.error('❌ Vite 构建失败:', err);
|
|
323
|
+
process.exit(1);
|
|
315
324
|
}
|
|
316
|
-
|
|
317
|
-
});
|
|
325
|
+
})();
|
|
318
326
|
}
|
|
319
327
|
|
|
320
328
|
/**
|
|
321
|
-
*
|
|
329
|
+
* Create Vite server with shared plugins
|
|
322
330
|
*/
|
|
323
|
-
function
|
|
324
|
-
const
|
|
325
|
-
const
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
331
|
+
function createViteServer(options = {}) {
|
|
332
|
+
const { createServer } = require('vite');
|
|
333
|
+
const plugins = createVitePlugins();
|
|
334
|
+
return createServer({ ...options, plugins });
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function createVitePlugins() {
|
|
338
|
+
try {
|
|
339
|
+
const reactPlugin = require('@vitejs/plugin-react');
|
|
340
|
+
return [reactPlugin()];
|
|
341
|
+
} catch (err) {
|
|
342
|
+
// If plugin not installed, return empty array and let Vite warn later
|
|
343
|
+
return [];
|
|
344
|
+
}
|
|
332
345
|
}
|
|
333
346
|
|
|
334
347
|
/**
|
|
335
|
-
*
|
|
348
|
+
* 预览构建结果
|
|
336
349
|
*/
|
|
337
|
-
function
|
|
338
|
-
//
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
350
|
+
function previewBuild(options) {
|
|
351
|
+
// Use Vite programmatic preview API if available, else fallback to npx vite preview
|
|
352
|
+
const distDir = options.dir || 'dist';
|
|
353
|
+
const port = Number(options.port) || 3000;
|
|
354
|
+
|
|
355
|
+
try {
|
|
356
|
+
const vite = require('vite');
|
|
357
|
+
if (typeof vite.preview === 'function') {
|
|
358
|
+
// Vite exposes preview function in some versions
|
|
359
|
+
(async () => {
|
|
360
|
+
try {
|
|
361
|
+
const server = await vite.preview({ root: process.cwd(), preview: { port } });
|
|
362
|
+
console.log(`📖 预览地址: http://localhost:${port}`);
|
|
363
|
+
} catch (err) {
|
|
364
|
+
console.error('❌ 启动 Vite preview 失败:', err);
|
|
365
|
+
process.exit(1);
|
|
366
|
+
}
|
|
367
|
+
})();
|
|
368
|
+
return;
|
|
346
369
|
}
|
|
347
|
-
|
|
348
|
-
|
|
370
|
+
|
|
371
|
+
// Fallback: try to create a server configured for preview
|
|
372
|
+
if (typeof vite.createServer === 'function') {
|
|
373
|
+
(async () => {
|
|
374
|
+
try {
|
|
375
|
+
const server = await vite.createServer({ root: process.cwd(), preview: { port } });
|
|
376
|
+
await server.listen();
|
|
377
|
+
console.log(`📖 预览地址: http://localhost:${port}`);
|
|
378
|
+
} catch (err) {
|
|
379
|
+
// continue to fallback
|
|
380
|
+
console.error('❌ 使用 createServer 作为 preview 启动失败,回退到 CLI:', err.message || err);
|
|
381
|
+
}
|
|
382
|
+
})();
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
} catch (err) {
|
|
386
|
+
// vite not installed locally, will fallback to npx
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// Final fallback: spawn npx vite preview
|
|
390
|
+
const args = ['vite', 'preview', '--port', String(port)];
|
|
391
|
+
const child = spawn('npx', args, { stdio: 'inherit', shell: true, cwd: process.cwd() });
|
|
392
|
+
child.on('close', (code) => process.exit(code));
|
|
393
|
+
}
|