-
Notifications
You must be signed in to change notification settings - Fork 36
/
switch-mode.sh
executable file
·369 lines (302 loc) · 12.1 KB
/
switch-mode.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/bin/bash
# Function to switch to local development mode
switch_to_local() {
echo "Switching to local development mode..."
# Add workspaces entry to package.json
jq '.workspaces = ["prototypr-packages/*"]' package.json > temp.json && mv temp.json package.json
# Create an array to store package names
declare -a packages_to_remove
# Uninstall @prototypr packages
if [ -d "prototypr-packages" ]; then
for package_dir in prototypr-packages/*; do
if [ -d "$package_dir" ] && [ -f "$package_dir/package.json" ]; then
package_name=$(jq -r .name "$package_dir/package.json")
echo "Uninstalling $package_name..."
npm uninstall "$package_name"
packages_to_remove+=("$package_name")
fi
done
fi
# Rename prototypr-packages folder if it exists
if [ -d "prototypr-packagesx" ]; then
mv prototypr-packagesx prototypr-packages
fi
# Loop through each package in prototypr-packages and delete its node_modules
if [ -d "prototypr-packages" ]; then
for package_dir in prototypr-packages/*; do
if [ -d "$package_dir" ]; then
echo "Deleting node_modules in $package_dir"
rm -rf "$package_dir/node_modules"
fi
done
fi
# Remove all packages from node_modules using the array
for package in "${packages_to_remove[@]}"; do
package_path=$(echo "$package" | sed 's/@//')
rm -rf "node_modules/$package_path"
done
# Install dependencies
npm install
echo "Switched to local development mode. Workspace and @prototypr packages have been linked."
# Update jsconfig.json to remove paths for packages in prototypr-packages
if [ -d "prototypr-packages" ]; then
for package_dir in prototypr-packages/*; do
if [ -d "$package_dir" ]; then
package_name=$(basename "$package_dir")
jq "del(.compilerOptions.paths[\"$package_name\"])" jsconfig.json > temp.json && mv temp.json jsconfig.json
fi
done
fi
# Update jsconfig.json
# for package_dir in prototypr-packages/*; do
# if [ -d "$package_dir/src" ]; then
# package_name=$(basename "$package_dir")
# jq --arg name "$package_name" --arg path "./prototypr-packages/$package_name/src" \
# '.compilerOptions.paths[$name] = [$path]' jsconfig.json > temp.json && mv temp.json jsconfig.json
# fi
# done
write_package_names
echo "Reverted next.config.mjs to original webpack configuration."
}
# Function to switch to src development mode
switch_to_src() {
echo "Switching to src development mode..."
# Call switch_to_local function
switch_to_local
# Update jsconfig.json
for package_dir in prototypr-packages/*; do
if [ -d "$package_dir/src" ]; then
package_name=$(basename "$package_dir")
jq --arg name "$package_name" --arg path "./prototypr-packages/$package_name/src" \
'.compilerOptions.paths[$name] = [$path]' jsconfig.json > temp.json && mv temp.json jsconfig.json
fi
done
write_package_names
echo "Switched to src development mode. You can now use 'npm run dev:src' to run in src mode."
}
# Function to switch to npm packages mode
switch_to_npm() {
echo "Switching to npm packages mode..."
# Source nvm
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
# Now use nvm
nvm use 18.17.0 || { echo "Failed to switch to Node.js 18.17.0. Please install it using 'nvm install 18.17.0'"; exit 1; }
# Load GitHub Personal Access Token from .env.local
if [ -f .env.local ]; then
export $(grep -v '^#' .env.local | xargs)
fi
# Ensure the token is set
if [ -z "$GITHUB_PERSONAL_ACCESS_TOKEN" ]; then
echo "Error: GITHUB_PERSONAL_ACCESS_TOKEN is not set in .env.local"
exit 1
fi
# Remove workspaces entry from package.json
jq 'del(.workspaces)' package.json > temp.json && mv temp.json package.json
# Create an array to store package names
declare -a packages_to_remove
# Uninstall @prototypr packages
if [ -d "prototypr-packages" ]; then
for package_dir in prototypr-packages/*; do
if [ -d "$package_dir" ] && [ -f "$package_dir/package.json" ]; then
package_name=$(jq -r .name "$package_dir/package.json")
echo "Uninstalling $package_name..."
npm uninstall "$package_name"
packages_to_remove+=("$package_name")
fi
done
fi
# Rename prototypr-packages folder if it exists
if [ -d "prototypr-packages" ]; then
mv prototypr-packages prototypr-packagesx
echo "Renamed prototypr-packages to prototypr-packagesx"
while [ -d "prototypr-packages" ]; do
sleep 1
done
echo "Renaming operation completed"
fi
# Remove all packages from node_modules using the array
for package in "${packages_to_remove[@]}"; do
package_path=$(echo "$package" | sed 's/@//')
rm -rf "node_modules/$package_path"
done
npm install
# Install @prototypr packages
if [ -d "prototypr-packages" ]; then
for package_dir in prototypr-packages/*; do
if [ -d "$package_dir" ] && [ -f "$package_dir/package.json" ]; then
package_name=$(jq -r .name "$package_dir/package.json")
private_package=$(jq -r .prototypr_private "$package_dir/package.json")
package_version=$(jq -r .version "$package_dir/package.json")
echo "Installing $package_name (version $package_version)"
if [ "$private_package" = "true" ]; then
npm install "$package_name@$package_version" --save-optional
else
npm install "$package_name@$package_version" --save
fi
fi
done
elif [ -d "prototypr-packagesx" ]; then
for package_dir in prototypr-packagesx/*; do
if [ -d "$package_dir" ] && [ -f "$package_dir/package.json" ]; then
package_name=$(jq -r .name "$package_dir/package.json")
private_package=$(jq -r .prototypr_private "$package_dir/package.json")
package_version=$(jq -r .version "$package_dir/package.json")
echo "Installing $package_name (version $package_version)"
if [ "$private_package" = "true" ]; then
npm install "$package_name@$package_version" --save-optional
else
npm install "$package_name@$package_version" --save
fi
fi
done
else
echo "prototypr-packages directory not found. Skipping package installation."
fi
# Update jsconfig.json
for package_dir in prototypr-packages/*; do
if [ -d "$package_dir/src" ]; then
package_name=$(basename "$package_dir")
jq --arg name "$package_name" --arg path "./prototypr-packages/$package_name/src" \
'.compilerOptions.paths[$name] = [$path]' jsconfig.json > temp.json && mv temp.json jsconfig.json
fi
done
echo "Switched to npm packages mode."
}
# Function to watch a package development
watch_package() {
echo "Watching package..."
# Check if package name is provided
if [ -z "$1" ]; then
echo "Error: Package name is required."
echo "Usage: $0 watch <package-name>"
exit 1
fi
package_name="$1"
package_dir="prototypr-packages/$package_name"
# Check if the package directory exists
if [ ! -d "$package_dir" ]; then
echo "Error: Package '$package_name' not found in prototypr-packages folder."
exit 1
fi
# Check if package.json exists in the package directory
if [ ! -f "$package_dir/package.json" ]; then
echo "Error: package.json not found in '$package_dir'."
exit 1
fi
echo "Watching package: $package_name"
# Here you can add the actual watch command
# For example, if using npm, you might do:
# npm run --prefix "$package_dir" watch
# Or if using a specific build tool, adjust accordingly
# For now, we'll just echo a placeholder message
echo "Navigating to watch $package_name"
# Navigate to the package directory
cd "$package_dir" || exit 1
# Run npm install
echo "Installing dependencies for $package_name..."
npm install
# Run npm watch
echo "Starting watch mode for $package_name..."
npm run watch
}
# Function to deploy a new package
deploy_package() {
echo "Deploying package..."
# Check if package name is provided
if [ -z "$1" ]; then
echo "Error: Package name is required."
echo "Usage: $0 deploy <package-name>"
exit 1
fi
package_name="$1"
package_dir="prototypr-packages/$package_name"
# Check if the package directory exists
if [ ! -d "$package_dir" ]; then
echo "Error: Package '$package_name' not found in prototypr-packages folder."
exit 1
fi
# Check if package.json exists in the package directory
if [ ! -f "$package_dir/package.json" ]; then
echo "Error: package.json not found in '$package_dir'."
exit 1
fi
echo "Deploying package: $package_name"
# Navigate to the package directory
cd "$package_dir" || exit 1
# Increase version number by 0.01
npm version patch
npm install
# Run npm build
npm run build
# Git operations
git add -A
git commit -m "new version"
git push origin main
# Publish if package name doesn't include @prototypr
if [[ "$package_name" != *"@prototypr"* ]]; then
npm publish
fi
# Return to project root
cd ../../
# Update package.json in project root
# new_version=$(node -p "require('./prototypr-packages/$package_name/package.json').version")
# sed -i '' "s/\"$package_name\": \".*\"/\"$package_name\": \"^$new_version\"/" package.json
echo "Deployment complete. Package version updated to $new_version. Make sure to update it in the main project package.json."
}
# New function to write package names to a JavaScript file
write_package_names() {
echo "Writing package names to prototypr_package_names.js..."
# Create the next-config directory if it doesn't exist
mkdir -p next-config
# Start the JavaScript array
echo "module.exports = {" > next-config/prototypr_package_names.js
echo " prototyprPackageNames: [" >> next-config/prototypr_package_names.js
# Add package names to the array
if [ -d "prototypr-packages" ]; then
for package_dir in prototypr-packages/*; do
if [ -d "$package_dir" ] && [ -f "$package_dir/package.json" ]; then
package_name=$(jq -r .name "$package_dir/package.json")
echo " '$package_name'," >> next-config/prototypr_package_names.js
fi
done
fi
# Close the JavaScript array and module.exports
echo " ]" >> next-config/prototypr_package_names.js
echo "};" >> next-config/prototypr_package_names.js
echo "Package names written to next-config/prototypr_package_names.js"
}
# Main script logic
case "$1" in
local)
switch_to_local
;;
npm)
switch_to_npm
;;
src)
switch_to_src
;;
deploy)
if [ -z "$2" ]; then
echo "Error: Package name is required for deployment."
echo "Usage: $0 deploy <package-name>"
exit 1
fi
deploy_package "$2"
;;
watch)
if [ -z "$2" ]; then
echo "Error: Package name is required for watch mode."
echo "Usage: $0 watch <package-name>"
exit 1
fi
watch_package "$2"
;;
*)
echo "Usage: $0 {local|npm|deploy|watch (<package-name>)}"
exit 1
;;
esac
exit 0