Skip to content

Commit

Permalink
Merge pull request #33 from carrot/before-render
Browse files Browse the repository at this point in the history
Add before render hook
  • Loading branch information
Jeff Escalante committed Nov 14, 2014
2 parents 2dd07b7 + 2809aae commit 79920d3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
5 changes: 5 additions & 0 deletions lib/api/init.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Init extends Base
.then(ensure_template_is_updated)
.then(checkout_version)
.then(copy_template)
.then(run_user_before_render_function)
.then(replace_ejs)
.then(run_user_after_function)
.then(-> "project created at '#{@target}'!")
Expand Down Expand Up @@ -146,6 +147,10 @@ class Init extends Base
return W.reject('template does not contain root directory')
nodefn.call(ncp, root_path, @target)

run_user_before_render_function = ->
if not @config.before_render then return W.resolve()
nodefn.call(@config.before_render, @)

replace_ejs = ->
nodefn.call(readdirp, { root: @target })
.tap (res) =>
Expand Down
13 changes: 10 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,16 @@ exports.configure = [
},
]

# This function is executed after the configuration info is collected.
# It's a good place to do any other custom config you need, like building
# extra files etc. You have the full power of node at your fingertips here.
# This function is executed after the configuration info is collected, but
# before the templates are rendered. It's a good place use user provided config
# to generate additional config values needed in the template.
exports.before_render = (sprout, done) ->
console.log sprout.config_values
console.log 'executed before templates are rendered'

# This function is executed after the templates are rendered. It's a good place
# to do any other custom config you need, like building extra files etc. You
# have the full power of node at your fingertips here.
exports.after = (sprout, done) ->
console.log sprout.config_values # all the config values you collected
if not sprout.config_values.travis then sprout.remove('.travis.yml')
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/before_render/init.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fs = require 'fs'
path = require 'path'

exports.before_render = (sprout, done) ->
sprout.config_values.foo = 'doge'
done()


exports.configure = [
{
type: 'input',
name: 'foo',
message: 'What is foo?'
}
]
1 change: 1 addition & 0 deletions test/fixtures/before_render/root/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>foo = <%= foo %></p>
14 changes: 14 additions & 0 deletions test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ describe 'js api', ->
.then -> sprout.remove('foobar-3')
.should.be.fulfilled

it 'executes before render function', ->
test_template = path.join(_path, 'before_render')

sprout.add(name: 'foobar-9', uri: test_template)
.then -> sprout.init(name: 'foobar-9', path: test_path, overrides: {foo: 'bar'})
.tap ->
contents = fs.readFileSync(path.join(test_path, 'index.html'), 'utf8')
contents.should.not.match /bar/
contents.should.match /doge/
.then -> rimraf.sync(test_path)
.then -> sprout.remove('foobar-9')
.should.be.fulfilled


it 'executes after function', ->
test_template = path.join(_path, 'after')

Expand Down

0 comments on commit 79920d3

Please sign in to comment.