Skip to content

Commit 88df54e

Browse files
committed
feat: Add support for loading mdiddlewares from npm.
1 parent 9595ee2 commit 88df54e

7 files changed

Lines changed: 63 additions & 37 deletions

File tree

lib/config/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'path';
22
import findUp from 'find-up';
33
import _ from 'lodash';
4+
import resolve from 'resolve';
45
import Constants from '../constants';
56
import log from '../log';
67
import schema from './config-schema';
@@ -118,10 +119,23 @@ export default class Config {
118119
return defaultObj;
119120
});
120121

122+
// For a given value if it is a string resolve it as if it's an NPM module.
123+
const resolveMiddlewareModules = (moduleVal) => {
124+
if (_.isString(moduleVal)) {
125+
const modulePath = resolve.sync(moduleVal, { basedir: this.root });
126+
// eslint-disable-next-line global-require, import/no-dynamic-require
127+
return require(modulePath);
128+
}
129+
return moduleVal;
130+
};
131+
121132
// Make sure all values are arrays.
122-
this._raw.middlewares = _.flatten(Array.of(this._raw.middlewares));
133+
const middlewares = _.flatten(Array.of(this._raw.middlewares));
134+
this._raw.middlewares = middlewares.map(resolveMiddlewareModules);
135+
123136
_.forEach(this._raw.lifecycle, (val, key) => {
124-
this._raw.lifecycle[key] = _.flatten(Array.of(val));
137+
const newVal = _.flatten(Array.of(val));
138+
this._raw.lifecycle[key] = newVal.map(resolveMiddlewareModules);
125139
});
126140
}
127141

test/e2e/yarn.spec.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ describe('reptar Reptar', function test() {
9999

100100
it('calls every middleware function in the expected order', () => {
101101
_.reduce(instance._test.middlewares, (prevValue, nextValue) => {
102-
assert(nextValue >= prevValue);
102+
assert(nextValue > prevValue);
103+
return nextValue;
103104
});
104105
});
105106

@@ -109,15 +110,15 @@ describe('reptar Reptar', function test() {
109110
didUpdate,
110111
} = instance._test.lifecycle;
111112

112-
assert(willUpdate[0] <= didUpdate[0]);
113+
assert(willUpdate[0] < didUpdate[0]);
113114
});
114115

115116
it('didUpdate is called before the first middleware', () => {
116117
const {
117118
didUpdate,
118119
} = instance._test.lifecycle;
119120

120-
assert(didUpdate[0] <= instance._test.middlewares[0]);
121+
assert(didUpdate[0] < instance._test.middlewares[0]);
121122
});
122123

123124
it('willBuild is not called', () => {
@@ -178,7 +179,7 @@ describe('reptar Reptar', function test() {
178179
instance._test.middlewares.length - 1
179180
];
180181

181-
assert(lastMiddleware <= willBuild[0]);
182+
assert(lastMiddleware < willBuild[0]);
182183
});
183184

184185
it('willBuild is called before didBuild', () => {
@@ -187,7 +188,7 @@ describe('reptar Reptar', function test() {
187188
didBuild,
188189
} = instance._test.lifecycle;
189190

190-
assert(willBuild[0] <= didBuild[0]);
191+
assert(willBuild[0] < didBuild[0]);
191192
});
192193
});
193194
});

