Skip to content

Add and document inPlace property. #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,24 @@ 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.

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

- `destDirName` - destination directory name

```json
{
"completeMessage": "To get started:\n\n cd {{destDirName}}\n npm install\n npm run dev"
}
```

- `inPlace` - generating template into current directory

```json
{
"completeMessage": "{{#inPlace}}To get started:\n\n npm install\n npm run dev.{{else}}To get started:\n\n cd {{destDirName}}\n npm install\n npm run dev.{{/inPlace}}"
}
```

### License

[MIT](http://opensource.org/licenses/MIT)
4 changes: 3 additions & 1 deletion lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = function generate (name, src, dest, done) {
var metalsmith = Metalsmith(path.join(src, 'template'))
var data = Object.assign(metalsmith.metadata(), {
destDirName: name,
inPlace: dest === process.cwd(),
noEscape: true
})
opts.helpers && Object.keys(opts.helpers).map(function (key) {
Expand All @@ -50,6 +51,8 @@ module.exports = function generate (name, src, dest, done) {
done(err)
logMessage(opts.completeMessage, data)
})

return data
}

/**
Expand Down Expand Up @@ -108,7 +111,6 @@ function renderTemplateFiles (files, metalsmith, done) {
*
* @param {String} message
* @param {Object} data
* @param {Function} cb
*/

function logMessage (message, data) {
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/mock-meta-json/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"completeMessage": "{{#inPlace}}To get started:\n\n npm install\n npm run dev.{{else}}To get started:\n\n cd {{destDirName}}\n npm install\n npm run dev.{{/inPlace}}"
}
3 changes: 1 addition & 2 deletions test/e2e/mock-metadata-repo-js/meta.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

module.exports = {
prompts: {
description: {
Expand All @@ -12,4 +11,4 @@ module.exports = {
return str.toUpperCase()
}
}
}
}
22 changes: 18 additions & 4 deletions test/e2e/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const extend = Object.assign || require('util')._extend
const generate = require('../../lib/generate')
const metadata = require('../../lib/options')

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'
Expand Down Expand Up @@ -43,18 +44,16 @@ describe('vue-cli', () => {
noEscape: true
}

it('read metadata from json', done => {
it('read metadata from json', () => {
const meta = metadata('test-pkg', MOCK_TEMPLATE_REPO_PATH)
expect(meta).to.be.an('object')
expect(meta.prompts).to.have.property('description')
done()
})

it('read metadata from js', done => {
it('read metadata from js', () => {
const meta = metadata('test-pkg', MOCK_METADATA_REPO_JS_PATH)
expect(meta).to.be.an('object')
expect(meta.prompts).to.have.property('description')
done()
})

it('helpers', done => {
Expand All @@ -67,6 +66,21 @@ describe('vue-cli', () => {
})
})

it('adds additional data to meta data', () => {
const data = generate('test', MOCK_META_JSON_PATH, MOCK_TEMPLATE_BUILD_PATH)
expect(data.destDirName).to.equal('test')
expect(data.inPlace).to.equal(false)
})

it('sets `inPlace` to true when generating in same directory', () => {
const currentDir = process.cwd()
process.chdir(MOCK_TEMPLATE_BUILD_PATH)
const data = generate('test', MOCK_META_JSON_PATH, MOCK_TEMPLATE_BUILD_PATH)
expect(data.destDirName).to.equal('test')
expect(data.inPlace).to.equal(true)
process.chdir(currentDir)
})

it('template generation', done => {
monkeyPatchInquirer(answers)
generate('test', MOCK_TEMPLATE_REPO_PATH, MOCK_TEMPLATE_BUILD_PATH, err => {
Expand Down