From 2809aaed3282a04063d7d6386c03ccb60c5681e6 Mon Sep 17 00:00:00 2001 From: Josh Rowley Date: Thu, 13 Nov 2014 23:46:58 -0500 Subject: [PATCH] add before render hook --- lib/api/init.coffee | 5 +++++ readme.md | 13 ++++++++++--- test/fixtures/before_render/init.coffee | 15 +++++++++++++++ test/fixtures/before_render/root/index.html | 1 + test/test.coffee | 14 ++++++++++++++ 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/before_render/init.coffee create mode 100644 test/fixtures/before_render/root/index.html diff --git a/lib/api/init.coffee b/lib/api/init.coffee index a323474..79ec40f 100644 --- a/lib/api/init.coffee +++ b/lib/api/init.coffee @@ -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}'!") @@ -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) => diff --git a/readme.md b/readme.md index a00852e..f396b8d 100644 --- a/readme.md +++ b/readme.md @@ -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') diff --git a/test/fixtures/before_render/init.coffee b/test/fixtures/before_render/init.coffee new file mode 100644 index 0000000..1b1292f --- /dev/null +++ b/test/fixtures/before_render/init.coffee @@ -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?' + } +] diff --git a/test/fixtures/before_render/root/index.html b/test/fixtures/before_render/root/index.html new file mode 100644 index 0000000..5473ef2 --- /dev/null +++ b/test/fixtures/before_render/root/index.html @@ -0,0 +1 @@ +

foo = <%= foo %>

diff --git a/test/test.coffee b/test/test.coffee index 8123808..1029588 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -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')