-
Notifications
You must be signed in to change notification settings - Fork 0
/
init
executable file
·219 lines (181 loc) · 6.29 KB
/
init
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
#! /usr/bin/env bash
skipPrompts=f
if [[ "$1" == "-y" || "$1" == "--yes" ]]; then
skipPrompts=t
fi
# ---- SCRIPT SETUP ----
unset CDPATH # Ensure safe CDing support
cd "$(dirname -- "$0")" || exit 1 # Ensure files are created in directory of script
# ---- GET PROJECT NAME ----
# Default project name is base of parent folder
foldername="$(basename -- "$(realpath -- .)")"
unset projectName
if [[ "$skipPrompts" = 't' ]]; then
projectName="$foldername"
else
npm_name_pattern='^(?:@[a-z0-9*~-][a-z0-9*._~-]*/)?[a-z0-9~-][a-z0-9._~-]*$'
while true; do
echo && read -rep "What is the name of the project? " -i "${projectName:-$foldername}" projectName
projectName="${projectName// /-}"
projectName="${projectName,,}"
if [[ -n "$projectName" ]] &&
(printf '%s' "$projectName" | grep -E "$npm_name_pattern" >/dev/null); then
read -rep "Use name '$projectName'? (yes) " usename
if [[ -z "$usename" || "${usename,,}" =~ ^\s*y(es)?\s*$ ]]; then
break
fi
else
cat <<-EOF
Please give a valid name for the project! Requirements:
The name must be less than or equal to 214 characters. This includes the scope for scoped packages.
The names of scoped packages can begin with a dot or an underscore. This is not permitted without a scope.
New packages must not have uppercase letters in the name.
The name ends up being part of a URL, an argument on the command line, and a folder name. Therefore, the name can't contain any non-URL-safe characters.
EOF
fi
done
fi
# ---- FILE SETUP ----
# Remove TODO file
command -v rm &>/dev/null && rm ./TODO
# Create src directory with index.ts file
mkdir ./src
touch ./src/index.ts
# Create Resources directory
mkdir ./resources
# Create README.md file with title of folder base
touch ./README.md
printf '# %s\n' "$projectName" >|README.md
# Replace the year in the License file
sed -si'' "s/\[YEAR\]/$(date +%Y)/g" ./License.*
# Ask user for name IF not --yes
if ! [[ "$skipPrompts" == 't' ]]; then
read -rep "What is your full name for the license? " name
if [[ -n "$name" ]]; then
sed -si'' "s/\[FULLNAME\]/$name/g" ./License.*
fi
fi
if grep -q "\[FULLNAME\]" ./License.*; then
printf '%s\n' "Please replace [FULLNAME] in "./License.*" with your name"
fi
# ---- NPM SETUP ----
# Setup package.json to allow for error avoidance with weird folder names
cat >|package.json <<-EOF
{
"version": "1.0.0",
"description": "",
"author": "",
"private": true,
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
"contributers": ["Aaron Dill"],
"files": ["dist"],
"publishConfig":{"access":"public"}
}
EOF
# Set some important fields
npm pkg set name="$projectName"
# ---- DEVDEPENDENCIES ----
# Add necessary DevDependencies
npm i -D typescript @types/node release-it sort-package-json rimraf eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin >/dev/null
# ---- SCRIPTS ----
# Add utility scripts to npm
npm pkg set scripts.clean="rimraf ./dist" \
scripts.lint="eslint . --fix --ext .js,.ts,.mjs,.mts,.cjs,.cts,.tsx,.jsx"
# Add build script to npm using typescript
npm pkg set scripts.prebuild="npm run lint && npm run clean" \
scripts.build="tsc --build"
# Add watch script to npm using typescript
npm pkg set scripts.prewatch="npm run clean" scripts.watch="tsc --watch"
# Add sortPackage script to npm using sort-package-json
npm pkg set scripts.sort-package="sort-package-json" scripts.prelint="npm run sort-package"
# Add release scripts to release using release-it
npm pkg set scripts.release="release-it"
# ---- ESLINT CONFIG ----
if [[ "$skipPrompts" = 't' ]]; then
useEslint='yes'
else
read -rep "Use included eslint config as root? (yes) " useEslint
fi
if [[ -n "$useEslint" && "${useEslint,,}" =~ ^n(o)?$ ]]; then
# User doesn't want as root
# Replace first root: true with root: false for first instance
sed -si'' '0,/root: true,/s//root: false,/' .eslintrc.*
fi
# ---- TESTING SETUP ----
if [[ "$skipPrompts" = 't' ]]; then
jest='yes'
else
read -rep "Include Jest for testing? (yes) " jest
fi
if [[ -z "$jest" || "${jest,,}" =~ ^y(es)?$ ]]; then
# Install Jest and Jest types
npm i -D jest ts-jest @types/jest >/dev/null
# Setup test scripts
npm pkg set scripts.test="jest"
# Create tests directory and test file for index.ts
mkdir -p tests
touch tests/index.test.ts
else
# Remove jest config if not required
rm jest.config.*
fi
# ---- SORT PACKAGE.JSON ----
# Already installed above
# display output if not --yes
if [[ "$skipPrompts" == 't' ]]; then
npx sort-package-json >/dev/null
npx prettier package.json --write >/dev/null
else
npx sort-package-json
npx prettier package.json --write >/dev/null
fi
# ---- GIT INIT ----
# Remove git files if already present from clone
[[ -d .git ]] && rm -rf .git
if [ "$(command -v git)" ]; then
# Create git repository and commit default files to it, EXCEPT init file
git init >/dev/null &&
git add . &&
git reset init &&
git commit -m "Initial commit" >/dev/null
fi
# ---- GH REPO CREATE ----
# if Github CLI is installed
if [[ "$skipPrompts" = 'false' ]] && command -v gh &>/dev/null; then
read -rep "Would you like to create a repo on Github? (yes) " GithubRepo
if [[ -z "$GithubRepo" || "${GithubRepo,,}" =~ ^\s*y(es)?\s*$ ]]; then
# Create A Github Repo
read -rep "Public or Private? (pub / pr) " -i "pr" public_status
if [[ -n "$public_status" && "${public_status,,}" =~ ^\s*pub(lic)?\s*$ ]]; then
# Confirm and make public if requested
read -rep "Create public repo with name of '$projectName'? (yes) " confirm
if [[ -z "$confirm" || "${confirm,,}" =~ ^\s*y(es)?\s*$ ]]; then
gh repo create "$projectName" --public --source=. --remote=origin --push
else
echo "Aborting"
fi
else
# Confirm and make private by default
read -rep "Create private repo with name of '$projectName'? (yes) " confirm
if [[ -z "$confirm" || "${confirm,,}" =~ ^\s*y(es)?\s*$ ]]; then
gh repo create "$projectName" --private --source=. --remote=origin --push
else
echo "Aborting"
fi
fi
fi
fi
# ---- INIT SCRIPT REMOVAL ----
# Get user confirmation before deleting init script
if [[ "$skipPrompts" = "t" ]]; then
removeInit='yes'
else
echo
read -rep "Remove init script? (yes) " removeInit
fi
if [[ -z "$removeInit" || "${removeInit,,}" =~ ^\s*y(es)?\s*$ ]]; then
rm ./init
fi
echo && echo 'Happy Coding!'