diff --git a/app/config/index.js b/app/config/index.js deleted file mode 100644 index ce4c5e9..0000000 --- a/app/config/index.js +++ /dev/null @@ -1,4 +0,0 @@ -/* eslint no-process-env: 0 */ -import varium from "varium"; - -module.exports = varium(process.env, "env.manifest"); diff --git a/app/index.js b/app/index.js deleted file mode 100644 index 0b26f55..0000000 --- a/app/index.js +++ /dev/null @@ -1,50 +0,0 @@ -/* eslint no-process-env: 0 */ - -import ws from "ws"; -import url from "url"; -import config from "./config"; - -const requestConfig = (webSocket, environment) => { - webSocket.send(environment); -}; - -export const startUpdateConfig = (environment) => { - let retryNumOfTimes = 0; - let interval; - - const connect = () => { - const webSocket = new ws(url.resolve(config.get("CONFIG_SERVICE_URL"), environment)); - - webSocket.on("open", () => { - retryNumOfTimes = 0; - requestConfig(webSocket, environment); - interval = setInterval( - requestConfig, - config.get("INTERVAL"), - webSocket, - environment, - ); - }); - - webSocket.on("message", (message) => { - process.env = { - ...process.env, - ...JSON.parse(message), - }; - }); - - webSocket.on("error", (error) => { - console.error(error); - }); - - webSocket.on("close", () => { - clearInterval(interval); - setTimeout(connect, Math.ceil(Math.E ** retryNumOfTimes) * 100); - retryNumOfTimes += 1; - }); - }; - - connect(); -}; - -startUpdateConfig("dev"); diff --git a/config/index.js b/config/index.js new file mode 100644 index 0000000..adf703f --- /dev/null +++ b/config/index.js @@ -0,0 +1,3 @@ +const varium = require("varium"); + +module.exports = varium(process.env, "env.manifest"); diff --git a/index.js b/index.js index d4cac0b..803680a 100644 --- a/index.js +++ b/index.js @@ -1,2 +1,47 @@ -require("babel-register"); -require("./app"); +const ws = require("ws"); +const url = require("url"); +const config = require("./config"); + +const requestConfig = (webSocket, environment) => { + webSocket.send(environment); +}; + +const startUpdateConfig = (environment) => { + let retryNumOfTimes = 0; + let interval; + + const connect = () => { + const webSocket = new ws(url.resolve(config.get("CONFIG_SERVICE_URL"), environment)); + + webSocket.on("open", () => { + retryNumOfTimes = 0; + requestConfig(webSocket, environment); + interval = setInterval( + requestConfig, + config.get("INTERVAL"), + webSocket, + environment + ); + }); + + webSocket.on("message", (message) => { + process.env = Object.assign(process.env, JSON.parse(message)); + }); + + webSocket.on("error", (error) => { + console.error(error); + }); + + webSocket.on("close", () => { + clearInterval(interval); + setTimeout(connect, Math.ceil(Math.pow(Math.E, retryNumOfTimes)) * 100); + retryNumOfTimes += 1; + }); + }; + + connect(); +}; + +module.exports = startUpdateConfig; + +//startUpdateConfig("dev"); diff --git a/package.json b/package.json index e6d828a..211470e 100644 --- a/package.json +++ b/package.json @@ -1,29 +1,18 @@ { - "name": "next-config-applier", + "name": "next-config-client", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "node .", - "dev-start": "nf run npm run test && nf run npm run start -s", - "test": "eslint --fix ." + "dev-start": "nf run npm run start -s" }, - "author": "", + "author": "Alex Robsahm", "license": "ISC", "devDependencies": { - "babel-core": "^6.18.2", - "babel-plugin-transform-exponentiation-operator": "^6.8.0", - "babel-plugin-transform-object-rest-spread": "^6.19.0", - "babel-preset-es2015": "^6.18.0", - "eslint": "^3.11.1", - "eslint-config-airbnb": "^13.0.0", - "eslint-plugin-import": "^2.2.0", - "eslint-plugin-jsx-a11y": "^2.2.3", - "eslint-plugin-react": "^6.7.1", "foreman": "^2.0.0" }, "dependencies": { - "babel-register": "^6.18.0", "varium": "github:ahultgren/node-varium", "ws": "^1.1.1" }