Improve hooks configurations by applying defaults

This commit is contained in:
Aurélien Georget 2016-12-18 13:46:11 +01:00
parent 862244f458
commit 6181044716
8 changed files with 254 additions and 13 deletions

View File

@ -0,0 +1,16 @@
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[{package.json,*.yml}]
indent_style = space
indent_size = 2
[*.md]
trim_trailing_whitespace = false

94
packages/strapi-ejs/.gitignore vendored Normal file
View File

@ -0,0 +1,94 @@
############################
# OS X
############################
.DS_Store
.AppleDouble
.LSOverride
Icon
.Spotlight-V100
.Trashes
._*
############################
# Linux
############################
*~
############################
# Windows
############################
Thumbs.db
ehthumbs.db
Desktop.ini
$RECYCLE.BIN/
*.cab
*.msi
*.msm
*.msp
############################
# Packages
############################
*.7z
*.csv
*.dat
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip
*.com
*.class
*.dll
*.exe
*.o
*.seed
*.so
*.swo
*.swp
*.swn
*.swm
*.out
*.pid
############################
# Logs and databases
############################
.tmp
*.log
*.sql
*.sqlite
############################
# Misc.
############################
*#
.idea
nbproject
############################
# Node.js
############################
lib-cov
lcov.info
pids
logs
results
build
node_modules
.node_history

View File

@ -0,0 +1,101 @@
############################
# OS X
############################
.DS_Store
.AppleDouble
.LSOverride
Icon
.Spotlight-V100
.Trashes
._*
############################
# Linux
############################
*~
############################
# Windows
############################
Thumbs.db
ehthumbs.db
Desktop.ini
$RECYCLE.BIN/
*.cab
*.msi
*.msm
*.msp
############################
# Packages
############################
*.7z
*.csv
*.dat
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip
*.com
*.class
*.dll
*.exe
*.o
*.seed
*.so
*.swo
*.swp
*.swn
*.swm
*.out
*.pid
############################
# Logs and databases
############################
.tmp
*.log
*.sql
*.sqlite
############################
# Misc.
############################
*#
.idea
nbproject
############################
# Node.js
############################
lib-cov
lcov.info
pids
logs
results
build
node_modules
.node_history
############################
# Tests
############################
test

View File

@ -0,0 +1,7 @@
Copyright (c) 2015-2016 Strapi.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,19 @@
# strapi-ejs
[![npm version](https://img.shields.io/npm/v/strapi-ejs.svg)](https://www.npmjs.org/package/strapi-ejs)
[![npm downloads](https://img.shields.io/npm/dm/strapi-ejs.svg)](https://www.npmjs.org/package/strapi-ejs)
[![npm dependencies](https://david-dm.org/strapi/strapi-ejs.svg)](https://david-dm.org/strapi/strapi-ejs)
[![Build status](https://travis-ci.org/strapi/strapi-ejs.svg?branch=master)](https://travis-ci.org/strapi/strapi)
[![Slack status](http://strapi-slack.herokuapp.com/badge.svg)](http://slack.strapi.io)
This built-in hook allows you to use the EJS template engine with custom options.
## Resources
- [MIT License](LICENSE.md)
## Links
- [Strapi website](http://strapi.io/)
- [Strapi community on Slack](http://slack.strapi.io)
- [Strapi news on Twitter](https://twitter.com/strapijs)

View File

@ -22,20 +22,20 @@ module.exports = function (strapi) {
* Default options
*/
defaults: {},
defaults: {
root: path.join(strapi.config.appPath, strapi.config.paths.views),
layout: 'layout',
viewExt: 'ejs',
cache: true,
debug: true
},
/**
* Initialize the hook
*/
initialize: cb => {
render(strapi.app, {
root: path.join(strapi.config.appPath, strapi.config.paths.views),
layout: 'layout',
viewExt: 'ejs',
cache: false,
debug: true
});
render(strapi.app, strapi.config.hooks.ejs);
strapi.app.context.render = co.wrap(strapi.app.context.render);

View File

@ -117,7 +117,11 @@ module.exports = function (configOverride, cb) {
// Map (warning: we could have some order issues).
_.assignWith(mapper, this.tree, (objValue, srcValue) => {
return objValue === false ? objValue : true;
if (_.isPlainObject(objValue)) {
return true;
}
return _.isBoolean(objValue) ? objValue : false;
});
// Pick hook to load.

View File

@ -49,11 +49,11 @@ module.exports = function (cb) {
}
// Function to apply a hook's `defaults` object or function.
function applyDefaults(hook) {
function applyDefaults(id, hook) {
// Get the hook defaults.
const defaults = (_.isFunction(hook.defaults) ? hook.defaults(this.config) : hook.defaults) || {};
const defaults = (_.isFunction(hook.defaults) ? hook.defaults(_.get(this.config, 'hooks.' + id)) : hook.defaults) || {};
_.defaultsDeep(this.config, defaults);
_.defaultsDeep(_.get(this.config, 'hooks.' + id), defaults);
}
// Load a hook and initialize it.
@ -86,7 +86,7 @@ module.exports = function (cb) {
async.series(_.map(this.hooks, (hook, identity) => {
return cb => {
prepareHook.apply(this, [identity]);
applyDefaults.apply(this, [hook]);
applyDefaults.apply(this, [identity, hook(this)]);
loadHook.apply(this, [identity, cb]);
};
}), err => cb(err));