test/fixtures/simple-site/expected/reptar.config.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import sinon from 'sinon';
2-
3-
const middleware = (reptar) => {
4-
reptar._test.middlewares.push(Date.now());
1+
const createMiddleware = val => (reptar) => {
2+
reptar._test.middlewares.push(val);
53
};
64

7-
const createLifecycleMiddleware = (lifecycleName) => (reptar) => {
8-
reptar._test.lifecycle[lifecycleName].push(Date.now());
5+
const createLifecycleMiddleware = (lifecycleName, val) => (reptar) => {
6+
reptar._test.lifecycle[lifecycleName].push(val);
97
};
108

119
module.exports = {
@@ -60,13 +58,14 @@ module.exports = {
6058
server: { port: 8080, host: '127.0.0.1', baseurl: '' },
6159
new_file_permalink: '/_posts/:date|YYYY-:date|MM-:date|D-:title.md',
6260
middlewares: [
63-
middleware,
64-
middleware,
61+
createMiddleware(3),
62+
'my-middleware',
63+
createMiddleware(5),
6564
],
6665
lifecycle: {
67-
willUpdate: createLifecycleMiddleware('willUpdate'),
68-
didUpdate: createLifecycleMiddleware('didUpdate'),
69-
willBuild: createLifecycleMiddleware('willBuild'),
70-
didBuild: createLifecycleMiddleware('didBuild'),
66+
willUpdate: createLifecycleMiddleware('willUpdate', 1),
67+
didUpdate: createLifecycleMiddleware('didUpdate', 2),
68+
willBuild: createLifecycleMiddleware('willBuild', 6),
69+
didBuild: createLifecycleMiddleware('didBuild', 7),
7170
}
7271
};

test/fixtures/simple-site/src/node_modules/fake-module/index.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/simple-site/src/node_modules/my-middleware/index.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/simple-site/src/reptar.config.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import sinon from 'sinon';
2-
3-
const middleware = (reptar) => {
4-
reptar._test.middlewares.push(Date.now());
1+
const createMiddleware = val => (reptar) => {
2+
reptar._test.middlewares.push(val);
53
};
64

7-
const createLifecycleMiddleware = (lifecycleName) => (reptar) => {
8-
reptar._test.lifecycle[lifecycleName].push(Date.now());
5+
const createLifecycleMiddleware = (lifecycleName, val) => (reptar) => {
6+
reptar._test.lifecycle[lifecycleName].push(val);
97
};
108

119
module.exports = {
@@ -60,13 +58,14 @@ module.exports = {
6058
server: { port: 8080, host: '127.0.0.1', baseurl: '' },
6159
new_file_permalink: '/_posts/:date|YYYY-:date|MM-:date|D-:title.md',
6260
middlewares: [
63-
middleware,
64-
middleware,
61+
createMiddleware(3),
62+
'my-middleware',
63+
createMiddleware(5),
6564
],
6665
lifecycle: {
67-
willUpdate: createLifecycleMiddleware('willUpdate'),
68-
didUpdate: createLifecycleMiddleware('didUpdate'),
69-
willBuild: createLifecycleMiddleware('willBuild'),
70-
didBuild: createLifecycleMiddleware('didBuild'),
66+
willUpdate: createLifecycleMiddleware('willUpdate', 1),
67+
didUpdate: createLifecycleMiddleware('didUpdate', 2),
68+
willBuild: createLifecycleMiddleware('willBuild', 6),
69+
didBuild: createLifecycleMiddleware('didBuild', 7),
7170
}
7271
};

test/unit/config/index.spec.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import path from 'path';
55
import _ from 'lodash';
66

77
import fixture from '../../fixture';
8+
import {
9+
simpleSite,
10+
} from '../../utils';
811

912
const ConfigRewire = rewire('../../../lib/config/index.js');
1013
const Config = ConfigRewire.default;
@@ -233,16 +236,14 @@ describe('config/index Config', () => {
233236
});
234237

235238
it('coerces middleware and lifecycle config values to arrays', () => {
236-
const rootPath = '/root/';
237-
238239
const instance = new Config('');
239-
instance.root = rootPath;
240+
instance.root = simpleSite.src;
240241

241242
const rawConfig = {
242-
middlewares: 'foo',
243+
middlewares: 'fake-module',
243244
lifecycle: {
244245
willUpdate: _.noop,
245-
didUpdate: ['one'],
246+
didUpdate: ['my-middleware'],
246247
},
247248
};
248249

@@ -254,9 +255,13 @@ describe('config/index Config', () => {
254255
instance.update();
255256

256257
assert(_.isArray(instance.get('middlewares')));
258+
assert.equal(typeof instance.get('middlewares[0]'), 'function');
259+
257260
assert(_.isArray(instance.get('lifecycle.willUpdate')));
261+
assert.equal(typeof instance.get('lifecycle.willUpdate[0]'), 'function');
262+
258263
assert(_.isArray(instance.get('lifecycle.didUpdate')));
259-
assert.equal(instance.get('lifecycle.didUpdate'), 'one');
264+
assert.equal(typeof instance.get('lifecycle.didUpdate[0]'), 'function');
260265
});
261266
});
262267
});

0 commit comments

Comments
 (0)