Skip to content

Instantly share code, notes, and snippets.

@jgilless
Last active December 7, 2017 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgilless/c36dde378a2f57408a275aa56236cd26 to your computer and use it in GitHub Desktop.
Save jgilless/c36dde378a2f57408a275aa56236cd26 to your computer and use it in GitHub Desktop.
Create Next App, instant regret writing it in bash not node.
#!/bin/bash
if [ -z "$1" ]
then
echo "No argument supplied"
exit 1
fi
mkdir $1 && cd $1
function run_npm {
npm init --yes
npm install --save next react react-dom redux react-redux next-redux-wrapper redux-thunk redux-devtools-extension
}
function create_env {
touch .env
echo "NODE_ENV=development" > .env
}
function create_store_js {
mkdir -p src/state
touch src/state/store.js
echo "import { createStore, combineReducers, compose, applyMiddleware } from 'redux';
import thunk from 'redux-thunk'
import { composeWithDevTools } from 'redux-devtools-extension';
import example from './example/reducer.js';
const { NODE_ENV } = process.env;
const reducers = {
example
};
let store = null;
export const initStore = (initialState = {}) => {
const reducer = combineReducers(reducers);
if (typeof window === 'undefined') {
store = createStore(reducer, initialState, compose(applyMiddleware(thunk)));
return store;
}
const composeEnhancers = NODE_ENV === 'development' ? composeWithDevTools : compose;
if (!store) {
store = createStore(reducer, initialState, composeEnhancers(applyMiddleware(thunk)));
// set window.store for production debugging purposes
window.store = store;
}
return store;
};
" > src/state/store.js
}
function create_example_reducer {
mkdir -p src/state/example
touch src/state/example/actions.js
touch src/state/example/constants.js
touch src/state/example/reducer.js
echo "export const EXAMPLE_ACTION = 'EXAMPLE_ACTION';" > src/state/example/constants.js
echo "import { EXAMPLE_ACTION } from './constants';
export const exampleAction = (data) => {
return {
type: EXAMPLE_ACTION,
data: data
}
};" > src/state/example/actions.js
echo "import { EXAMPLE_ACTION } from './constants';
const defaultState = {
exampleKey: 'exampleValue'
};
export default (state = defaultState, action) => {
const { type, data } = action;
switch (type) {
case EXAMPLE_ACTION: {
return Object.assign({}, state, {
data
});
}
default:
return state;
}
};" > src/state/example/reducer.js
}
function create_example_component {
mkdir -p src/components
touch src/components/example.js
echo "import React, { Component } from 'react';
class ExampleComponent extends Component {
render() {
return (
<div>
<h2>Example Component!</h2>
</div>
);
}
}
export default ExampleComponent;" > src/components/example.js
}
function create_pages {
mkdir -p pages
touch pages/index.js
echo "import React, { Component } from 'react';
import withRedux from 'next-redux-wrapper';
import { connect } from 'react-redux';
import ExampleComponent from '../src/components/example';
import { initStore } from '../src/state/store';
class App extends Component {
render() {
return (
<div>
<ExampleComponent />
</div>
);
}
}
export default withRedux(initStore)(App);" > pages/index.js
}
function add_scripts_to_package {
node -e "
var fs = require('fs');
var fileName = './package.json';
var file = require(fileName);
file.scripts.dev = 'next';
fs.writeFile(fileName, JSON.stringify(file, null, 2), function (err) {
if (err) return console.log(err);
console.log(JSON.stringify(file));
console.log('writing to ' + fileName);
});
"
}
run_npm
create_env
create_example_component
create_example_reducer
create_pages
create_store_js
add_scripts_to_package
printf "\n\n\n*************************\n\nYour app is now ready, run:\n\n"
printf "\e[1;32m%s\e[0m\n" "cd $1 && npm run dev"
printf "\nto start your app\n\n*************************\n\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment