2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
25 function startFixtureServer() {
26 return require('child_process').fork('fixture/express', [PORT]);
29 function buildProxyMiddleware() {
30 return require('./middleware')(PORT);
33 function getProxyData(fixtureServerOptions, req, res, next) {
34 if (!fixtureServerOptions.serverProcess) {
35 fixtureServerOptions.serverProcess = startFixtureServer();
38 return fixtureServerOptions.proxy(req, res, next);
41 function wrapFixture(fixtureServerOptions, req, res, next) {
42 if (fixtureServerOptions.proxy) {
43 return getProxyData(fixtureServerOptions, req, res, next);
50 module.exports = function fixture(options) {
54 proxy = buildProxyMiddleware();
57 let fixtureServerOptions = {
62 (function startWatch() {
63 var nodeWatch = require('node-watch');
65 nodeWatch(['fixture/data', 'fixture/express.js'], function () {
66 if (fixtureServerOptions.proxy && fixtureServerOptions.serverProcess) {
67 fixtureServerOptions.serverProcess.kill();
68 fixtureServerOptions.serverProcess = startFixtureServer();
72 nodeWatch(['devConfig.json'], function () {
73 let devConfigDefaults = require('../devConfig.defaults');
74 require('fs').readFile('devConfig.json', (err, data) => {
76 const config = Object.assign({}, devConfigDefaults, JSON.parse(data));
77 if (config.useFixture) {
78 fixtureServerOptions.proxy = proxy || buildProxyMiddleware();
81 fixtureServerOptions.proxy = null;
82 if (fixtureServerOptions.serverProcess) {
83 fixtureServerOptions.serverProcess.kill();
84 fixtureServerOptions.serverProcess = null;
92 return (req, res, next) => wrapFixture(fixtureServerOptions, req, res, next);