-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.sh
executable file
·212 lines (179 loc) · 4.36 KB
/
init.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Check if argument was passed
if [ $# -eq 0 ]; then
echo "Usage: curl https://raw.githubusercontent.com/d-eisenga/project-init/master/init.sh | bash -s <project-name>"
exit 1
fi
dir="$1"
# Abort if directory exists
if [ -d $dir ]; then
echo "Directory '$dir' already exists. Exiting."
exit 1
fi
# Create a new directory
mkdir $dir
cd $dir
# Create git repo
git init
# Create .gitignore
echo 'node_modules/
dist/
.cache/' > .gitignore
# Get Node and NPM versions (cut the 'v' off the Node version)
node_version=$(node -v | sed -e 's/^v//')
npm_version=$(npm -v)
# Create package.json
echo '{
"name": "'$dir'",
"version": "0.0.0",
"scripts": {
"start": "parcel src/index.html",
"build": "parcel build src/index.html"
},
"private": true,
"license": "UNLICENSED",
"engines": {
"node": "'^$node_version'",
"npm": "'^$npm_version'"
}
}' > package.json
# Install dependencies
npm i -D \
babel-core \
babel-preset-env \
classnames \
@types/classnames \
parcel \
parcel-plugin-clean-dist \
postcss \
postcss-modules \
preact \
redux-zero \
sass \
typescript
# Add directories
mkdir -p src/components/App
mkdir -p src/@types/scss
mkdir -p src/store/actions
# Static files
# ============
# .babelrc
echo '{
"presets": [
["env", {
"targets": {
"browsers": ["last 2 Chrome versions"]
}
}]
]
}' > .babelrc
# .postcssrc
echo '{
"modules": true
}' > .postcssrc
# tsconfig.json
echo '{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es2018",
"lib": [
"es2018",
"esnext.array",
"scripthost",
"dom"
],
"strict": true,
"baseUrl": "./",
"paths": {
"*": ["src/@types/*"]
},
"jsx": "react",
"jsxFactory": "h",
"esModuleInterop": true
}
}' > tsconfig.json
# src/index.html
echo '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app"></div>
<script src="./index.tsx"></script>
</body>
</html>' > src/index.html
# src/index.tsx
echo "import {h, render} from 'preact';
import App from './components/App';
render(<App/>, document.getElementById('app')!);" > src/index.tsx
# src/types.d.ts
echo "export interface State {
greeting: string;
}" > src/types.d.ts
# src/@types/scss/index.d.ts
echo "declare module '*.scss';" > src/@types/scss/index.d.ts
# src/store/index.ts
echo "import createStore from 'redux-zero';
import getInitialState from './getInitialState';
const store = createStore(getInitialState());
export default store;" > src/store/index.ts
# src/store/getInitialState.ts
echo "import {State} from '../types';
const getInitialState = (): State => ({
greeting: 'Hello, world!',
});
export default getInitialState;" > src/store/getInitialState.ts
# src/store/actions/state.ts
echo "import store from '../';
import {State} from '../../types';
export const getState = () => store.getState();
export const setState = (f: ((state: State) => Partial<State>)|Partial<State>) => store.setState(f);" > src/store/actions/state.ts
# src/components/App/index.tsx
echo "import {h} from 'preact';
import {Connect, Provider} from 'redux-zero/preact';
import store from '../../store';
import {setState} from '../../store/actions/state';
import {State} from '../../types';
import styles from './styles.scss';
type StateProps = Pick<State, 'greeting'>;
const mapToProps = ({greeting}: State): StateProps => ({greeting});
const leave = () => setState({greeting: 'Goodbye, cruel world!'});
const App = () => (
<Provider store={store}>
<Connect mapToProps={mapToProps}>
{({greeting}: StateProps) => (
<div class={styles.app}>
{greeting}<br/>
<button class={styles.button} onClick={leave}>Goodbye</button>
</div>
)}
</Connect>
</Provider>
);
export default App;" > src/components/App/index.tsx
# src/components/App/styles.scss
echo ':global {
html, body {
margin: 0;
padding: 0;
}
* {
box-sizing: inherit;
cursor: inherit;
user-select: inherit;
}
}
.app {
font-family: "Trebuchet MS", sans-serif;
cursor: default;
user-select: none;
box-sizing: border-box;
padding: 12px;
}
.button {
margin-top: 8px;
}' > src/components/App/styles.scss