-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupViteProject.bash
More file actions
executable file
·141 lines (122 loc) · 3.39 KB
/
setupViteProject.bash
File metadata and controls
executable file
·141 lines (122 loc) · 3.39 KB
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
#!/bin/bash
# Verify that the script is the only file in the directory
SCRIPT_DIR=$(dirname "$0")
FILE_COUNT=$(ls -1A | wc -l)
if [[ "$FILE_COUNT" -gt 1 ]]; then
echo "The script must be the only file in the directory. Quitting"
exit 1
fi
# Remove this script. Vite will complain if it is in the directory
rm "$0"
# Create a vite project with typescript and react
npm create vite@latest . -- --template react-swc-ts
# Install the default dependencies
npm install
# Install some more dev dependencies, for a better development experience
npm install -D \
@testing-library/react \
@testing-library/user-event \
@testing-library/jest-dom \
@typescript-eslint/eslint-plugin \
@typescript-eslint/parser \
@vitest/coverage-v8 \
eslint \
eslint-config-prettier \
eslint-plugin-prettier \
jsdom \
prettier \
sass \
vitest \
# XXX I would like the following files to simply be downloaded from git, but that requiers authentication. Adding tokens
# to the script will just be cumbersome, so the files are inlined here instead
# Setup ESLint
echo "module.exports = {
root: true,
env: {
es6: true,
jest: true,
node: true
},
extends: [
\"eslint:recommended\",
\"plugin:@typescript-eslint/recommended\",
\"plugin:@typescript-eslint/recommended-requiring-type-checking\",
\"prettier\",
\"plugin:prettier/recommended\"
],
parser: \"@typescript-eslint/parser\",
parserOptions: {
\"ecmaVersion\": 2020,
\"sourceType\": \"module\",
\"project\": [
\"./tsconfig.json\"
]
},
plugins: [
\"@typescript-eslint\",
\"prettier\",
\"react-hooks\"
],
rules: {
\"no-var\": \"error\",
\"prefer-const\": \"warn\",
\"eqeqeq\": \"error\",
\"class-methods-use-this\": \"warn\",
\"prettier/prettier\": \"error\",
\"no-eval\": \"error\",
\"no-multi-spaces\": \"error\",
\"react-hooks/rules-of-hooks\": \"error\",
\"react-hooks/exhaustive-deps\": \"warn\"
}
}" > .eslintrc.cjs
# Setup prettier
echo "{
\"printWidth\": 120,
\"tabWidth\": 4,
\"useTabs\": false,
\"trailingComma\": \"none\",
\"bracketSpacing\": true,
\"arrowParens\": \"avoid\",
\"endOfLine\": \"auto\"
}" > .prettierrc
# Setup the test environment
echo "import { cleanup } from \"@testing-library/react\";
import { expect, afterEach } from \"vitest\";
import matchers from \"@testing-library/jest-dom/matchers\";
// Add DOM matchers to \`expect\`
expect.extend(matchers);
// Clear the DOM after each test
afterEach(() => {
cleanup();
});
" > src/test-setup.ts
echo "/// <reference types=\"vitest\" />
/// <reference types=\"vite/client\" />
import { defineConfig } from \"vite\";
import react from \"@vitejs/plugin-react-swc\";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
test: {
environment: \"jsdom\",
setupFiles: [\"src/test-setup.ts\"]
}
})" > vite.config.ts
# Update package.json with some new scripts
node -e "
const fs = require(\"fs\");
const package = require(\"./package.json\");
package.scripts = {
...package.scripts,
\"test\": \"vitest --mode test\",
\"coverage\": \"vitest --coverage --mode test\",
\"lint\": \"eslint src --ext js,jsx,ts,tsx --report-unused-disable-directives --max-warnings 0\",
};
fs.writeFileSync(\"./package.json\", JSON.stringify(package, null, 2));
"
# Add stuff to .gitignore
echo "" >> .gitignore
echo "# Other" >> .gitignore
echo "coverage" >> .gitignore
# Fix lint errors. Vite sets up with different styles than we have defined
npx eslint --fix src