|
| 1 | +import { readFileSync, writeFileSync, existsSync } from 'fs'; |
| 2 | +import { dirname } from 'path'; |
| 3 | +import { fileURLToPath } from 'url'; |
| 4 | + |
| 5 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 6 | +const rootDir = `${__dirname}/..`; |
| 7 | + |
| 8 | +// ============================================================ |
| 9 | +// 同步配置 |
| 10 | +// ============================================================ |
| 11 | +// source: 被监听的源文件 |
| 12 | +// targets: 同步目标列表 |
| 13 | +// - path: 目标文件路径 |
| 14 | +// - transform: 可选,内容转换函数名 |
| 15 | +// ============================================================ |
| 16 | + |
| 17 | +const transforms = { |
| 18 | + // 移除语言切换链接: [English](./README.md) | 简体中文 |
| 19 | + removeLangLink: (content) => { |
| 20 | + return content.replace(/\[English\]\(\.\/README\.md\) \| 简体中文\n?/g, ''); |
| 21 | + }, |
| 22 | +}; |
| 23 | + |
| 24 | +const syncConfig = [ |
| 25 | + { |
| 26 | + source: 'README.md', |
| 27 | + targets: [ |
| 28 | + { path: 'tdesign-component/README.md' }, |
| 29 | + ], |
| 30 | + }, |
| 31 | + { |
| 32 | + source: 'README_zh_CN.md', |
| 33 | + targets: [ |
| 34 | + { path: 'tdesign-component/README_zh_CN.md' }, |
| 35 | + { path: 'tdesign-site/site/docs/getting-started.md', transform: 'removeLangLink' }, |
| 36 | + ], |
| 37 | + }, |
| 38 | +]; |
| 39 | + |
| 40 | +// ============================================================ |
| 41 | +// 同步逻辑 |
| 42 | +// ============================================================ |
| 43 | + |
| 44 | +function sync() { |
| 45 | + for (const { source, targets } of syncConfig) { |
| 46 | + const sourcePath = `${rootDir}/${source}`; |
| 47 | + |
| 48 | + if (!existsSync(sourcePath)) { |
| 49 | + console.log(`[Skip] Source not found: ${source}`); |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + const sourceContent = readFileSync(sourcePath, 'utf-8'); |
| 54 | + console.log(`[Read] ${source}`); |
| 55 | + |
| 56 | + for (const { path, transform } of targets) { |
| 57 | + let content = sourceContent; |
| 58 | + |
| 59 | + if (transform) { |
| 60 | + const transformFn = transforms[transform]; |
| 61 | + if (transformFn) { |
| 62 | + content = transformFn(content); |
| 63 | + console.log(` [Transform: ${transform}]`); |
| 64 | + } else { |
| 65 | + console.log(` [Warn] Unknown transform: ${transform}`); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + writeFileSync(`${rootDir}/${path}`, content, 'utf-8'); |
| 70 | + console.log(` [Sync] -> ${path}`); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + console.log('\nDone!'); |
| 75 | +} |
| 76 | + |
| 77 | +sync(); |
0 commit comments