From c2541d265aab1dc8ccfb03a2315f511fdb5fe20c Mon Sep 17 00:00:00 2001 From: lexmin0412 Date: Sat, 11 May 2024 15:32:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor(api):=20=E7=94=9F=E6=88=90=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E6=8A=BD=E7=A6=BB=E4=B8=BA=E5=8D=95=E7=8B=AC=E7=9A=84?= =?UTF-8?q?=20Generator=20=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/api/src/api/generate.ts | 26 +++--------------- packages/api/src/utils/index.ts | 45 +++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 23 deletions(-) diff --git a/packages/api/src/api/generate.ts b/packages/api/src/api/generate.ts index b10eb20..f59701f 100644 --- a/packages/api/src/api/generate.ts +++ b/packages/api/src/api/generate.ts @@ -1,6 +1,4 @@ -import { loadBlock } from "../utils"; -import { getConfig } from "@readue/config" -import type { ReadueBlockFunction } from '@readue/config' +import { Generator } from "../utils"; /** * 判断是否是私有包 @@ -52,23 +50,7 @@ export const generate4SinglePkg = (pkgJson: Record) => { * 生成 Monorepo 的 README 内容 * @param pkgJson 仓库的 package.json 内容 */ -export const generate4Monorepo = (pkgJson: Record, cwd: string) => { - let readmeLines: string[] = [] - const config = getConfig(cwd) - - // 根据 列表生成 README.md 中的信息 - const blockFuncs = loadBlock(process.cwd()) - blockFuncs?.map((funcItem: ReadueBlockFunction)=>{ - const { name, content } = funcItem(config, pkgJson) - console.log(`[info] 执行块插件 ${name} - 开始`); - readmeLines = [ - ...readmeLines, - ...content, - '' - ] - console.log(`[info] 执行块插件 ${name} - 结束`); - console.log('') - }) - - return readmeLines; +export const generate4Monorepo = (_pkgJson: Record, cwd: string) => { + const generator = new Generator(cwd) + return generator.loadBlocks().gen() }; diff --git a/packages/api/src/utils/index.ts b/packages/api/src/utils/index.ts index 6ca30c4..d787f2c 100644 --- a/packages/api/src/utils/index.ts +++ b/packages/api/src/utils/index.ts @@ -1,6 +1,6 @@ import * as path from 'path' import * as fs from 'fs' -import { getConfig } from '@readue/config' +import { ReadueBlockFunction, getConfig } from '@readue/config' // 简单判断,当存在 pnpm-workspace.yaml 文件时,则认为是 monorepo export const isMonorepo = (cwd: string) => { @@ -13,3 +13,46 @@ export const loadBlock = (cwd: string) => { return require(item).default }) } + +/** + * 内容生成器 + */ +export class Generator { + + constructor(public cwd: string = process.cwd()) {} + + /** + * 块插件列表 + */ + blocks: ReadueBlockFunction[] = [] + + /** + * 加载块插件 + */ + loadBlocks() { + const config = getConfig(this.cwd) + config.blocks?.list.forEach(element => { + this.blocks.push(require(element).default as ReadueBlockFunction) + }); + return this + } + + /** + * 生成内容 + */ + gen() { + const config = getConfig(this.cwd) + const content = fs.readFileSync(path.resolve(this.cwd, 'package.json')).toString() + const pkgJson = JSON.parse(content) + + const contentLines: string[] = [] + this.blocks.forEach(block=>{ + const { name, content } = block(config, pkgJson) + console.log(`[info] 执行块插件 ${name} - 开始`); + contentLines.push(...content, '') + console.log(`[info] 执行块插件 ${name} - 结束`); + console.log('') + }) + return contentLines + } +}