Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Commit f5667c3

Browse files
committed
Migrate to ES6 and new plugin infrastructure
1 parent 6f13d13 commit f5667c3

13 files changed

+237
-262
lines changed

.babelrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"plugins": [ "add-module-exports" ],
3+
"presets": [ "es2015" ]
4+
}

.gitattributes

-1
This file was deleted.

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
tmp*
22
node_modules/
33
npm-debug.log
4-
.idea/
4+
.idea/
5+
lib/

.npmignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ docs/
33
test/
44
.DS_Store
55
*.markdown
6-
*.md
6+
*.md
7+
!lib/
8+
src/
9+
.babelrc
10+
.editorconfig

.travis.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
sudo: false
22
language: node_js
3-
node_js: node
3+
node_js:
4+
- "0.10"
5+
- "node"
6+
- "iojs"

lib/hooks.js

-16
This file was deleted.

package.json

+14-14
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
"feathers-plugin",
88
"feathers"
99
],
10-
"licenses": [
11-
{
12-
"type": "MIT",
13-
"url": "https://github.com/feathersjs/feathers-hooks/blob/master/LICENSE"
14-
}
15-
],
10+
"license": "MIT",
1611
"repository": {
1712
"type": "git",
1813
"url": "git://github.com/feathersjs/feathers-hooks.git"
@@ -31,25 +26,30 @@
3126
},
3227
"main": "lib/hooks.js",
3328
"scripts": {
34-
"test": "npm run jshint && npm run mocha",
35-
"jshint": "jshint lib/. test/. --config",
36-
"mocha": "mocha test/",
29+
"prepublish": "npm run compile",
3730
"publish": "git push origin && git push origin --tags",
3831
"release:patch": "npm version patch && npm publish",
3932
"release:minor": "npm version minor && npm publish",
40-
"release:major": "npm version major && npm publish"
33+
"release:major": "npm version major && npm publish",
34+
"compile": "rm -rf lib/ && babel -d lib/ src/",
35+
"watch": "babel --watch -d lib/ src/",
36+
"jshint": "jshint src/. test/. --config",
37+
"mocha": "mocha test/ --compilers js:babel-core/register",
38+
"test": "npm run compile && npm run jshint && npm run mocha"
4139
},
4240
"directories": {
4341
"lib": "lib"
4442
},
4543
"dependencies": {
46-
"feathers-commons": "^0.2.8",
47-
"lodash": "^2.4.1"
44+
"feathers-commons": "^0.5.0"
4845
},
4946
"devDependencies": {
47+
"babel-cli": "^6.4.0",
48+
"babel-core": "^6.4.0",
49+
"babel-plugin-add-module-exports": "^0.1.2",
50+
"babel-preset-es2015": "^6.3.13",
5051
"feathers": "^1.0.0",
5152
"jshint": "^2.8.0",
52-
"mocha": "^2.3.3",
53-
"q": "^1.0.1"
53+
"mocha": "^2.3.3"
5454
}
5555
}
+12-15
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
'use strict';
2-
3-
var _ = require('lodash');
4-
var commons = require('./commons');
5-
var utils = require('feathers-commons').hooks;
1+
import { hooks as utils } from 'feathers-commons';
2+
import { makeHookFn, createMixin } from './commons';
63

