Skip to content

Commit

Permalink
add skipInterpolation option to specify files that should skip render…
Browse files Browse the repository at this point in the history
…ing (vuejs#225)

* add skipInterpolation option to specify files that should not be rendered with handlebars

* add skipInterpolation description
  • Loading branch information
李文富 authored and yyx990803 committed Nov 5, 2016
1 parent 1795cc1 commit 27af417
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 17 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ Files under `test` will only be generated if the user answered yes to the prompt

Note that the `dot` option for minimatch is set to `true` so glob patterns would also match dotfiles by default.

#### Skip rendering

The `skipInterpolation` field in the metadata file should be a [minimatch glob pattern](https://github.com/isaacs/minimatch). The files matched should skip rendering. Example:

``` json
{
"skipInterpolation": "src/**/*.vue"
}
```

#### Additional data available in meta.{js,json}

- `destDirName` - destination directory name
Expand Down
39 changes: 23 additions & 16 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var Handlebars = require('handlebars')
var async = require('async')
var render = require('consolidate').handlebars.render
var path = require('path')
var match = require('minimatch')
var getOptions = require('./options')
var ask = require('./ask')
var filter = require('./filter')
Expand Down Expand Up @@ -43,7 +44,7 @@ module.exports = function generate (name, src, dest, done) {
metalsmith
.use(askQuestions(opts.prompts))
.use(filterFiles(opts.filters))
.use(renderTemplateFiles)
.use(renderTemplateFiles(opts.skipInterpolation))
.clean(false)
.source('.') // start from template root instead of `./src` which is Metalsmith's default for `source`
.destination(dest)
Expand Down Expand Up @@ -89,21 +90,27 @@ function filterFiles (filters) {
* @param {Function} done
*/

function renderTemplateFiles (files, metalsmith, done) {
var keys = Object.keys(files)
var metalsmithMetadata = metalsmith.metadata()
async.each(keys, function (file, next) {
var str = files[file].contents.toString()
// do not attempt to render files that do not have mustaches
if (!/{{([^{}]+)}}/g.test(str)) {
return next()
}
render(str, metalsmithMetadata, function (err, res) {
if (err) return next(err)
files[file].contents = new Buffer(res)
next()
})
}, done)
function renderTemplateFiles (skipInterpolation) {
return function (files, metalsmith, done) {
var keys = Object.keys(files)
var metalsmithMetadata = metalsmith.metadata()
async.each(keys, function (file, next) {
// skipping files with skipInterpolation option
if (skipInterpolation && match(file, skipInterpolation, { dot: true })) {
return next()
}
var str = files[file].contents.toString()
// do not attempt to render files that do not have mustaches
if (!/{{([^{}]+)}}/g.test(str)) {
return next()
}
render(str, metalsmithMetadata, function (err, res) {
if (err) return next(err)
files[file].contents = new Buffer(res)
next()
})
}, done)
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/mock-template-repo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
"filters": {
"src/*.js": "pick === 'yes'",
"**/*.vue": "pick === 'no'"
}
},
"skipInterpolation": "src/*-{one,two}.vue"
}
1 change: 1 addition & 0 deletions test/e2e/mock-template-repo/template/src/skip-one.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<template>one: {{description}}</template>
1 change: 1 addition & 0 deletions test/e2e/mock-template-repo/template/src/skip-two.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<template>two: {{description}}</template>
25 changes: 25 additions & 0 deletions test/e2e/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,31 @@ describe('vue-cli', () => {
})
})

it('avoid rendering files that match skipInterpolation option', done => {
monkeyPatchInquirer(answers)
const binFilePath = `${MOCK_TEMPLATE_REPO_PATH}/template/bin.file`
const wstream = fs.createWriteStream(binFilePath)
wstream.write(crypto.randomBytes(100))
wstream.end()

generate('test', MOCK_TEMPLATE_REPO_PATH, MOCK_TEMPLATE_BUILD_PATH, err => {
if (err) done(err)

const originalVueFileOne = fs.readFileSync(`${MOCK_TEMPLATE_REPO_PATH}/template/src/skip-one.vue`, 'utf8')
const originalVueFileTwo = fs.readFileSync(`${MOCK_TEMPLATE_REPO_PATH}/template/src/skip-two.vue`, 'utf8')
const generatedVueFileOne = fs.readFileSync(`${MOCK_TEMPLATE_BUILD_PATH}/src/skip-one.vue`, 'utf8')
const generatedVueFileTwo = fs.readFileSync(`${MOCK_TEMPLATE_BUILD_PATH}/src/skip-two.vue`, 'utf8')

expect(originalVueFileOne).to.equal(generatedVueFileOne)
expect(originalVueFileTwo).to.equal(generatedVueFileTwo)
expect(exists(binFilePath)).to.equal(true)
expect(exists(`${MOCK_TEMPLATE_BUILD_PATH}/bin.file`)).to.equal(true)
rm(binFilePath)

done()
})
})

it('validate input value', done => {
// deep copy
var invalidName = extend({}, answers, {name: 'INVALID-NAME'})
Expand Down

0 comments on commit 27af417

Please sign in to comment.