|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import { fileURLToPath } from 'url'; |
| 4 | + |
| 5 | +// 获取当前文件的目录路径 |
| 6 | +const __filename = fileURLToPath(import.meta.url); |
| 7 | +const __dirname = path.dirname(__filename); |
| 8 | + |
| 9 | +// 获取版本号 |
| 10 | +const getPackageVersion = () => { |
| 11 | + const packageJson = JSON.parse( |
| 12 | + fs.readFileSync(path.join(__dirname, '../packages/tdesign-miniprogram/package.json'), 'utf8'), |
| 13 | + ); |
| 14 | + return packageJson.version; |
| 15 | +}; |
| 16 | + |
| 17 | +// 工具函数:同步复制文件 |
| 18 | +const copyFiles = (fromPath, toPath) => { |
| 19 | + try { |
| 20 | + fs.cpSync(fromPath, toPath, { recursive: true }); |
| 21 | + // eslint-disable-next-line no-console |
| 22 | + console.log('文件已成功复制:', fromPath, '->', toPath); |
| 23 | + } catch (err) { |
| 24 | + // eslint-disable-next-line no-console |
| 25 | + console.error('复制文件时出错:', err); |
| 26 | + throw err; |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | +// 工具函数:删除指定文件 |
| 31 | +const removeFiles = (targetPath, filterFiles, saveFileTypes) => { |
| 32 | + const files = fs.readdirSync(targetPath); |
| 33 | + |
| 34 | + files.forEach((file) => { |
| 35 | + const filePath = path.join(targetPath, file); |
| 36 | + const stat = fs.lstatSync(filePath); |
| 37 | + |
| 38 | + if (stat.isDirectory()) { |
| 39 | + if (filterFiles.includes(file)) { |
| 40 | + fs.rmSync(filePath, { recursive: true }); |
| 41 | + } |
| 42 | + } else { |
| 43 | + const fileExtension = file.slice(file.lastIndexOf('.') + 1); |
| 44 | + if (!saveFileTypes.includes(fileExtension)) { |
| 45 | + fs.unlinkSync(filePath); |
| 46 | + } |
| 47 | + } |
| 48 | + }); |
| 49 | +}; |
| 50 | + |
| 51 | +// 工具函数:替换文件内容 |
| 52 | +const replaceFileContent = (targetPath, componentName, version) => { |
| 53 | + const specialFileContent = [ |
| 54 | + { |
| 55 | + file: 'app.json', |
| 56 | + replaceContent: [['pages/button/button', `pages/${componentName}/${componentName}`]], |
| 57 | + }, |
| 58 | + { |
| 59 | + file: 'project.private.config.json', |
| 60 | + replaceContent: [['tdesign-button-demo', `tdesign-${componentName}-demo`]], |
| 61 | + }, |
| 62 | + { |
| 63 | + file: 'package.json', |
| 64 | + replaceContent: [ |
| 65 | + ['tdesign-button-demo', `tdesign-${componentName}-demo`], |
| 66 | + ['tdesign-miniprogram-version', version], |
| 67 | + ], |
| 68 | + }, |
| 69 | + ]; |
| 70 | + |
| 71 | + specialFileContent.forEach((item) => { |
| 72 | + const filePath = path.join(targetPath, item.file); |
| 73 | + let data = fs.readFileSync(filePath, 'utf8'); |
| 74 | + item.replaceContent.forEach(([oldContent, newContent]) => { |
| 75 | + data = data.replaceAll(oldContent, newContent); |
| 76 | + }); |
| 77 | + fs.writeFileSync(filePath, data, 'utf8'); |
| 78 | + }); |
| 79 | +}; |
| 80 | + |
| 81 | +// 主函数:生成 Demo 代码片段 |
| 82 | +const generateDemoSnippets = (componentName) => { |
| 83 | + if (!componentName) { |
| 84 | + // eslint-disable-next-line no-console |
| 85 | + console.error('错误:请提供组件名称'); |
| 86 | + process.exit(1); |
| 87 | + } |
| 88 | + |
| 89 | + const VERSION = getPackageVersion(); |
| 90 | + const filterFiles = ['skyline']; // 需要过滤的示例文件夹 |
| 91 | + const saveFileTypes = ['js', 'json', 'wxml', 'wxss']; // 需要保存的文件类型 |
| 92 | + |
| 93 | + const baseDemoPath = path.resolve(__dirname, './templates'); |
| 94 | + const snippetsTargetPath = path.resolve(__dirname, './__snippets__/'); |
| 95 | + const snippetsOriginPath = path.join(path.resolve(__dirname, '../_example/pages'), componentName); |
| 96 | + |
| 97 | + if (!fs.existsSync(snippetsOriginPath)) { |
| 98 | + // eslint-disable-next-line no-console |
| 99 | + console.log('示例代码路径不存在:', snippetsOriginPath); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + const targetPath = path.join(snippetsTargetPath, `tdesign-${componentName}-demo`); |
| 104 | + |
| 105 | + // 检查目标路径是否存在,存在则先移除 |
| 106 | + if (fs.existsSync(targetPath)) { |
| 107 | + fs.rmSync(targetPath, { recursive: true, force: true }); |
| 108 | + } |
| 109 | + |
| 110 | + // 1. 复制基础模板到目标路径 |
| 111 | + copyFiles(baseDemoPath, targetPath); |
| 112 | + |
| 113 | + // 2. 复制示例代码到目标路径 |
| 114 | + const cpDemoCodePath = path.join(targetPath, `pages/${componentName}`); |
| 115 | + copyFiles(snippetsOriginPath, cpDemoCodePath); |
| 116 | + |
| 117 | + // 3. 清理不需要的文件 |
| 118 | + removeFiles(cpDemoCodePath, filterFiles, saveFileTypes); |
| 119 | + |
| 120 | + // 4. 替换模板内容 |
| 121 | + replaceFileContent(targetPath, componentName, VERSION); |
| 122 | + |
| 123 | + // eslint-disable-next-line no-console |
| 124 | + console.log(`Demo 片段生成完成: ${componentName}`); |
| 125 | +}; |
| 126 | + |
| 127 | +/** |
| 128 | + * @description 命令行执行: node generate-demo-snippet.mjs button |
| 129 | + */ |
| 130 | +const COMPONENT_NAME = process.argv[2]; |
| 131 | +generateDemoSnippets(COMPONENT_NAME); |
0 commit comments