diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..0c14cc2 --- /dev/null +++ b/.babelrc @@ -0,0 +1,8 @@ +{ + "presets": [ + "es2015" + ], + "plugins": [ + "transform-object-rest-spread" + ] +} \ No newline at end of file diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..7b782ae --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,27 @@ +{ + "env": { + "node": true, + "es6": true + }, + "extends": "airbnb", + "parserOptions": { + "ecmaFeatures": { + "experimentalObjectRestSpread": true + } + }, + "rules": { + "no-console": 0, + "no-mixed-operators": 0, + "react/require-extension": 0, + "arrow-body-style": 0, + "no-confusing-arrow": 0, + "import/prefer-default-export": 0, + "new-cap": 0, + "no-underscore-dangle": 0, + "camelcase": 0, + "no-else-return": 0, + "no-process-env": 2, + "quotes": [1, "double"], + "no-console": [2, { "allow": ["error"] }] + } +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2d7ec5c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +node_modules/ diff --git a/app/config/index.js b/app/config/index.js new file mode 100644 index 0000000..ce4c5e9 --- /dev/null +++ b/app/config/index.js @@ -0,0 +1,4 @@ +/* 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 new file mode 100644 index 0000000..d52b169 --- /dev/null +++ b/app/index.js @@ -0,0 +1,45 @@ +/* eslint no-process-env: 0 */ + +import ws from "ws"; +import url from "url"; +import config from "./config"; + +const updateConfig = (webSocket, environment) => { + webSocket.send(environment); +}; + +export const startUpdateConfig = (environment) => { + const connect = () => { + const webSocket = new ws(url.resolve(config.get("CONFIG_SERVICE_URL"), environment)); + + webSocket.on("open", () => { + updateConfig(webSocket, environment); + setInterval( + updateConfig, + config.get("INTERVAL"), + webSocket, + environment, + ); + }); + + webSocket.on("message", (message) => { + process.env = { + ...process.env, + ...JSON.parse(message), + }; + }); + + webSocket.on("error", (error) => { + console.error("Error:", error); + }); + + webSocket.on("close", () => { + console.error("Socket closed"); + setTimeout(connect, 2000); + }); + }; + + connect(); +}; + +startUpdateConfig("dev"); diff --git a/env.manifest b/env.manifest new file mode 100644 index 0000000..fc237e8 --- /dev/null +++ b/env.manifest @@ -0,0 +1,2 @@ +INTERVAL : Int +CONFIG_SERVICE_URL : String \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..d4cac0b --- /dev/null +++ b/index.js @@ -0,0 +1,2 @@ +require("babel-register"); +require("./app"); diff --git a/package.json b/package.json new file mode 100644 index 0000000..b13a5e4 --- /dev/null +++ b/package.json @@ -0,0 +1,29 @@ +{ + "name": "next-config-applier", + "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 ." + }, + "author": "", + "license": "ISC", + "devDependencies": { + "babel-core": "^6.18.2", + "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" + } +}