-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·171 lines (145 loc) · 4.44 KB
/
setup.sh
File metadata and controls
executable file
·171 lines (145 loc) · 4.44 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
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
#!/usr/bin/env bash
set -euo pipefail
# -----------------------------
# 1. Имя проекта
# -----------------------------
APP_NAME="${1:-my-app}"
echo "▶ Creating Vite React app: $APP_NAME"
# Создаём проект Vite + React
# В некоторых версиях create-vite задаёт вопросы.
# Важно: если спросит "Install and start now?" — отвечай NO.
npm create vite@latest "$APP_NAME" -- --template react
cd "$APP_NAME"
# -----------------------------
# 2. Чистим public
# -----------------------------
echo "▶ Cleaning up public folder"
rm -rf public/* || true
# -----------------------------
# 3. Чистим index.html
# -----------------------------
echo "▶ Cleaning up index.html"
sed -i.bak "/vite.svg/d" index.html
sed -i.bak "s|<title>.*</title>|<title>$APP_NAME</title>|" index.html
rm -f index.html.bak
# -----------------------------
# 4. Ставим зависимости
# -----------------------------
echo "▶ Installing dependencies"
npm i
npm i -D json-server concurrently
# -----------------------------
# 5. Находим свободный порт для json-server
# -----------------------------
API_PORT=3001
while lsof -i :$API_PORT >/dev/null 2>&1; do
API_PORT=$((API_PORT+1))
done
echo "▶ json-server will use port $API_PORT"
# -----------------------------
# 6. Создаём db.json
# -----------------------------
echo "▶ Creating db.json (mock data)"
cat > db.json <<'JSON'
{
"todos": [
{ "id": 1, "title": "Learn Vite", "done": false },
{ "id": 2, "title": "Build React app", "done": true }
]
}
JSON
# -----------------------------
# 7. vite.config.js с прокси на /api
# -----------------------------
echo "▶ Writing vite.config.js"
cat > vite.config.js <<JS
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
server: {
proxy: {
'/api': {
target: 'http://localhost:$API_PORT',
changeOrigin: true,
rewrite: path => path.replace(/^\\/api/, '')
}
}
}
})
JS
# -----------------------------
# 8. Простенький API-клиент
# -----------------------------
echo "▶ Adding example API client and demo component"
mkdir -p src/services
cat > src/services/api.js <<'JS'
export async function getTodos() {
const res = await fetch('/api/todos')
if (!res.ok) throw new Error('Failed to load todos')
return res.json()
}
JS
cat > src/App.jsx <<'JSX'
import { useEffect, useState } from 'react'
import './App.css'
import { getTodos } from './services/api'
export default function App() {
const [todos, setTodos] = useState([])
const [error, setError] = useState(null)
const [loading, setLoading] = useState(true)
useEffect(() => {
getTodos()
.then(setTodos)
.catch((e) => setError(e.message))
.finally(() => setLoading(false))
}, [])
return (
<div style={{ maxWidth: 640, margin: '40px auto', fontFamily: 'system-ui' }}>
<h1>Vite + React + json-server</h1>
{loading && <p>Loading…</p>}
{error && <p style={{ color: 'crimson' }}>{error}</p>}
<ul>
{todos.map(t => (
<li key={t.id}>
<input type="checkbox" checked={t.done} readOnly /> {t.title}
</li>
))}
</ul>
<p style={{opacity:.7}}>
API: <code>/api/todos</code> → proxied to <code>http://localhost:$API_PORT/todos</code>
</p>
</div>
)
}
JSX
# -----------------------------
# 9. Обновляем scripts в package.json
# -----------------------------
echo "▶ Updating package.json scripts"
node <<NODE
const fs = require('fs');
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
pkg.scripts = {
...pkg.scripts,
"dev": "vite --open",
"build": "vite build",
"preview": "vite preview",
"server": "json-server --watch db.json --port $API_PORT",
"dev:all": "concurrently -n web,api -c auto \"npm run dev\" \"npm run server\""
};
fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2));
console.log('New scripts:', pkg.scripts);
NODE
# -----------------------------
# 10. Финальное сообщение
# -----------------------------
cat <<TXT
✅ Setup complete for project: $APP_NAME
Запуск всего стека:
cd $APP_NAME
npm run dev:all
Или отдельно:
npm run dev # только фронт
npm run server # только json-server (API на http://localhost:$API_PORT)
TXT