From cace59888e5264fa36720fd77eca6c9c8e48753c Mon Sep 17 00:00:00 2001 From: Mike Murray Date: Fri, 3 Apr 2020 15:39:54 -0700 Subject: [PATCH 1/8] chore: update hydra script to use env vars Signed-off-by: Mike Murray --- package/bin/create-hydra-client.js | 87 --------------------- package/bin/init-hydra | 15 ++++ package/scripts/createHydraClient.js | 108 +++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 87 deletions(-) delete mode 100644 package/bin/create-hydra-client.js create mode 100644 package/bin/init-hydra create mode 100644 package/scripts/createHydraClient.js diff --git a/package/bin/create-hydra-client.js b/package/bin/create-hydra-client.js deleted file mode 100644 index e1a932d..0000000 --- a/package/bin/create-hydra-client.js +++ /dev/null @@ -1,87 +0,0 @@ -#!/usr/bin/env node -/* eslint-disable node/shebang */ -/* eslint-disable no-console */ - -const http = require("http"); -const { URL } = require("url"); - -// const missing = [ -// "OAUTH2_ADMIN_URL", -// "OAUTH2_CLIENT_ID", -// "ROOT_URL" -// ].filter((key) => !process.env[key]); - -const OAUTH2_ADMIN_HOST = "localhost"; -const OAUTH2_ADMIN_PORT = 4445; -const OAUTH2_CLIENT_ID = "reaction-admin-core"; -const ROOT_URL = "http://localhost:8081"; - -// if (missing.length) { -// console.error(`ERROR: Missing required environment variables ${missing.join(" ")}`); -// process.exit(12); -// } - -const makeAbsolute = (relativeUrl, baseUrl = ROOT_URL) => { - const url = new URL(relativeUrl, baseUrl); - return url.href; -}; - -/* eslint-disable camelcase */ -const bodyEncoded = JSON.stringify({ - client_id: OAUTH2_CLIENT_ID, - token_endpoint_auth_method: "none", - redirect_uris: [ - makeAbsolute("/authentication/callback"), - makeAbsolute("/authentication/silent_callback") - ], - grant_types: ["authorization_code"], - response_types: ["code"], - post_logout_redirect_uris: [ROOT_URL] -}); -/* eslint-enable camelcase */ - -const options = { - hostname: OAUTH2_ADMIN_HOST, - port: OAUTH2_ADMIN_PORT, - path: "/clients", - method: "POST", - headers: { - "Content-Type": "application/json", - "Content-Length": Buffer.byteLength(bodyEncoded) - } -}; - -const req = http.request(options, (res) => { - res.setEncoding("utf8"); - let body = ""; - res.on("data", (chunk) => { - body += chunk.toString(); - }); - res.on("end", () => { - switch (res.statusCode) { - case 200: - // intentional fallthrough! - // eslint-disable-line no-fallthrough - case 201: - console.log("OK: hydra client created"); - break; - case 409: - console.log("OK: hydra client already exists"); - break; - default: - console.error("ERROR: Could not create hydra client"); - console.error(body); - // eslint-disable-next-line no-process-exit - process.exit(10); - } - }); -}); - -req.on("error", (error) => { - console.error("ERROR: Could not create hydra client"); - console.error(error.message); - // eslint-disable-next-line no-process-exit - process.exit(11); -}); - -req.end(bodyEncoded); diff --git a/package/bin/init-hydra b/package/bin/init-hydra new file mode 100644 index 0000000..dc44932 --- /dev/null +++ b/package/bin/init-hydra @@ -0,0 +1,15 @@ +#!/usr/bin/env node +/* eslint-disable node/shebang */ +const createHydraClient = require("../scripts/createHydraClient"); + +/** + * Run command + * @returns {undefined} + */ +async function run() { + await createHydraClient(); + // eslint-disable-next-line no-process-exit + process.exit(10); +} + +run(); diff --git a/package/scripts/createHydraClient.js b/package/scripts/createHydraClient.js new file mode 100644 index 0000000..46530e8 --- /dev/null +++ b/package/scripts/createHydraClient.js @@ -0,0 +1,108 @@ +/* eslint-disable no-console */ +// eslint-disable-next-line node/no-extraneous-require +require("dotenv").config(); +const http = require("http"); +const { URL } = require("url"); +const envalid = require("envalid"); + +const { + HYDRA_OAUTH2_ADMIN_HOST, + HYDRA_OAUTH2_ADMIN_PORT, + HYDRA_OAUTH2_CLIENT_ID, + HYDRA_REDIRECT_URL +} = envalid.cleanEnv(process.env, { + HYDRA_OAUTH2_ADMIN_HOST: envalid.str({ + desc: "Hydra admin hostname", + example: "localhost" + }), + HYDRA_OAUTH2_ADMIN_PORT: envalid.num({ + default: "reaction-admin-core", + desc: "Hydra admin port", + example: 4445 + }), + HYDRA_OAUTH2_CLIENT_ID: envalid.str({ + desc: "Client ID", + example: "reaction-admin-core" + }), + HYDRA_REDIRECT_URL: envalid.url({ + desc: "The canonical root URL for the Reaction Admin Core site", + example: "http://localhost:8080" + }) +}); + +const makeAbsolute = (relativeUrl, baseUrl = HYDRA_REDIRECT_URL) => { + const url = new URL(relativeUrl, baseUrl); + return url.href; +}; + +/* eslint-disable camelcase */ +const bodyEncoded = JSON.stringify({ + client_id: HYDRA_OAUTH2_CLIENT_ID, + token_endpoint_auth_method: "none", + redirect_uris: [ + makeAbsolute("/authentication/callback"), + makeAbsolute("/authentication/silent_callback") + ], + grant_types: ["authorization_code"], + response_types: ["code"], + post_logout_redirect_uris: [HYDRA_REDIRECT_URL] +}); +/* eslint-enable camelcase */ + +const options = { + hostname: HYDRA_OAUTH2_ADMIN_HOST, + port: HYDRA_OAUTH2_ADMIN_PORT, + path: "/clients", + method: "POST", + headers: { + "Content-Type": "application/json", + "Content-Length": Buffer.byteLength(bodyEncoded) + } +}; + +/** + * Create Hydra client + * @returns {Promise} A promise that resolves on success and rejects of failure + */ +module.exports = async function createHydraClient() { + const promise = new Promise((resolve, reject) => { + const req = http.request(options, (res) => { + res.setEncoding("utf8"); + let body = ""; + + res.on("data", (chunk) => { + body += chunk.toString(); + }); + + res.on("end", () => { + switch (res.statusCode) { + case 200: + // intentional fallthrough! + // eslint-disable-line no-fallthrough + case 201: + console.log("OK: hydra client created"); + resolve(); + break; + case 409: + console.log("OK: hydra client already exists"); + resolve(); + break; + default: + console.error("ERROR: Could not create hydra client"); + console.error(body); + reject(10); + } + }); + }); + + req.on("error", (error) => { + console.error("ERROR: Could not create hydra client"); + console.error(error.message); + reject(11); + }); + + req.end(bodyEncoded); + }); + + return promise; +}; From c7313574b2225377c332d37c2587aa6dafc13f5f Mon Sep 17 00:00:00 2001 From: Mike Murray Date: Fri, 3 Apr 2020 15:42:46 -0700 Subject: [PATCH 2/8] fix: remove node_modules dir exclusion Signed-off-by: Mike Murray --- package/config/webpack.common.js | 1 - 1 file changed, 1 deletion(-) diff --git a/package/config/webpack.common.js b/package/config/webpack.common.js index 7553174..8204115 100644 --- a/package/config/webpack.common.js +++ b/package/config/webpack.common.js @@ -35,7 +35,6 @@ module.exports = { }, { test: /\.(graphql|gql)$/, - exclude: /node_modules/, loader: "graphql-tag/loader" }, { From 6b7837c6aaa4e981cb1a38e0481cf1fab6bea2e4 Mon Sep 17 00:00:00 2001 From: Mike Murray Date: Fri, 3 Apr 2020 15:49:03 -0700 Subject: [PATCH 3/8] fix: update build script Signed-off-by: Mike Murray --- .../bin/{reaction-plugin-build => build-plugin} | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) rename package/bin/{reaction-plugin-build => build-plugin} (59%) diff --git a/package/bin/reaction-plugin-build b/package/bin/build-plugin similarity index 59% rename from package/bin/reaction-plugin-build rename to package/bin/build-plugin index ff1b249..15f8c73 100755 --- a/package/bin/reaction-plugin-build +++ b/package/bin/build-plugin @@ -9,10 +9,11 @@ rimraf dist-modules-temp BABEL_ENV=production BABEL_MODULES=1 babel --root-mode upward src --out-dir dist # Copy build scripts, config and other sources -cp -r bin dist/bin -cp -r config dist/config -cp -r src/graphql dist/graphql -cp -r src/test-utils dist/test-utils -cp package.json dist/package.json -cp README.md dist/README.md -cp LICENSE dist/LICENSE +cp -r bin dist +cp -r config dist +cp -r scripts dist +cp -r src/graphql dist +cp -r src/test-utils dist +cp package.json dist +cp README.md dist +cp LICENSE dist From 2b2ee2388fb2cf90568eefdab217e1f80674b799 Mon Sep 17 00:00:00 2001 From: Mike Murray Date: Fri, 3 Apr 2020 15:50:08 -0700 Subject: [PATCH 4/8] feat: create hydra client when starting dev sever Also get the port from ENV Signed-off-by: Mike Murray --- package/bin/dev-server | 3 ++ package/bin/server.js | 33 ---------------------- package/scripts/devServer.js | 53 ++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 33 deletions(-) create mode 100755 package/bin/dev-server delete mode 100755 package/bin/server.js create mode 100755 package/scripts/devServer.js diff --git a/package/bin/dev-server b/package/bin/dev-server new file mode 100755 index 0000000..2bbbb69 --- /dev/null +++ b/package/bin/dev-server @@ -0,0 +1,3 @@ +#!/usr/bin/env node +/* eslint-disable node/shebang */ +require("../scripts/devServer")(); diff --git a/package/bin/server.js b/package/bin/server.js deleted file mode 100755 index 7732d00..0000000 --- a/package/bin/server.js +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env node -/* eslint-disable no-console */ - -const argv = require("minimist")(process.argv.slice(2)); - -// node.js server used to serve assets bundled by Webpack -// use `npm start` command to launch the server. -const webpack = require("webpack"); -const WebpackDevServer = require("webpack-dev-server"); -const config = require("../config/webpack.dev.js"); - -const port = argv.port || 8080; - -const options = { - publicPath: config.output.publicPath, - hot: true, - inline: true, - open: true, - contentBase: "build", - stats: { colors: true }, - historyApiFallback: true -}; - -const server = new WebpackDevServer(webpack(config), options); - -console.log("Starting the dev web server..."); - -server.listen(port, "0.0.0.0", (err) => { - if (err) { - console.log(err); - } - console.log("WebpackDevServer listening at localhost:", port); -}); diff --git a/package/scripts/devServer.js b/package/scripts/devServer.js new file mode 100755 index 0000000..8a2715f --- /dev/null +++ b/package/scripts/devServer.js @@ -0,0 +1,53 @@ +#!/usr/bin/env node +/* eslint-disable node/shebang */ +/* eslint-disable no-console */ +// eslint-disable-next-line node/no-extraneous-require +require("dotenv").config(); + +// node.js server used to serve assets bundled by Webpack +// use `npm start` command to launch the server. +const webpack = require("webpack"); +const WebpackDevServer = require("webpack-dev-server"); +const envalid = require("envalid"); +const config = require("../config/webpack.dev.js"); +const createHydraClient = require("./createHydraClient"); + +const { + DEV_SERVER_PORT +} = envalid.cleanEnv(process.env, { + DEV_SERVER_PORT: envalid.num({ + desc: "Dev server port", + example: "8080", + default: 8080 + }) +}); + +const options = { + publicPath: config.output.publicPath, + hot: true, + inline: true, + open: true, + contentBase: "build", + stats: { colors: true }, + historyApiFallback: true +}; + +const server = new WebpackDevServer(webpack(config), options); + +/** + * Start the devserver + * @returns {Promise} An implicit promise + */ +module.exports = async function start() { + console.log("Creating hydra client..."); + await createHydraClient(); + + console.log("Starting the dev web server..."); + + server.listen(DEV_SERVER_PORT, "0.0.0.0", (err) => { + if (err) { + console.log(err); + } + console.log("WebpackDevServer listening at localhost:", DEV_SERVER_PORT); + }); +}; From 89a08d429db15d264ac9926b9bdc2d8e61032cc7 Mon Sep 17 00:00:00 2001 From: Mike Murray Date: Fri, 3 Apr 2020 15:50:34 -0700 Subject: [PATCH 5/8] chore: update scripts Signed-off-by: Mike Murray --- package.json | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index f2d9abc..e4dcb85 100644 --- a/package.json +++ b/package.json @@ -4,21 +4,15 @@ "description": "Reaction admin core library testing app", "main": "index.js", "scripts": { - "build": "./package/bin/reaction-plugin-build", + "build": "./package/bin/build-plugin", "build:app": "webpack --config ./package/config/webpack.prod.js", "bp": "npm run build && npm run publish", - "publish": "yalc publish --push", - "hydra:init": "node ./package/bin/create-hydra-client.js", "lint": "eslint ./package/src", - "start": "./package/bin/server.js --port 8081", + "start": "./package/bin/dev-server", "test": "jest ./package/src", "test:watch": "jest --watch ./package/src", "test:file": "jest --no-cache --watch" }, - "bin": { - "reaction": "./package/bin/server.js", - "reaction-build": "./package/bin/reaction-plugin-build" - }, "keywords": [], "author": "", "license": "ISC", From b4596f9c8fe93eb50feb9f48f0437cdca74a116a Mon Sep 17 00:00:00 2001 From: Mike Murray Date: Fri, 3 Apr 2020 15:51:14 -0700 Subject: [PATCH 6/8] chore: add example env vars Signed-off-by: Mike Murray --- .env.example | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index a3f8406..7395b03 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,14 @@ +# Devserver +DEV_SERVER_PORT=8081 + +# ORY Hydra clinet configuration +HYDRA_OAUTH2_ADMIN_HOST=localhost +HYDRA_OAUTH2_ADMIN_PORT=4445 +HYDRA_OAUTH2_CLIENT_ID=reaction-admin-core +HYDRA_REDIRECT_URL=http://localhost:8081 + +# Public vars for the web-app PUBLIC_GRAPHQL_API_URL=http://localhost:3000/graphql PUBLIC_OIDC_CLIENT_ID=reaction-admin-core PUBLIC_OIDC_URL=http://localhost:4444 -PUBLIC_ROOT_URL=http://localhost:8081 \ No newline at end of file +PUBLIC_ROOT_URL=http://localhost:8081 From 9011d11fda461e64afbd25a37a7c009005b580c2 Mon Sep 17 00:00:00 2001 From: Mike Murray Date: Fri, 3 Apr 2020 15:52:50 -0700 Subject: [PATCH 7/8] feat: update bin scripts Signed-off-by: Mike Murray --- package/package.json | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/package/package.json b/package/package.json index 91ee7c6..daff8d8 100644 --- a/package/package.json +++ b/package/package.json @@ -16,12 +16,12 @@ "access": "public" }, "scripts": { - "build": "bin/reaction-plugin-build", - "init:hydra": "node bin/create-hydra-client.js" + "build": "bin/build-plugin" }, "bin": { - "reaction": "./bin/server.js", - "reaction-build": "./bin/reaction-plugin-build" + "reaction-dev-server": "./bin/dev-server", + "reaction-build-plugin": "./bin/build-plugin", + "reaction-init-hydra": "./bin/init-hydra" }, "dependencies": { "@apollo/react-hooks": "^3.1.3", @@ -73,7 +73,6 @@ "html-loader": "~0.5.5", "html-webpack-plugin": "~3.2.0", "jest": "^25.1.0", - "minimist": "~1.2.5", "oidc-client": "~1.10.1", "prop-types": "^15.7.2", "raf": "^3.4.1", From e7c2bb5b4646227be401943ba52c6b9fb8aba3dd Mon Sep 17 00:00:00 2001 From: Mike Murray Date: Fri, 3 Apr 2020 16:08:59 -0700 Subject: [PATCH 8/8] docs: update running instructions Signed-off-by: Mike Murray --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d809326..8ccdb99 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,13 @@ ### With Docker +1. `cp .env.example .env` 1. `docker-compose up` ### Outside of Docker -1. `npm install` -1. `cd package && npm install` -1. `cd package && mpn run init:hydra` +1. `npm install && (cd package && npm install)` +1. `cp .env.example .env` 1. `npm start` ### Developer Certificate of Origin