doc-render-sdk 0.0.3 → 0.0.4

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.
@@ -0,0 +1,348 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Doc SDK CLI 工具
5
+ */
6
+
7
+ const { program } = require('commander');
8
+ const path = require('path');
9
+ const fs = require('fs');
10
+ const { spawn } = require('child_process');
11
+ const sdkVersion = require('../package.json').version;
12
+ program
13
+ .name('doc-render-sdk')
14
+ .description('Doc SDK CLI - 文档站点构建工具')
15
+ .version('1.0.0');
16
+
17
+ // 创建新项目
18
+ program
19
+ .command('create <project-name>')
20
+ .description('创建新的文档项目')
21
+ .option('-t, --template <template>', '使用模板', 'default')
22
+ .action(async (projectName, options) => {
23
+ console.log(`🚀 创建文档项目: ${projectName}`);
24
+
25
+ const projectDir = path.resolve(projectName);
26
+
27
+ if (fs.existsSync(projectDir)) {
28
+ console.error(`❌ 目录已存在: ${projectDir}`);
29
+ process.exit(1);
30
+ }
31
+
32
+ try {
33
+ await createProject(projectDir, options.template);
34
+ console.log('✅ 项目创建成功!');
35
+ console.log(`📁 项目目录: ${projectDir}`);
36
+ console.log('\n下一步:');
37
+ console.log(` cd ${projectName}`);
38
+ console.log(' npm install');
39
+ console.log(' npm run dev');
40
+ } catch (error) {
41
+ console.error('❌ 创建失败:', error.message);
42
+ process.exit(1);
43
+ }
44
+ });
45
+
46
+ // 开发服务器
47
+ program
48
+ .command('dev')
49
+ .description('启动开发服务器')
50
+ .option('-p, --port <port>', '端口号', '8080')
51
+ .option('-h, --host <host>', '主机地址', 'localhost')
52
+ .action((options) => {
53
+ console.log('🚀 启动开发服务器...');
54
+
55
+ const configPath = findConfig();
56
+ if (!configPath) {
57
+ console.error('❌ 未找到配置文件');
58
+ process.exit(1);
59
+ }
60
+
61
+ startDevServer(configPath, options);
62
+ });
63
+
64
+ // 构建项目
65
+ program
66
+ .command('build')
67
+ .description('构建文档站点')
68
+ .option('-o, --output <dir>', '输出目录', 'dist')
69
+ .action((options) => {
70
+ console.log('📦 构建文档站点...');
71
+
72
+ const configPath = findConfig();
73
+ if (!configPath) {
74
+ console.error('❌ 未找到配置文件');
75
+ process.exit(1);
76
+ }
77
+
78
+ buildProject(configPath, options);
79
+ });
80
+
81
+
82
+ // 预览构建结果
83
+ program
84
+ .command('preview')
85
+ .description('预览构建结果')
86
+ .option('-p, --port <port>', '端口号', '3000')
87
+ .option('-d, --dir <dir>', '构建目录', 'dist')
88
+ .action((options) => {
89
+ console.log('👀 预览构建结果...');
90
+ previewBuild(options);
91
+ });
92
+
93
+ program.parse();
94
+
95
+ /**
96
+ * 创建新项目
97
+ */
98
+ async function createProject(projectDir, template) {
99
+ // 创建项目目录
100
+ fs.mkdirSync(projectDir, { recursive: true });
101
+
102
+ // 生成package.json
103
+ const packageJson = {
104
+ name: path.basename(projectDir),
105
+ version: '1.0.0',
106
+ description: 'Documentation site built with Doc SDK',
107
+ main: 'index.js',
108
+ scripts: {
109
+ dev: 'doc-render-sdk dev',
110
+ build: 'doc-render-sdk build',
111
+ preview: 'doc-render-sdk preview'
112
+ },
113
+ dependencies: {
114
+ 'doc-render-sdk': sdkVersion
115
+ },
116
+ devDependencies: {
117
+ webpack: '^5.88.0',
118
+ 'webpack-cli': '^5.1.0',
119
+ 'webpack-dev-server': '^4.15.0'
120
+ }
121
+ };
122
+
123
+ fs.writeFileSync(
124
+ path.join(projectDir, 'package.json'),
125
+ JSON.stringify(packageJson, null, 2)
126
+ );
127
+
128
+ // 生成配置文件
129
+ const config = {
130
+ title: 'My Documentation',
131
+ description: 'Component documentation built with Doc SDK',
132
+ version: '1.0.0',
133
+ components: {
134
+ 'example': {
135
+ label: 'Example Component',
136
+ description: 'An example component to get you started',
137
+ demos: [
138
+ {
139
+ title: 'Basic Usage',
140
+ desc: 'Basic usage of the component',
141
+ source: 'basic'
142
+ }
143
+ ],
144
+ apis: [
145
+ {
146
+ title: 'Example',
147
+ apiKey: 'Example'
148
+ }
149
+ ]
150
+ }
151
+ }
152
+ };
153
+
154
+ fs.writeFileSync(
155
+ path.join(projectDir, 'doc.config.js'),
156
+ `export default ${JSON.stringify(config, null, 2)};`
157
+ );
158
+
159
+ // 生成入口文件
160
+ const indexJs = `import DocSDK from 'doc-render-sdk';
161
+ import config from './doc.config.js';
162
+
163
+ // 注册示例组件
164
+ const ExampleComponent = () => {
165
+ return (
166
+ <div style={{ padding: '20px' }}>
167
+ <h3>Example Component</h3>
168
+ <p>This is an example component.</p>
169
+ </div>
170
+ );
171
+ };
172
+
173
+ // 注册全局组件
174
+ window.__DOC_SDK_DEMOS__ = {
175
+ 'example': {
176
+ 'basic': ExampleComponent
177
+ }
178
+ };
179
+
180
+ window.__DOC_SDK_DEMO_CODES__ = {
181
+ 'example': {
182
+ 'basic': \`const ExampleComponent = () => {
183
+ return (
184
+ <div style={{ padding: '20px' }}>
185
+ <h3>Example Component</h3>
186
+ <p>This is an example component.</p>
187
+ </div>
188
+ );
189
+ };\`
190
+ }
191
+ };
192
+
193
+ window.__DOC_SDK_APIS__ = {
194
+ 'example': {
195
+ 'Example': [
196
+ {
197
+ param: 'children',
198
+ type: 'ReactNode',
199
+ desc: 'The content of the component',
200
+ option: '',
201
+ default: '',
202
+ required: false
203
+ }
204
+ ]
205
+ }
206
+ };
207
+
208
+ const docSdk = new DocSDK(config);
209
+ docSdk.render('#app');
210
+ `;
211
+
212
+ fs.writeFileSync(path.join(projectDir, 'index.js'), indexJs);
213
+
214
+ // 生成HTML文件
215
+ const indexHtml = `<!DOCTYPE html>
216
+ <html lang="zh-CN">
217
+ <head>
218
+ <meta charset="UTF-8">
219
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
220
+ <title>My Documentation</title>
221
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css">
222
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>
223
+ </head>
224
+ <body>
225
+ <div id="app"></div>
226
+ </body>
227
+ </html>`;
228
+
229
+ fs.writeFileSync(path.join(projectDir, 'index.html'), indexHtml);
230
+
231
+ // 生成README
232
+ const readme = `# ${path.basename(projectDir)}
233
+
234
+ Documentation site built with Doc SDK.
235
+
236
+ ## Getting Started
237
+
238
+ \`\`\`bash
239
+ npm install
240
+ npm run dev
241
+ \`\`\`
242
+
243
+ ## Commands
244
+
245
+ - \`npm run dev\` - Start development server
246
+ - \`npm run build\` - Build for production
247
+ - \`npm run preview\` - Preview build
248
+
249
+ ## Documentation
250
+
251
+ - [Doc SDK Documentation](https://github.com/Sunny-117/doc-render-sdk)
252
+ `;
253
+
254
+ fs.writeFileSync(path.join(projectDir, 'README.md'), readme);
255
+ }
256
+
257
+ /**
258
+ * 查找配置文件
259
+ */
260
+ function findConfig() {
261
+ const configFiles = [
262
+ 'doc.config.js',
263
+ 'doc.config.json',
264
+ 'docs.config.js',
265
+ 'docs.config.json'
266
+ ];
267
+
268
+ for (const file of configFiles) {
269
+ if (fs.existsSync(file)) {
270
+ return path.resolve(file);
271
+ }
272
+ }
273
+
274
+ return null;
275
+ }
276
+
277
+ /**
278
+ * 启动开发服务器
279
+ */
280
+ function startDevServer(configPath, options) {
281
+ const webpackConfig = generateWebpackConfig(configPath, 'development', options);
282
+ const configFile = path.join(__dirname, '../webpack.temp.js');
283
+
284
+ fs.writeFileSync(configFile, `module.exports = ${JSON.stringify(webpackConfig, null, 2)};`);
285
+
286
+ const child = spawn('npx', ['webpack', 'serve', '--config', configFile], {
287
+ stdio: 'inherit',
288
+ shell: true
289
+ });
290
+
291
+ child.on('close', (code) => {
292
+ fs.unlinkSync(configFile);
293
+ process.exit(code);
294
+ });
295
+ }
296
+
297
+ /**
298
+ * 构建项目
299
+ */
300
+ function buildProject(configPath, options) {
301
+ const webpackConfig = generateWebpackConfig(configPath, 'production', options);
302
+ const configFile = path.join(__dirname, '../webpack.temp.js');
303
+
304
+ fs.writeFileSync(configFile, `module.exports = ${JSON.stringify(webpackConfig, null, 2)};`);
305
+
306
+ const child = spawn('npx', ['webpack', '--config', configFile], {
307
+ stdio: 'inherit',
308
+ shell: true
309
+ });
310
+
311
+ child.on('close', (code) => {
312
+ fs.unlinkSync(configFile);
313
+ if (code === 0) {
314
+ console.log('✅ 构建完成!');
315
+ }
316
+ process.exit(code);
317
+ });
318
+ }
319
+
320
+ /**
321
+ * 预览构建结果
322
+ */
323
+ function previewBuild(options) {
324
+ const express = require('express');
325
+ const app = express();
326
+
327
+ app.use(express.static(options.dir));
328
+
329
+ app.listen(options.port, () => {
330
+ console.log(`📖 预览地址: http://localhost:${options.port}`);
331
+ });
332
+ }
333
+
334
+ /**
335
+ * 生成webpack配置
336
+ */
337
+ function generateWebpackConfig(configPath, mode, options) {
338
+ // 这里应该生成完整的webpack配置
339
+ // 简化版本,实际使用时需要完善
340
+ return {
341
+ mode,
342
+ entry: './index.js',
343
+ output: {
344
+ path: path.resolve(options.output || 'dist'),
345
+ filename: 'bundle.js'
346
+ }
347
+ };
348
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doc-render-sdk",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "A powerful documentation rendering SDK for component libraries",
5
5
  "main": "dist/index.mjs",
6
6
  "module": "dist/index.mjs",