Skip to content

Commit

Permalink
Support array in skipInterpolation (vuejs#282)
Browse files Browse the repository at this point in the history
* Add test to support arrays in skipInterpolation

* Support arrays in skipInterpolation

Closes vuejs#281
Use multimatch instead of match for this purpose.
The minimatch dependency has not been removed because it's still used in
lib/filter.js. Filters should directly use multimatch. Once this
happens, the dependency to minimatch can be removed.

* Remove useless statement
  • Loading branch information
posva authored and yyx990803 committed Jan 2, 2017
1 parent 0d3f96d commit c3217ee
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +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 multimatch = require('multimatch')
var getOptions = require('./options')
var ask = require('./ask')
var filter = require('./filter')
Expand Down Expand Up @@ -91,12 +91,15 @@ function filterFiles (filters) {
*/

function renderTemplateFiles (skipInterpolation) {
skipInterpolation = typeof skipInterpolation === 'string'
? [skipInterpolation]
: 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 })) {
if (skipInterpolation && multimatch([file], skipInterpolation, { dot: true }).length) {
return next()
}
var str = files[file].contents.toString()
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"inquirer": "^0.12.0",
"metalsmith": "^2.1.0",
"minimatch": "^3.0.0",
"multimatch": "^2.1.0",
"ora": "^0.2.1",
"read-metadata": "^1.0.0",
"request": "^2.67.0",
Expand Down
27 changes: 27 additions & 0 deletions test/e2e/mock-skip-glob/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"schema": {
"name": {
"type": "string",
"required": true,
"label": "Project name"
},
"description": {
"type": "string",
"required": true,
"label": "Project description",
"default": "A Vue.js project"
},
"author": {
"type": "string",
"label": "Author"
},
"private": {
"type": "boolean",
"default": true
}
},
"skipInterpolation": [
"src/**/*.{vue,js}",
"!src/**/yes.*"
]
}
13 changes: 13 additions & 0 deletions test/e2e/mock-skip-glob/template/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "{{ name }}",
"description": "{{ description }}",
"author": "{{ author }}",
"devDependencies": {
{{#preprocessor.less}}
"less": "^2.6.1",
{{/preprocessor.less}}
{{#preprocessor.sass}}
"node-sass": "^3.4.2"
{{/preprocessor.sass}}
}
}
1 change: 1 addition & 0 deletions test/e2e/mock-skip-glob/template/src/no.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('{{ no }}')
3 changes: 3 additions & 0 deletions test/e2e/mock-skip-glob/template/src/no.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<p>{{ no }}</p>
</template>
1 change: 1 addition & 0 deletions test/e2e/mock-skip-glob/template/src/yes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('yes.')
3 changes: 3 additions & 0 deletions test/e2e/mock-skip-glob/template/src/yes.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
\{{yes}} {{pick}}
</template>
26 changes: 26 additions & 0 deletions test/e2e/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const MOCK_META_JSON_PATH = './test/e2e/mock-meta-json'
const MOCK_TEMPLATE_REPO_PATH = './test/e2e/mock-template-repo'
const MOCK_TEMPLATE_BUILD_PATH = path.resolve('./test/e2e/mock-template-build')
const MOCK_METADATA_REPO_JS_PATH = './test/e2e/mock-metadata-repo-js'
const MOCK_SKIP_GLOB = './test/e2e/mock-skip-glob'

function monkeyPatchInquirer (answers) {
// monkey patch inquirer
Expand Down Expand Up @@ -155,6 +156,31 @@ describe('vue-cli', () => {
})
})

it('support multiple globs in skipInterpolation', done => {
monkeyPatchInquirer(answers)
const binFilePath = `${MOCK_SKIP_GLOB}/template/bin.file`
const wstream = fs.createWriteStream(binFilePath)
wstream.write(crypto.randomBytes(100))
wstream.end()

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

const originalVueFileOne = fs.readFileSync(`${MOCK_SKIP_GLOB}/template/src/no.vue`, 'utf8')
const originalVueFileTwo = fs.readFileSync(`${MOCK_SKIP_GLOB}/template/src/no.js`, 'utf8')
const generatedVueFileOne = fs.readFileSync(`${MOCK_TEMPLATE_BUILD_PATH}/src/no.vue`, 'utf8')
const generatedVueFileTwo = fs.readFileSync(`${MOCK_TEMPLATE_BUILD_PATH}/src/no.js`, '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 c3217ee

Please sign in to comment.