|
| 1 | +import { promises as fs } from 'fs'; |
| 2 | +import path, { dirname } from 'path'; |
| 3 | +import { fileURLToPath } from 'url'; |
| 4 | + |
| 5 | +import generateChangelogJson from '@tdesign/common-docs/plugins/changelog-to-json'; |
| 6 | + |
| 7 | +// eslint-disable-next-line no-underscore-dangle |
| 8 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 9 | +const distDir = path.resolve(__dirname, '../../dist'); |
| 10 | +const rootDir = path.resolve(__dirname, '../../../'); |
| 11 | + |
| 12 | +const changelogConfigs = [ |
| 13 | + { |
| 14 | + input: 'CHANGELOG.md', |
| 15 | + output: 'changelog.json', |
| 16 | + }, |
| 17 | + { |
| 18 | + input: 'CHANGELOG.en-US.md', |
| 19 | + output: 'changelog.en-US.json', |
| 20 | + }, |
| 21 | +]; |
| 22 | + |
| 23 | +async function generate(input) { |
| 24 | + return generateChangelogJson(path.resolve(rootDir, input), 'chat'); |
| 25 | +} |
| 26 | + |
| 27 | +export default function changelog2Json() { |
| 28 | + return { |
| 29 | + name: 'changelog-to-json', |
| 30 | + configureServer(server) { |
| 31 | + // 开发模式时拦截请求 |
| 32 | + changelogConfigs.forEach(({ input, output }) => { |
| 33 | + server.middlewares.use(`/${output}`, async (_, res) => { |
| 34 | + const json = await generate(input); |
| 35 | + res.setHeader('Content-Type', 'application/json'); |
| 36 | + res.end(JSON.stringify(json)); |
| 37 | + }); |
| 38 | + }); |
| 39 | + }, |
| 40 | + |
| 41 | + async closeBundle(error) { |
| 42 | + // 构建失败时跳过 |
| 43 | + if (error) return; |
| 44 | + // 生产构建时写入物理文件 |
| 45 | + if (process.env.NODE_ENV !== 'production') return; |
| 46 | + await Promise.all( |
| 47 | + changelogConfigs.map(async ({ input, output }) => { |
| 48 | + const json = await generate(input); |
| 49 | + await fs.writeFile(path.resolve(distDir, output), JSON.stringify(json)); |
| 50 | + }), |
| 51 | + ); |
| 52 | + }, |
| 53 | + }; |
| 54 | +} |
0 commit comments