Skip to content

Commit

Permalink
🎉feat: support generate different template~
Browse files Browse the repository at this point in the history
  • Loading branch information
LHammer committed May 9, 2018
1 parent 50a9e92 commit 1ed97cf
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
39 changes: 37 additions & 2 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
*/
const Metalsmith = require('metalsmith') // 批量处理模板
const Handlebars = require('handlebars') // Mustache风格的模板引擎:{{foo}}
const render = require('consolidate').handlebars.render // 支持各种模板引擎的渲染
const chalk = require('chalk') // 终端打印信息美化
const async = require('async') // 强大的异步处理
const multimatch = require('multimatch') // 支持多个条件的匹配
const getOptions = require('../lib/options')
const logger = require('./logger')
const ask = require('./ask')
Expand All @@ -28,7 +31,7 @@ Handlebars.registerHelper('unless_eq', function (a, b, opts) {
/**
* Create a middleware for asking questions.
*
* @param {Object} prompts
* @param {Object} prompts meta.js中prompts对象
* @return {Function}
*/
function askQuestions (prompts) {
Expand All @@ -40,7 +43,7 @@ function askQuestions (prompts) {
/**
* Create a middleware for filtering files.
*
* @param {Object} filters
* @param {Object} filters meta.js中filters对象
* @return {Function}
*/
function filterFiles (filters) {
Expand All @@ -49,6 +52,37 @@ function filterFiles (filters) {
}
}

/**
* Create a middleware for render template files.
*
* @param {Object} metalsmith metalsmith对象
*/
function renderTemplateFiles (skipInterpolation) {
return (files, metalsmith, done) => {
const keys = Object.keys(files)
const metalsmithMetadata = metalsmith.metadata()

async.each(keys, (file, next) => {
if (skipInterpolation && multimatch([file], skipInterpolation, { dot: true }).length) {
return next()
}
const str = files[file].contents.toString()
// 跳过不符合handlebars语法的file
if (!/{{([^{}]+)}}/g.test(str)) {
return next()
}
render(str, metalsmithMetadata, (err, res) => {
if (err) {
err.message = `[${file}] ${err.message}`
return next(err)
}
files[file].contents = Buffer.from(res)
next()
})
}, done)
}
}

/**
* Generate a template
*
Expand All @@ -74,6 +108,7 @@ module.exports = (name, src, dest, done) => {
metalsmith
.use(askQuestions(opts.prompts))
.use(filterFiles(opts.filters))
.use(renderTemplateFiles(opts.skipInterpolation))

metalsmith
.clean(false)
Expand Down
36 changes: 31 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
"async": "^2.6.0",
"chalk": "^2.4.1",
"commander": "^2.15.1",
"consolidate": "^0.15.1",
"download-git-repo": "^1.0.2",
"handlebars": "^4.0.11",
"ini": "^1.3.5",
"inquirer": "^5.2.0",
"metalsmith": "^2.3.0",
"minimatch": "^3.0.4",
"multimatch": "^2.1.0",
"ora": "^2.1.0",
"request": "^2.85.0",
"rimraf": "^2.6.2",
Expand Down

0 comments on commit 1ed97cf

Please sign in to comment.