-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
70 lines (59 loc) · 2.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict';
var path = require('path');
var cmp = require('semver-compare');
var watchr = require('watchr');
var bodyParser = require('body-parser');
var query = require('connect-query');
var cookieParser = require('cookie-parser');
var mockMiddleware = require('./lib/mockMiddleware');
var fetchConfFile = require('./lib/fetchConfFile');
var fetchRules = require('./lib/fetchRules');
var requireUncached = require('./lib/util/fs').requireUncached;
var logMockError = require('./lib/util/log').logMockError;
var store = require('./lib/store');
var isConfFileHandled = false;
module.exports = {
config: function(options, cwd) {
return {
modifyWebpackConfig: function(config) {
var self = this;
var isStartingServer = process.argv[2] === 's' || process.argv[2] === 'server';
var mockingDisabled = process.argv.some(function(param) {
return param.split('=').length > 1 && param.split('=')[0] === 'mock' && param.split('=')[1] === 'false'
})
if (self.env !== 'local' || !isStartingServer || mockingDisabled) {
return config;
}
if (!isConfFileHandled) {
isConfFileHandled = true;
if (!global.ykitVer || !cmp(global.ykitVer, '0.4.1')) {
return logWarn('ykit-config-mock only supports ykit version >= 0.5.0');
}
try {
var confFile = fetchConfFile.bind(self)(options.confPath);
store.set('rules', fetchRules.bind(self)(confFile));
store.set('confFileDir', path.dirname(confFile));
// Create the stalker for the mock config file
var stalker = watchr.create(confFile)
stalker.on('change', function(changeType) {
if (changeType === 'update') {
store.set('rules', fetchRules.bind(self)(confFile, true));
}
});
stalker.setConfig({catchupDelay: 500});
stalker.watch(function() {});
} catch (e) {
logError(e.stack);
}
}
// add middlewares
self.applyMiddleware(query(), {global: true})
self.applyMiddleware(cookieParser(), {global: true})
self.applyMiddleware(bodyParser.urlencoded({extended: false}), {global: true})
self.applyMiddleware(bodyParser.json(), {global: true})
self.applyMiddleware(mockMiddleware.bind(self), {global: true});
return config;
}
}
}
}