Skip to content
This repository has been archived by the owner on Dec 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #9 from reactioncommerce/chore-mikemurray-dev-scripts
Browse files Browse the repository at this point in the history
feat/core: Update dev scripts
  • Loading branch information
mikemurray authored Apr 3, 2020
2 parents 2788be0 + e7c2bb5 commit 858940e
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 145 deletions.
12 changes: 11 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -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
PUBLIC_ROOT_URL=http://localhost:8081
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 8 additions & 7 deletions package/bin/reaction-plugin-build → package/bin/build-plugin
Original file line number Diff line number Diff line change
Expand Up @@ -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
87 changes: 0 additions & 87 deletions package/bin/create-hydra-client.js

This file was deleted.

3 changes: 3 additions & 0 deletions package/bin/dev-server
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node
/* eslint-disable node/shebang */
require("../scripts/devServer")();
15 changes: 15 additions & 0 deletions package/bin/init-hydra
Original file line number Diff line number Diff line change
@@ -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();
33 changes: 0 additions & 33 deletions package/bin/server.js

This file was deleted.

1 change: 0 additions & 1 deletion package/config/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ module.exports = {
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: "graphql-tag/loader"
},
{
Expand Down
9 changes: 4 additions & 5 deletions package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
108 changes: 108 additions & 0 deletions package/scripts/createHydraClient.js
Original file line number Diff line number Diff line change
@@ -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;
};
Loading

0 comments on commit 858940e

Please sign in to comment.