blob: 7e5b263c5c3d57572d0fa877c019c9da90c57c70 [file] [log] [blame]
Michael Landoefa037d2017-02-19 12:57:33 +02001/*-
2 * ============LICENSE_START=======================================================
3 * SDC
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
21'use strict';
22
23const PORT = 4000;
24
25function startFixtureServer() {
26 return require('child_process').fork('fixture/express', [PORT]);
27}
28
29function buildProxyMiddleware() {
30 return require('./middleware')(PORT);
31}
32
33function getProxyData(fixtureServerOptions, req, res, next) {
34 if (!fixtureServerOptions.serverProcess) {
35 fixtureServerOptions.serverProcess = startFixtureServer();
36 }
37
38 return fixtureServerOptions.proxy(req, res, next);
39}
40
41function wrapFixture(fixtureServerOptions, req, res, next) {
42 if (fixtureServerOptions.proxy) {
43 return getProxyData(fixtureServerOptions, req, res, next);
44 } else {
45 next();
46 }
47}
48
49
50module.exports = function fixture(options) {
51
52 let proxy;
53 if(options.enabled) {
54 proxy = buildProxyMiddleware();
55 }
56
57 let fixtureServerOptions = {
58 proxy,
59 serverProcess: null
60 };
61
62 (function startWatch() {
63 var nodeWatch = require('node-watch');
64
65 nodeWatch(['fixture/data', 'fixture/express.js'], function () {
66 if (fixtureServerOptions.proxy && fixtureServerOptions.serverProcess) {
67 fixtureServerOptions.serverProcess.kill();
68 fixtureServerOptions.serverProcess = startFixtureServer();
69 }
70 });
71
72 nodeWatch(['devConfig.json'], function () {
73 let devConfigDefaults = require('../devConfig.defaults');
74 require('fs').readFile('devConfig.json', (err, data) => {
75 if (err) throw err;
76 const config = Object.assign({}, devConfigDefaults, JSON.parse(data));
77 if (config.useFixture) {
78 fixtureServerOptions.proxy = proxy || buildProxyMiddleware();
79 }
80 else {
81 fixtureServerOptions.proxy = null;
82 if (fixtureServerOptions.serverProcess) {
83 fixtureServerOptions.serverProcess.kill();
84 fixtureServerOptions.serverProcess = null;
85 }
86 }
87 });
88 });
89
90 })();
91
92 return (req, res, next) => wrapFixture(fixtureServerOptions, req, res, next);
93};