Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": ["--runInBand"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": ["${relativeFile}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
13 changes: 7 additions & 6 deletions build-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ var fs = require("fs-extra");
var packageJson = require("./package.json");

var deps = {
"dependencies": ["babel-runtime", "babel-polyfill", "html-webpack-plugin", "prop-types", "react", "react-dom", "react-redux", "react-router", "react-router-dom", "redux", "redux-saga", "webpack", "node-sass", "history"],
"devDependencies": ["babel-core", "babel-eslint", "babel-jest", "babel-loader", "babel-plugin-transform-async-to-generator", "babel-plugin-transform-class-properties", "babel-plugin-transform-es2015-modules-umd", "babel-plugin-transform-object-rest-spread", "babel-plugin-transform-runtime", "babel-preset-env", "babel-preset-react", "bundlesize", "compression-webpack-plugin", "css-loader", "enzyme", "enzyme-adapter-react-16", "eslint-config-mcrowder65", "jest", "fetch-mock", "style-loader", "postcss-loader", "postcss-flexbugs-fixes", "sass-loader", "react-hot-loader", "webpack-dev-server", "identity-obj-proxy", "webpack-bundle-analyzer"]
"dependencies": ["babel-runtime", "babel-polyfill", "html-webpack-plugin", "prop-types", "express", "react", "react-dom", "react-redux", "react-router", "react-router-dom", "redux", "redux-saga", "webpack", "node-sass", "history", "isomorphic-fetch"],
"devDependencies": ["babel-core", "babel-eslint", "babel-jest", "babel-loader", "babel-plugin-transform-async-to-generator", "babel-plugin-transform-class-properties", "babel-plugin-transform-es2015-modules-umd", "babel-plugin-transform-object-rest-spread", "babel-plugin-transform-runtime", "babel-preset-env", "babel-preset-react", "bundlesize", "compression-webpack-plugin", "css-loader", "enzyme", "enzyme-adapter-react-16", "eslint-config-mcrowder65", "jest", "fetch-mock", "style-loader", "shortid", "postcss-loader", "postcss-flexbugs-fixes", "sass-loader", "react-hot-loader", "webpack-dev-server", "identity-obj-proxy", "webpack-bundle-analyzer"]
};
var executeFunction = function executeFunction(func, loadingText) {
var spinner = void 0;
Expand Down Expand Up @@ -124,16 +124,17 @@ var cli = function cli() {
linter: "./node_modules/.bin/eslint src --ext .js,.jsx && ./node_modules/.bin/eslint test --ext .js,.jsx",
webpack: "export NODE_ENV=production && ./node_modules/.bin/webpack -p --progress",
bundlesize: "bundlesize",
"analyze-bundle": "export ANALYZE_BUNDLE=true && npm run webpack"
"analyze-bundle": "export ANALYZE_BUNDLE=true && npm run webpack",
"server-watch": "NODE_ENV=development && babel-watch src/server/index.js"
}),
jest: (0, _extends4.default)({}, pkgJson.jest, {
setupTestFrameworkScriptFile: "<rootDir>/test/client/config.js",
moduleNameMapper: {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/file-mock.js",
"\\.(css|scss|less)$": "identity-obj-proxy"
},
collectCoverageFrom: ["src/client/**/*.{js*}", "!src/client/browser-history.js", "!src/client/app.js", "!src/client/router.js", "!src/client/actions/sagas/config.js", "!src/client/actions/sagas/index.js"],
modulePaths: ["src/client/"],
collectCoverageFrom: ["src/**/*.{js*}", "!src/client/browser-history.js", "!src/client/app.js", "!src/client/router.js", "!src/client/actions/sagas/config.js", "!src/client/actions/sagas/index.js"],
modulePaths: ["src/"],
coverageReporters: ["html"]
})

Expand Down Expand Up @@ -227,7 +228,7 @@ var cli = function cli() {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
files = ["webpack.config.js", ".babelrc", "src/client/actions/sagas/config.js", "src/client/actions/sagas/index.js", "src/client/actions/sagas/ping-server.js", "src/client/actions/sagas/types.js", "src/client/actions/index.js", "src/client/actions/types.js", "src/client/components/home.js", "src/client/reducers/index.js", "src/client/reducers/initial-state.js", "src/client/styles/base.scss", "src/client/app.js", "src/client/browser-history.js", "src/client/index.html", "src/client/router.js", "test/client/__mocks__/file-mock.js", "test/client/actions/sagas/ping-server.spec.js", "test/client/actions/index.spec.js", "test/client/config.js", "test/client/reducers/index.spec.js"];
files = ["webpack.config.js", ".babelrc", "src/client/actions/sagas/config.js", "src/client/actions/sagas/index.js", "src/client/actions/sagas/ping-server.js", "src/client/actions/sagas/types.js", "src/client/actions/index.js", "src/client/actions/types.js", "src/client/components/home.js", "src/client/reducers/index.js", "src/client/reducers/initial-state.js", "src/client/styles/base.scss", "src/client/app.js", "src/client/browser-history.js", "src/client/index.html", "src/client/router.js", "src/server/index.js", "src/shared/constants.js", "src/shared/fetch-wrapper.js", "test/client/__mocks__/file-mock.js", "test/client/actions/sagas/ping-server.spec.js", "test/client/actions/index.spec.js", "test/client/config.js", "test/client/reducers/index.spec.js", "test/server/index.spec.js", "test/shared/fetch-wrapper.spec.js"];
_iteratorNormalCompletion = true;
_didIteratorError = false;
_iteratorError = undefined;
Expand Down
2 changes: 1 addition & 1 deletion index-tests/force.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { executeBashFunction, cli } from "./utils.js";
test("When force exists, it should reject", async () => {
try {
const folder = "force";
await executeBashFunction(`mkdir ${folder}`);
await executeBashFunction(`rm -rf ${folder} && mkdir ${folder}`);
process.argv.push(folder);
await cli();
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion index-tests/forcef.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { executeBashFunction, cli, doesFileExist } from "./utils.js";

test("When forcef exists and you pass -f, it should delete it and work!", async () => {
const folder = "forcef";
await executeBashFunction(`mkdir ${folder}`);
await executeBashFunction(`rm -rf ${folder} && mkdir ${folder}`);
process.argv.push(folder);
process.argv.push("-f");
process.argv.push("-s");
Expand Down
20 changes: 14 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const deps = {
"babel-polyfill",
"html-webpack-plugin",
"prop-types",
"express",
"react",
"react-dom",
"react-redux",
Expand All @@ -21,7 +22,8 @@ const deps = {
"redux-saga",
"webpack",
"node-sass",
"history"
"history",
"isomorphic-fetch"
],
"devDependencies": [
"babel-core",
Expand All @@ -44,6 +46,7 @@ const deps = {
"jest",
"fetch-mock",
"style-loader",
"shortid",
"postcss-loader",
"postcss-flexbugs-fixes",
"sass-loader",
Expand Down Expand Up @@ -89,7 +92,6 @@ const createFolder = folder => {
return executeFunction(callback => fs.mkdir(folder, callback), `Creating ${folder}`);
};


const cli = () => {
return new Promise((outerResolve, outerReject) => {

Expand Down Expand Up @@ -160,7 +162,8 @@ const cli = () => {
linter: "./node_modules/.bin/eslint src --ext .js,.jsx && ./node_modules/.bin/eslint test --ext .js,.jsx",
webpack: "export NODE_ENV=production && ./node_modules/.bin/webpack -p --progress",
bundlesize: "bundlesize",
"analyze-bundle": "export ANALYZE_BUNDLE=true && npm run webpack"
"analyze-bundle": "export ANALYZE_BUNDLE=true && npm run webpack",
"server-watch": "NODE_ENV=development && babel-watch src/server/index.js"
},
jest: {
...pkgJson.jest,
Expand All @@ -170,14 +173,14 @@ const cli = () => {
"\\.(css|scss|less)$": "identity-obj-proxy"
},
collectCoverageFrom: [
"src/client/**/*.{js*}",
"src/**/*.{js*}",
"!src/client/browser-history.js",
"!src/client/app.js",
"!src/client/router.js",
"!src/client/actions/sagas/config.js",
"!src/client/actions/sagas/index.js"
],
modulePaths: ["src/client/"],
modulePaths: ["src/"],
coverageReporters: ["html"]
}

Expand Down Expand Up @@ -236,11 +239,16 @@ npm-debug.log`;
"src/client/browser-history.js",
"src/client/index.html",
"src/client/router.js",
"src/server/index.js",
"src/shared/constants.js",
"src/shared/fetch-wrapper.js",
"test/client/__mocks__/file-mock.js",
"test/client/actions/sagas/ping-server.spec.js",
"test/client/actions/index.spec.js",
"test/client/config.js",
"test/client/reducers/index.spec.js"
"test/client/reducers/index.spec.js",
"test/server/index.spec.js",
"test/shared/fetch-wrapper.spec.js"
];
for (const f of files) {
try {
Expand Down
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-react-matt",
"version": "0.0.87",
"version": "0.0.88",
"description": "React, Redux, Webpack, Babel, Jest, and code coverage all provided for you",
"main": "main-index.js",
"repository": {
Expand All @@ -21,7 +21,8 @@
"jest": "npm run pre-jest && jest --coverage --silent && npm run pre-jest",
"bundlesize": "bundlesize",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
"prepublishOnly": "npm run build"
"prepublishOnly": "npm run build",
"server-watch": "NODE_ENV=development && babel-watch src/server/index.js"
},
"bundlesize": [
{
Expand Down Expand Up @@ -53,7 +54,7 @@
],
"jest": {
"collectCoverageFrom": [
"src/client/**/*.{js*}",
"src/**/*.{js*}",
"index.js",
"!src/client/browser-history.js",
"!src/client/app.js",
Expand All @@ -71,7 +72,7 @@
"lcov"
],
"modulePaths": [
"src/client/"
"src/"
],
"globals": {
"localStorage": {}
Expand Down Expand Up @@ -121,6 +122,7 @@
"history": "4.7.2",
"html-webpack-plugin": "3.2.0",
"identity-obj-proxy": "3.0.0",
"isomorphic-fetch": "2.2.1",
"jest": "23.1.0",
"node-sass": "4.9.0",
"path": "0.12.7",
Expand All @@ -137,6 +139,7 @@
"redux": "4.0.0",
"redux-saga": "0.16.0",
"sass-loader": "7.0.3",
"shortid": "^2.2.8",
"style-loader": "0.21.0",
"webpack": "3.12.0",
"webpack-bundle-analyzer": "2.13.1",
Expand Down
3 changes: 2 additions & 1 deletion src/client/actions/sagas/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { pingServer } from "client/actions/sagas/ping-server";
import * as sagaTypes from "./types";
import { pingServer } from "./ping-server";

// The second argument of your value object can be method,
// where you can override takeEvery and do takeLatest instead
const sagaConfig = {
Expand Down
9 changes: 5 additions & 4 deletions src/client/actions/sagas/ping-server.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { call, put } from "redux-saga/effects";

import { setPing } from "../index";
import { fetchGet } from "../../../shared/fetch-wrapper";
import { HTTP_RESPONSE_TYPES } from "../../../shared/constants";

export const apiCall = async () => {
const url = process.env.NODE_ENV === "production" ? "" : "http://localhost:3000";
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be abstracted

export const apiCall = () => {
// this is up to you whether or not you want to implement this server...
const res = await fetch("http://localhost:3000/ping", { method: "GET" });
return res.text();
return fetchGet({ url: `${url}/ping`, headers: { "Content-Type": HTTP_RESPONSE_TYPES.PLAIN, Accept: HTTP_RESPONSE_TYPES.PLAIN } });
};

// eslint-disable-next-line
export function* pingServer() {
try {
const resp = yield call(apiCall);
Expand Down
1 change: 1 addition & 0 deletions src/client/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Router = () => (
<BrowserRouter history={browserHistory}>
<div>
<Route exact path="/" component={Home}/>
<Route path="/asdf" component={Home}/>
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to make more sense

</div>
</BrowserRouter>

Expand Down
25 changes: 23 additions & 2 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ import bodyParser from "body-parser";
import path from "path";

const app = express();

if (process.env.NODE_ENV === "development") {
// eslint-disable-next-line no-console
console.log("CORs enabled");
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get("*.js", (req, res, next) => {
Expand All @@ -12,6 +23,16 @@ app.get("*.js", (req, res, next) => {
res.set("Content-Encoding", "gzip");
next();
});
app.get("/ping", (req, res) => {
res.send(`The server says hello`);
});
app.use(express.static(path.resolve(__dirname, "../..", "build")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "../../build", "index.html"));
});

// eslint-disable-next-line no-console
app.listen(3000, () => console.log(`server started on port 3000`));
const server = app.listen(3000, () =>
console.log(`server started on port 3000`)
);
export default server;
5 changes: 5 additions & 0 deletions src/shared/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const HTTP_RESPONSE_TYPES = {
JSON: "application/json",
HTML: "text/html",
PLAIN: "text/plain"
};
58 changes: 58 additions & 0 deletions src/shared/fetch-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import "isomorphic-fetch";
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to have a space between these two.

see mcrowder65/eslint-config-mcrowder65#13

import { HTTP_RESPONSE_TYPES } from "./constants";

const resolveResponse = (response, responseType) => {
if (responseType.indexOf(HTTP_RESPONSE_TYPES.JSON) !== -1) {
return response.json();
} else if (responseType.indexOf(HTTP_RESPONSE_TYPES.HTML) !== -1) {
return response.text();
} else if (responseType.indexOf(HTTP_RESPONSE_TYPES.PLAIN) !== -1) {
return response.text();
} else {
throw Error("Response type not supported yet!");
}
};

const initialHeaders = { "Content-Type": HTTP_RESPONSE_TYPES.JSON, Accept: HTTP_RESPONSE_TYPES.JSON };

const fetchWrapper = async ({
url,
method,
body,
headers,
}) => {
// do this here to get Content-Type to not be overridden if you didn't want to.
const actualHeaders = { ...initialHeaders, ...headers };

const response = await fetch(url, {
method,
headers: actualHeaders,
body
});
const responseType = response.headers.get("content-type");
if (!responseType) {
throw Error("Response type was not defined");
}
const resolvedResponse = await resolveResponse(response, responseType);
if (!response.ok) {
throw Error(JSON.stringify(resolvedResponse));
}
return resolvedResponse;
};


export const fetchGet = ({ url, headers }) => {
return fetchWrapper({ url, method: "GET", headers });
};

export const fetchPost = ({ url, body, headers }) => {
return fetchWrapper({ url, method: "POST", body, headers });
};

export const fetchPut = ({ url, body, headers }) => {
return fetchWrapper({ url, method: "PUT", body, headers });
};

export const fetchDelete = ({ url, body, headers }) => {
return fetchWrapper({ url, method: "DELETE", body, headers });
};
Loading