From df1ad7acd8df8b35d44ccf49961fe024856d7fb0 Mon Sep 17 00:00:00 2001 From: Ray Millward Date: Thu, 14 Jun 2018 16:08:01 +0100 Subject: [PATCH] Updated the build system so the right config files get added in with initial development --- .travis.yml | 3 +-- continuous_integration/make_settings.sh | 13 ------------- continuous_integration/update_settings.js | 21 +++++++++++++++++++++ package.json | 9 ++++++--- settings.dev.json | 4 ++++ example-settings.json => settings.prod.json | 0 src/malcolm/actions/socket.actions.js | 12 ++++++++---- src/malcolm/malcolmSocketHandler.js | 4 ---- src/navbar/navcontrol.test.js | 3 ++- 9 files changed, 42 insertions(+), 27 deletions(-) delete mode 100644 continuous_integration/make_settings.sh create mode 100644 continuous_integration/update_settings.js create mode 100644 settings.dev.json rename example-settings.json => settings.prod.json (100%) diff --git a/.travis.yml b/.travis.yml index 52cb4726..95ef1c14 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,8 +10,6 @@ install: - npm install -g codecov - npm install script: -- chmod +x $TRAVIS_BUILD_DIR/continuous_integration/make_settings.sh -- "$TRAVIS_BUILD_DIR/continuous_integration/make_settings.sh $TRAVIS_BUILD_DIR" - npm run lint:css - npm run lint:js - npm run build @@ -19,6 +17,7 @@ script: - codecov - npm run e2e before_deploy: +- npm run config:prod - chmod +x $TRAVIS_BUILD_DIR/continuous_integration/package_artifacts.sh - "$TRAVIS_BUILD_DIR/continuous_integration/package_artifacts.sh $TRAVIS_BUILD_DIR" deploy: diff --git a/continuous_integration/make_settings.sh b/continuous_integration/make_settings.sh deleted file mode 100644 index fec7dd4e..00000000 --- a/continuous_integration/make_settings.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash -set -e - -working_dir=$1 -cd $working_dir - -VERSION=`git describe --tags --long` -echo "Packageing version: $VERSION" - -pwd -#cp ./example-settings.json ./public/settings.json - -sed "s/\"version\": \".*\"/\"version\": \"${VERSION}\"/g" ./example-settings.json > ./public/settings.json \ No newline at end of file diff --git a/continuous_integration/update_settings.js b/continuous_integration/update_settings.js new file mode 100644 index 00000000..22ec9cec --- /dev/null +++ b/continuous_integration/update_settings.js @@ -0,0 +1,21 @@ +const { execSync } = require('child_process'); +var fs = require('fs'); + +console.log(process.argv) +if (process.argv[2] === 'dev') { + console.log('checking if settings already exist...'); + if (!fs.existsSync('./public/settings.json')) { + console.log('copying dev settings over'); + fs.copyFileSync('./settings.dev.json', './public/settings.json'); + } else { + console.log('settings exist...doing nothing') + } +} else if (process.argv[2] === 'prod') { + const gitInfo = execSync('git describe --tags --long').toString(); + console.log('Packaging version ' + gitInfo); + + let prodConfig = JSON.parse(fs.readFileSync('./settings.prod.json', 'utf8')); + prodConfig.version = gitInfo.replace('\r', '').replace('\n', ''); + + fs.writeFileSync('./build/settings.json', JSON.stringify(prodConfig, null, 2), 'utf8'); +} \ No newline at end of file diff --git a/package.json b/package.json index 2793ec9d..8c76880e 100644 --- a/package.json +++ b/package.json @@ -61,8 +61,9 @@ "precommit": "lint-staged", "server": "node ./server/test-malcolm-server.js", "server:dev": "nodemon ./server/test-malcolm-server.js", - "start": "concurrently \"npm run server:dev\" \"react-scripts start\"", - "build": "react-scripts build", + "start": "npm run config:dev & concurrently \"npm run server:dev\" \"react-scripts start\"", + "build": "npm run config:dev & react-scripts build", + "build:prod": "npm run build & npm run config:prod", "test": "react-scripts test --env=jsdom --coverage", "test:watch": "react-scripts test --env=jsdom --watch", "eject": "react-scripts eject", @@ -77,7 +78,9 @@ "storybook": "start-storybook -p 9009 -s public", "build-storybook": "build-storybook -s public", "build-docs": "sphinx-build -b html docs/source docs/build/html", - "sonarqube": "sonar-scanner" + "sonarqube": "sonar-scanner", + "config:dev": "node ./continuous_integration/update_settings.js dev", + "config:prod": "node ./continuous_integration/update_settings.js prod" }, "devDependencies": { "babel-core": "^6.26.0", diff --git a/settings.dev.json b/settings.dev.json new file mode 100644 index 00000000..305e6f8c --- /dev/null +++ b/settings.dev.json @@ -0,0 +1,4 @@ +{ + "malcolmSocket": "ws://localhost:8000/ws", + "version": "1.0.0" +} \ No newline at end of file diff --git a/example-settings.json b/settings.prod.json similarity index 100% rename from example-settings.json rename to settings.prod.json diff --git a/src/malcolm/actions/socket.actions.js b/src/malcolm/actions/socket.actions.js index 0fe21ddd..ea674a2b 100644 --- a/src/malcolm/actions/socket.actions.js +++ b/src/malcolm/actions/socket.actions.js @@ -11,18 +11,22 @@ export const registerSocketAndConnect = (socketContainer, socketUrl) => ({ }); export const configureSocket = socketContainer => dispatch => { - const baseUrl = `${window.location.protocol}//${window.location.host}/`; + const baseUrl = `${window.location.protocol}//${window.location.host}`; axios.get(`${baseUrl}/settings.json`).then(res => { const settings = res.data; console.log(settings); dispatch(updateVersionNumber(settings.version)); - const malcolmSocket = - settings.malcolmSocket || `ws://${window.location.host}/ws`; - dispatch(registerSocketAndConnect(socketContainer, malcolmSocket)); + // in production no socket will be defined and it will default to ws://{{host}}/ws + if (settings.malcolmSocket) { + dispatch( + registerSocketAndConnect(socketContainer, settings.malcolmSocket) + ); + } }); }; export default { configureSocket, + registerSocketAndConnect, }; diff --git a/src/malcolm/malcolmSocketHandler.js b/src/malcolm/malcolmSocketHandler.js index 66c7682e..1540b6ae 100644 --- a/src/malcolm/malcolmSocketHandler.js +++ b/src/malcolm/malcolmSocketHandler.js @@ -17,10 +17,6 @@ import handleLocationChange from './middleware/malcolmRouting'; const configureMalcolmSocketHandlers = (inputSocketContainer, store) => { const socketContainer = inputSocketContainer; - if (socketContainer.socket instanceof MalcolmReconnector) { - // setTimeout(socketContainer.socket.connect, 0, socketContainer.socket); - } - socketContainer.socket.onerror = error => { const errorString = JSON.stringify(error); store.dispatch( diff --git a/src/navbar/navcontrol.test.js b/src/navbar/navcontrol.test.js index 898c1904..b4f1a4f3 100644 --- a/src/navbar/navcontrol.test.js +++ b/src/navbar/navcontrol.test.js @@ -54,6 +54,7 @@ describe('NavControl', () => { wrapper.find('IconButton').simulate('click'); - expect(wrapper.state.anchorEl).toEqual('werwer'); + console.log(wrapper.state()); + expect(wrapper.state('anchorEl')).toEqual('werwer'); }); });