forked from Workday/canvas-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-module.sh
executable file
·285 lines (245 loc) · 6.25 KB
/
create-module.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/bin/bash
cat << EOM
This utility will walk you through creating a new canvas-kit module.
Press ^C at any time to quit.
EOM
# Colors
RED='\033[0;31m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Get module name
read -p "Module/component name (@workday/canvas-kit-<TARGET>-<NAME>): " name
path="./modules/$name"
reactPath="$path/react"
wantsCss=false
if [ -d "$path" ]; then
echo -e "${RED}Module with name '$name' already exists."
exit 1
fi
# Get module info
read -p "Module description: " description
upperName="$(tr '[:lower:]' '[:upper:]' <<< ${name:0:1})${name:1}"
# Create module directory
echo -e "\nCreating ${CYAN}$path${NC}"
mkdir $path
mkdir $reactPath
# Create package.json
packageJson="$reactPath/package.json"
echo -e "Creating ${CYAN}$packageJson${NC}"
cat > $packageJson << EOF
{
"name": "@workday/canvas-kit-react-$name",
"version": "0.0.0",
"description": "$description",
"author": "Workday, Inc. (https://www.workday.com)",
"license": "Apache-2.0",
"main": "dist/commonjs/index.js",
"module": "dist/es6/index.js",
"sideEffects": false,
"types": "dist/es6/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/Workday/canvas-kit/tree/master/modules/$name/react"
},
"files": [
"dist/",
"lib/",
"index.ts"
],
"scripts": {
"clean": "rimraf dist && rimraf .build-info && mkdirp dist",
"build:cjs": "tsc -p tsconfig.cjs.json",
"build:es6": "tsc -p tsconfig.es6.json",
"build:rebuild": "npm-run-all clean build",
"build": "npm-run-all --parallel build:cjs build:es6"
},
"keywords": [
"canvas",
"canvas-kit",
"react",
"components",
"workday",
"$name"
],
"peerDependencies": {
"react": ">= 16.8 < 17"
}
}
EOF
# Create lib/MyComponent.tsx
mkdir "$reactPath/lib"
componentTsx="$reactPath/lib/MyComponent.tsx"
echo -e "Creating ${CYAN}$componentTsx${NC}"
cat > $componentTsx << EOF
import * as React from 'react'
export default class MyComponent extends React.Component {
public render() {
return (
<div>
MyComponent
</div>
)
}
}
EOF
# Create index.ts
indexTs="$reactPath/index.ts"
echo -e "Creating ${CYAN}$indexTs${NC}"
cat > $indexTs << EOF
import MyComponent from './lib/MyComponent';
export default MyComponent;
export {MyComponent};
export * from './lib/MyComponent';
EOF
# Create stories.tsx
mkdir "$reactPath/stories"
storiesJs="$reactPath/stories/stories.tsx"
echo -e "Creating ${CYAN}$storiesJs${NC}"
cat > $storiesJs << EOF
/// <reference path="../../../../typings.d.ts" />
import * as React from 'react';
import {storiesOf} from '@storybook/react';
import withReadme from 'storybook-readme/with-readme';
import MyComponent from '../index';
import README from '../README.md';
storiesOf('Canvas Kit/$upperName', module)
.addDecorator(withReadme(README))
.add('All', () => (
<div className="story">
<MyComponent />
</div>
));
EOF
# Create README.md
readme="$reactPath/README.md"
echo -e "Creating ${CYAN}$readme${NC}"
cat > $readme << EOF
# Canvas Kit $upperName
EOF
# Create tsconfig.json
tsconfig="$reactPath/tsconfig.json"
echo -e "Creating ${CYAN}$tsconfig${NC}"
cat > $tsconfig << EOF
{
"extends": "../../../tsconfig.json",
"exclude": ["node_modules", "ts-tmp", "dist", "spec", "stories"]
}
EOF
# Create tsconfig.cjs.json
tsconfig="$reactPath/tsconfig.cjs.json"
echo -e "Creating ${CYAN}$tsconfig${NC}"
cat > $tsconfig << EOF
{
"extends": "./tsconfig.json",
"compilerOptions": {
"declaration": true,
"module": "commonjs",
"outDir": "dist/commonjs",
"skipLibCheck": true,
"tsBuildInfoFile": "./.build-info/tsconfig.cjs.tsbuildinfo"
}
}
EOF
# Create tsconfig.es6.json
tsconfig="$reactPath/tsconfig.es6.json"
echo -e "Creating ${CYAN}$tsconfig${NC}"
cat > $tsconfig << EOF
{
"extends": "./tsconfig.json",
"compilerOptions": {
"declaration": true,
"outDir": "dist/es6",
"tsBuildInfoFile": "./.build-info/tsconfig.es6.tsbuildinfo"
}
}
EOF
### CSS
echo
read -p "Would you like to create a css module as well? [Y/n] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
wantsCss=true
cssPath="$path/css"
mkdir $cssPath
# Create css package.json
packageJson="$cssPath/package.json"
echo -e "Creating ${CYAN}$packageJson${NC}"
cat > $packageJson << EOF
{
"name": "@workday/canvas-kit-css-$name",
"version": "0.0.0",
"description": "$description",
"author": "Workday, Inc. (https://www.workday.com)",
"license": "Apache-2.0",
"files": [
"dist",
"lib",
"index.scss"
],
"style": "dist/canvas-kit-css-$name.min.css",
"main": "dist/canvas-kit-css-$name.min.css",
"repository": {
"type": "git",
"url": "https://github.com/Workday/canvas-kit/tree/master/modules/$name/css"
},
"scripts": {},
"keywords": [
"canvas",
"canvas-kit",
"css",
"components",
"workday",
"$name"
]
}
EOF
# Create lib/<NAME>.scss
moduleCss="$cssPath/lib/$name.scss"
echo -e "Creating ${CYAN}$moduleCss${NC}"
mkdir -p "$cssPath/lib"
touch $moduleCss
# Create index.scss
indexCss="$cssPath/index.scss"
echo -e "Creating ${CYAN}$indexCss${NC}"
cat > $indexCss << EOF
@import "@workday/canvas-kit-css-core/index.scss";
@import "./lib/$name.scss";
EOF
# Create stories.js
storiesJs="$cssPath/stories.tsx"
echo -e "Creating ${CYAN}$storiesJs${NC}"
cat > $storiesJs << EOF
import React from 'react'
import { storiesOf } from '@storybook/react'
import withReadme from 'storybook-readme/with-readme'
import README from './README.md'
import './index.scss'
storiesOf('CSS/$upperName', module)
.addDecorator(withReadme(README))
.add('Default', () => (
<div className="story">
<h1 className="section-label">$upperName</h1>
</div>
))
EOF
# Create README.md
readme="$cssPath/README.md"
echo -e "Creating ${CYAN}$readme${NC}"
cat > $readme << EOF
# Canvas Kit CSS $upperName
EOF
fi
# Copy LICENSE
echo -e "Adding License file to ${CYAN}$reactPath{NC}"
cp LICENSE $reactPath
# Install deps using Yarn workspaces (instead of Lerna bootstrap)
echo -e "\nInstalling dependencies\n"
yarn
# We always add the React module as dependency and set up export
echo -e 'Adding module as dependency and adding export to index'
node "utils/create-module.js" "$name" "react";
if [ "$wantsCss" = true ] ; then
echo -e 'Adding module as CSS dependency and adding Sass module import'
node "utils/create-module.js" "$name" "css";
fi