Skip to content

Commit

Permalink
feat(takin): 优化生成器逻辑增加模版解析对 <%_ 或 _%> 或 -%> 的支持 (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
lyfeyaj authored Jun 9, 2023
1 parent e2681c3 commit e82bb63
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions packages/takin/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,14 @@ export class Generator implements Required<GeneratorOptions> {
* 生成终端的 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)
}
})
}
}

/**
Expand Down Expand Up @@ -361,10 +363,21 @@ export class Generator implements Required<GeneratorOptions> {

/**
* 拷贝模版文件
*
* 基于 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)}`)
Expand Down

0 comments on commit e82bb63

Please sign in to comment.