diff --git a/.gitignore b/.gitignore index ff94e6a1..fb5ec0b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .DS_Store node_modules docs/_build +.*.sw* diff --git a/lib/api/compile.coffee b/lib/api/compile.coffee index 9b4ec1e9..3662a073 100644 --- a/lib/api/compile.coffee +++ b/lib/api/compile.coffee @@ -53,6 +53,7 @@ class Compile .with(@) .tap(create_folders) .then(process_files) + .then(after_ext_hook) .then(after_hook) .then(purge_empty_folders) .then(@roots.emit.bind(@roots, 'done'), @roots.emit.bind(@roots, 'error')) @@ -75,6 +76,15 @@ class Compile after_hook = (ast) -> hook_method.call(@, @roots.config.after) + ###* + * Calls any extension-provided after hooks with the roots context. + * + * @private + ### + + after_ext_hook = -> + sequence(@extensions.hooks('project_hooks.after'), @) + ###* * Checks to ensure the requested hook(s) is/are present, then calls them, * whether there was an array of hooks provided or just a single hook. diff --git a/lib/extensions.coffee b/lib/extensions.coffee index d2285a8a..7f81962b 100644 --- a/lib/extensions.coffee +++ b/lib/extensions.coffee @@ -62,6 +62,9 @@ class Extensions if not_function(ext.compile_hooks) @roots.bail(125, 'The compile_hooks property must be a function', ext) + if not_function(ext.project_hooks) + @roots.bail(125, 'The project_hooks property must be a function', ext) + if not_function(ext.category_hooks) @roots.bail(125, 'The category_hooks property must be a function', ext) @@ -101,10 +104,11 @@ class Extensions if typeof called_namespace isnt 'object' @[@length-2].roots.bail(125, "#{namespc} should return an object", ext) - if called_namespace.category - if called_namespace.category isnt category then return - else - if ext.category and ext.category isnt category then return + if category? + if called_namespace.category + if called_namespace.category isnt category then return + else + if ext.category and ext.category isnt category then return called_namespace[key] diff --git a/test/extensions.coffee b/test/extensions.coffee index f7abf01b..6b1617d9 100644 --- a/test/extensions.coffee +++ b/test/extensions.coffee @@ -28,6 +28,7 @@ describe 'extension hooks', -> .on('after_pass', => @after_pass = true) .on('write', => @write = true) .on('after_category', => @after_category = true) + .on('after_project', => @after_project = true) .on('done', -> done()) @project.compile() @@ -50,6 +51,9 @@ describe 'extension hooks', -> it 'after category hook should work', -> @after_category.should.be.ok + it 'after project hook should work', -> + @after_project.should.be.ok + describe 'write hook', -> before (done) -> @@ -189,6 +193,20 @@ describe 'extension failures', -> project.compile() + # this should not throw + it 'should bail when project_hooks is defined but not a function', -> + project = new Roots(path.join(@path, 'case11')) + (-> project.compile()).should.throw('The project_hooks property must be a function') + + it 'should bail when project_hooks is a function but doesnt return an object', (done) -> + project = new Roots(path.join(@path, 'case12')) + + project.on 'error', (err) -> + err.toString().should.equal('Malformed Extension: project_hooks should return an object') + done() + + project.compile() + it 'should bail if write hook returns anything other than an array, object, or boolean', (done) -> project = new Roots(path.join(@path, 'case9')) diff --git a/test/fixtures/extensions/basic/test_extension.coffee b/test/fixtures/extensions/basic/test_extension.coffee index ae3cb637..4cb20e19 100644 --- a/test/fixtures/extensions/basic/test_extension.coffee +++ b/test/fixtures/extensions/basic/test_extension.coffee @@ -28,3 +28,7 @@ module.exports = -> category_hooks: -> after: (ctx, category) -> ctx.roots.emit('after_category', ctx.path) + + project_hooks: -> + after: (ctx) -> + ctx.roots.emit('after_project', ctx.path) diff --git a/test/fixtures/extensions/failures/case11/app.coffee b/test/fixtures/extensions/failures/case11/app.coffee new file mode 100644 index 00000000..1484953e --- /dev/null +++ b/test/fixtures/extensions/failures/case11/app.coffee @@ -0,0 +1,6 @@ +ext = -> + class Fail7 + project_hooks: 'wow' + +module.exports = + extensions: [ext()] diff --git a/test/fixtures/extensions/failures/case11/test b/test/fixtures/extensions/failures/case11/test new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/extensions/failures/case12/app.coffee b/test/fixtures/extensions/failures/case12/app.coffee new file mode 100644 index 00000000..ffe52cf9 --- /dev/null +++ b/test/fixtures/extensions/failures/case12/app.coffee @@ -0,0 +1,6 @@ +ext = -> + class Fail8 + project_hooks: -> true + +module.exports = + extensions: [ext()]