From e82bb63c23e27f8094083be6057e724a5fd7c0fa Mon Sep 17 00:00:00 2001 From: lyfeyaj Date: Fri, 9 Jun 2023 10:40:13 +0800 Subject: [PATCH] =?UTF-8?q?feat(takin):=20=E4=BC=98=E5=8C=96=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=99=A8=E9=80=BB=E8=BE=91=E5=A2=9E=E5=8A=A0=E6=A8=A1?= =?UTF-8?q?=E7=89=88=E8=A7=A3=E6=9E=90=E5=AF=B9=20<%=5F=20=E6=88=96=20=5F%?= =?UTF-8?q?>=20=E6=88=96=20-%>=20=E7=9A=84=E6=94=AF=E6=8C=81=20(#57)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/takin/src/generator.ts | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/takin/src/generator.ts b/packages/takin/src/generator.ts index 4502c99d..3c0397ea 100644 --- a/packages/takin/src/generator.ts +++ b/packages/takin/src/generator.ts @@ -321,12 +321,14 @@ export class Generator implements Required { * 生成终端的 prompts 问题 */ async prompting() { - this.answers = await prompts(this.questions, { - onCancel() { - // 用户取消时自动退出 - process.exit(0) - } - }) + if (this.questions?.length) { + this.answers = await prompts(this.questions, { + onCancel() { + // 用户取消时自动退出 + process.exit(0) + } + }) + } } /** @@ -361,10 +363,21 @@ export class Generator implements Required { /** * 拷贝模版文件 + * + * 基于 lodash.template 方法来解析模版,并增加了对 + * 1. <%_ _%> 的支持,可用于移除前后的空格或 Tab 符号 + * 2. 增加了对 -%> 的支持,可用于移除控制语句引入的换行符 * @param opts - 拷贝选项 */ async copyTemplate(opts: GeneratorCopyOptions) { - const tpl = await fs.readFile(opts.path, 'utf-8') + let tpl = await fs.readFile(opts.path, 'utf-8') + + // 移除控制语句前 <%_ _%> 后的空格或 Tab 符号 + tpl = tpl.replace(/[ \t]*<%_/gm, '<%').replace(/_%>[ \t]*/gm, '%>') + + // 移除控制语句结尾引入的换行符 + tpl = tpl.replace(/-%>(?:\r\n|\r|\n)?/gm, '%>') + const content = template(tpl)(opts.context) await fs.mkdirp(dirname(opts.to)) logger.success(`写入: ${relative(this.baseDir, opts.to)}`)