-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
132 lines (85 loc) · 2.88 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const cwd = process.cwd();
const fs = require( 'fs' );
const path = require( 'path' );
const argv = require( 'yargs' );
const WebpackDevServer = require( 'webpack-dev-server' );
const webpack = require( 'webpack' );
/**
* Webpack super server
*/
function SuperServer( options ) {
const configFilePath = ( options.config || 'webpack.config.js' )
const webpackConfig = require( cwd + '/' + configFilePath );
const compiler = webpack( webpackConfig );
const proxySetup = {};
if ( options.proxy ) {
console.log( 'Proxying from ' + options.proxyPath + ' to ' + options.proxy );
proxySetup[ options.proxyPath ] = options.proxy;
}
const contentBase = ( options.contentBase || 'app' );
var server = new WebpackDevServer( compiler, Object.assign( webpackConfig.devServer, {
contentBase: contentBase,
proxy: proxySetup,
hot: true,
quiet: false,
noInfo: false,
stats: { colors: true }
} ) );
// Setup mocks if defined
if ( options.mockConfig ) {
console.log( 'Mocking following routes' );
if ( Array.isArray( options.mockConfig ) ) {
// iterateMultipleMocks
options.mockConfig.forEach( ( mockFile ) => {
_buildMockRoutes( mockFile, server.app );
});
} else {
_buildMockRoutes( options.mockConfig, server.app );
}
}
server.listen( options.port, options.host );
/**
* Build mock middleware
*
* @param {Buffer} content Resource return content
* @param {string} status Resource return status
*
* @private
*/
function _mockMiddleware( responseContentFileName, status ) {
return generatedMockMiddleware;
/**
* Custom middleware for each specified mock
*/
function generatedMockMiddleware( req, res ) {
var responseContent = fs.readFileSync( responseContentFileName );
res.writeHead( status );
res.end( responseContent );
}
}
// @TODO move to separate module as middleware
/**
* _buildMockRoutes - Iterate mock structure and build routes
*
* @private
* @param {Object} mockFilePath mock definition object
* @param {Object} app server instance
*/
function _buildMockRoutes( mockFilePath, app ) {
const rootPath = path.dirname( mockFilePath );
var mock = require( cwd + '/' + mockFilePath );
// iterateMethods
mock.forEach( ( methods, requestRoute ) => {
// iterateRoutePaths
methods.forEach( ( states, requestMethod ) => {
// iterateStates
states.forEach( ( responseContentFileName, responseState ) => {
const responseContentFilePath = path.join( rootPath, responseContentFileName );
console.log( requestRoute, requestMethod, responseContentFilePath, responseState );
app[requestMethod.toLowerCase()]( requestRoute, _mockMiddleware( responseContentFilePath, responseState ) );
});
});
});
}
}
module.exports = SuperServer;