74
/**
85
* Return the hook mixin method for the given name.
96
*
107
* @param {String} method The service method name
118
* @returns {Function}
129
*/
13-
var getMixin = function (method) {
10+
function getMixin(method) {
1411
return function() {
1512
var _super = this._super;
1613

1714
if(!this.__after || !this.__after[method].length) {
1815
return _super.apply(this, arguments);
1916
}
2017

21-
var args = _.toArray(arguments);
22-
var hookObject = utils.hookObject(method, 'after', args);
18+
const args = Array.from(arguments);
19+
const hookObject = utils.hookObject(method, 'after', args);
2320
// Make a copy of our hooks
24-
var hooks = this.__after[method].slice();
21+
const hooks = this.__after[method].slice();
2522

2623
// Remove the old callback and replace with the new callback that runs the hook
2724
args.pop();
@@ -40,7 +37,7 @@ var getMixin = function (method) {
4037
hookObject.result = result;
4138

4239
while(hooks.length) {
43-
fn = commons.makeHookFn(hooks.pop(), fn);
40+
fn = makeHookFn(hooks.pop(), fn);
4441
}
4542

4643
return fn.call(this, hookObject);
@@ -49,10 +46,10 @@ var getMixin = function (method) {
4946

5047
return _super.apply(this, args);
5148
};
52-
};
49+
}
5350

54-
var addHooks = function(hooks, method) {
55-
var myHooks = this.__after[method];
51+
function addHooks(hooks, method) {
52+
const myHooks = this.__after[method];
5653

5754
if(hooks[method]) {
5855
myHooks.push.apply(myHooks, hooks[method]);
@@ -61,6 +58,6 @@ var addHooks = function(hooks, method) {
6158
if(hooks.all) {
6259
myHooks.push.apply(myHooks, hooks.all);
6360
}
64-
};
61+
}
6562

66-
module.exports = commons.createMixin('after', getMixin, addHooks);
63+
module.exports = createMixin('after', getMixin, addHooks);
+11-13
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,36 @@
1-
'use strict';
2-
3-
var commons = require('./commons');
4-
var utils = require('feathers-commons').hooks;
1+
import { hooks as utils } from 'feathers-commons';
2+
import { makeHookFn, createMixin } from './commons';
53

64
/**
75
* Return the hook mixin method for the given name.
86
*
97
* @param {String} method The service method name
108
* @returns {Function}
119
*/
12-
var getMixin = function (method) {
10+
function getMixin(method) {
1311
return function() {
14-
var _super = this._super;
12+
const _super = this._super;
1513

1614
if(!this.__before || !this.__before[method].length) {
1715
return _super.apply(this, arguments);
1816
}
1917

2018
// Make a copy of our hooks
21-
var hooks = this.__before[method].slice();
19+
const hooks = this.__before[method].slice();
2220
// The chained function
23-
var fn = function(hookObject) {
21+
let fn = function(hookObject) {
2422
return _super.apply(this, utils.makeArguments(hookObject));
2523
};
2624

2725
while(hooks.length) {
28-
fn = commons.makeHookFn(hooks.pop(), fn);
26+
fn = makeHookFn(hooks.pop(), fn);
2927
}
3028

3129
return fn.call(this, utils.hookObject(method, 'before', arguments));
3230
};
33-
};
31+
}
3432

35-
var addHooks = function(hooks, method) {
33+
function addHooks(hooks, method) {
3634
var myHooks = this.__before[method];
3735

3836
if(hooks.all) {
@@ -42,6 +40,6 @@ var addHooks = function(hooks, method) {
4240
if(hooks[method]) {
4341
myHooks.push.apply(myHooks, hooks[method]);
4442
}
45-
};
43+
}
4644

47-
module.exports = commons.createMixin('before', getMixin, addHooks);
45+
export default createMixin('before', getMixin, addHooks);
+19-26
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
'use strict';
2-
3-
var _ = require('lodash');
4-
var utils = require('feathers-commons').hooks;
1+
import { hooks as utils } from 'feathers-commons';
52

63
/**
74
* Creates a new hook function for the execution chain.
@@ -10,11 +7,11 @@ var utils = require('feathers-commons').hooks;
107
* @param {Function} prev The previous hook method in the chain
118
* @returns {Function}
129
*/
13-
exports.makeHookFn = function(hook, prev) {
10+
export function makeHookFn(hook, prev) {
1411
return function(hookObject) {
1512
// The callback for the hook
16-
var hookCallback = function(error, newHookObject) {
17-
var currentHook = newHookObject || hookObject;
13+
const hookCallback = (error, newHookObject) => {
14+
const currentHook = newHookObject || hookObject;
1815

1916
// Call the callback with the result we set in `newCallback`
2017
if(error) {
@@ -23,21 +20,19 @@ exports.makeHookFn = function(hook, prev) {
2320
}
2421

2522
prev.call(this, currentHook);
26-
}.bind(this);
23+
};
2724

28-
var promise = hook.call(this, hookObject, hookCallback);
25+
const promise = hook.call(this, hookObject, hookCallback);
2926

3027
if(typeof promise !== 'undefined' && typeof promise.then === 'function') {
3128
promise.then(function() {
3229
hookCallback();
33-
}, function(error) {
34-
hookCallback(error);
35-
});
30+
}, hookCallback);
3631
}
3732

3833
return promise;
3934
};
40-
};
35+
}
4136

4237
/**
4338
* Returns the main mixin function for the given type.
@@ -48,33 +43,31 @@ exports.makeHookFn = function(hook, prev) {
4843
* @param {Function} addHooks A callback that adds the hooks
4944
* @returns {Function}
5045
*/
51-
exports.createMixin = function(type, getMixin, addHooks) {
46+
export function createMixin(type, getMixin, addHooks) {
5247
return function(service) {
53-
if(!_.isFunction(service.mixin)) {
48+
if(typeof service.mixin !== 'function') {
5449
return;
5550
}
5651

57-
var app = this;
58-
var hookProp = '__' + type;
59-
var methods = app.methods;
60-
var old = service[type];
52+
const app = this;
53+
const hookProp = '__' + type;
54+
const methods = app.methods;
55+
const old = service[type];
6156

62-
var mixin = {};
57+
const mixin = {};
6358

6459
mixin[type] = function(obj) {
6560
if(!this[hookProp]) {
6661
this[hookProp] = {};
67-
_.each(methods, function(method) {
68-
this[hookProp][method] = [];
69-
}.bind(this));
62+
methods.forEach(method => this[hookProp][method] = []);
7063
}
7164

72-
_.each(methods, addHooks.bind(this, utils.convertHookData(obj)));
65+
methods.forEach(addHooks.bind(this, utils.convertHookData(obj)));
7366

7467
return this;
7568
};
7669

77-
_.each(methods, function(method) {
70+
methods.forEach(method => {
7871
if(typeof service[method] !== 'function') {
7972
return;
8073
}
@@ -88,4 +81,4 @@ exports.createMixin = function(type, getMixin, addHooks) {
8881
service[type](old);
8982
}
9083
};
91-
};
84+
}

src/hooks.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
if(!global._babelPolyfill) { require('babel-polyfill'); }
2+
3+
import before from './before';
4+
import after from './after';
5+
6+
export default function() {
7+
return function() {
8+
this.mixins.push(before);
9+
this.mixins.push(after);
10+
};
11+
}

0 commit comments

Comments
 (0)