Skip to content

Commit 4d41902

Browse files
committed
feat: ✨ improve fwd/dep scripts
1 parent 6b5205c commit 4d41902

File tree

8 files changed

+120
-56
lines changed

8 files changed

+120
-56
lines changed

src/larasets/_art.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#!/bin/sh
22
set -e
33

4-
workspace=${containerWorkspaceFolder:-${CODESPACE_VSCODE_FOLDER:-.}}
4+
#### Goto repository root
5+
cd "$(git rev-parse --show-toplevel)" >/dev/null
6+
7+
#### Execute artisan command
58
if [ -n "$LARAVEL_SAIL" ] && [ "$LARAVEL_SAIL" -eq 1 ] && sail ps --status running | grep --after-context=1 -q -; then
69
sail artisan "$@"
710
else

src/larasets/_dep.sh

Whitespace-only changes.

src/larasets/_fwd.sh

Lines changed: 76 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,70 @@
11
#!/bin/sh
2+
23
set -e
34

4-
### Help
5-
if [ "x$1" = "xhelp" ] || [ "x$1" = "x-h" ] || [ "x$1" = "x--help" ] || [ -z "$1" ]; then
6-
echo "Usage: $0 [remote|local]"
7-
echo " remote: set APP_URL and VITE_HOST to use the codespace url"
8-
echo " local: remove APP_URL and VITE_HOST to use localhost"
9-
exit 0
10-
fi >&2
11-
12-
### Check if the environment variables are set
13-
if [ -z "$GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN" ] || [ -z "$CODESPACE_NAME" ]; then
14-
echo "GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN and CODESPACE_NAME must be set, force to local"
15-
$1=local
16-
fi >&2
17-
18-
### Function to update or replace export entry in .bashrc
5+
# Source colors script
6+
. zz_colors
7+
8+
# Function to print help and manage arguments
9+
eval $(
10+
zz_args "Configure port forwarding" $0 "$@" <<-help
11+
d domain domain Port formading domain
12+
c codespace codespace Codespace name
13+
p - prefix Port to prefix
14+
s - suffix Port to suffix
15+
- preset preset Port forwarding preset (github/daytona/local)
16+
help
17+
)
18+
19+
#### Goto repository root
20+
cd "$(git rev-parse --show-toplevel)" >/dev/null
21+
22+
#### Load preset values
23+
case "$preset" in
24+
github)
25+
domain="$GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN"
26+
codespace="$CODESPACE_NAME"
27+
suffix="-suffix"
28+
mode="remote"
29+
;;
30+
daytona)
31+
domain="$DAYTONA_WS_DOMAIN"
32+
codespace="$DAYTONA_WS_ID"
33+
prefix="-prefix"
34+
mode="remote"
35+
;;
36+
local)
37+
domain=""
38+
codespace="localhost"
39+
prefix=""
40+
suffix=""
41+
mode="local"
42+
;;
43+
*)
44+
if [ -z "$domain" ]; then
45+
zz_log e "Domain is required."
46+
exit 1
47+
fi
48+
if [ -z "$codespace" ]; then
49+
zz_log e "Codespace is required."
50+
exit 1
51+
fi
52+
if [ -z "$prefix" -o -z "$suffix" ]; then
53+
zz_log e "One of prefix or suffix is required."
54+
exit 1
55+
fi
56+
mode="remote"
57+
;;
58+
esac
59+
60+
#### Function to update or replace export entry in .bashrc
1961
setexport() {
2062
local key="$1"
2163
local value="$2"
2264

65+
# Check if the key is provided
66+
touch ./.env
67+
2368
### In .bashrc
2469
local bashrc="$HOME/.bashrc"
2570
if grep -q "^export $key=" "$bashrc"; then
@@ -41,21 +86,19 @@ setexport() {
4186
fi
4287
}
4388

44-
### Define urls if using a web editor with http port redirection
45-
if [ "x$1" = "xremote" ]; then
46-
47-
### update or add APP_URL
48-
setexport APP_URL "https://$CODESPACE_NAME-${APP_PORT:-80}.$GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN"
49-
50-
### update of add VITE_HOST
51-
setexport VITE_HOST "$CODESPACE_NAME-${VITE_PORT:-5173}.$GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN"
52-
fi
53-
54-
### Define urls if using a local editor with localhost port redirection
55-
if [ "x$1" = "xlocal" ] || [ -z "$1" ]; then
56-
### remove APP_URL
57-
setexport APP_URL
58-
59-
### remove VITE_HOST
60-
setexport VITE_HOST
61-
fi
89+
case "$mode" in
90+
remote)
91+
# Set the APP_URL and VITE_HOST for remote mode
92+
setexport APP_URL "https://${prefix:+${APP_PORT:-80}-}$codespace${suffix:+-${APP_PORT:-80}}${domain:+.$domain}"
93+
setexport VITE_HOST "${prefix:+${VITE_PORT:-5173}-}$codespace${suffix:+-${VITE_PORT:-5173}}${domain:+.$domain}"
94+
;;
95+
local)
96+
# Set the APP_URL and VITE_HOST for local mode
97+
setexport APP_URL "http://$codespace${domain:+.$domain}:${APP_PORT:-80}"
98+
setexport VITE_HOST "$codespace${domain:+.$domain}:${VITE_PORT:-5173}"
99+
;;
100+
*)
101+
zz_log e "Invalid mode. Use 'remote' or 'local'."
102+
exit 1
103+
;;
104+
esac

src/larasets/_init.sh

100644100755
Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,59 @@
11
#!/bin/sh
22
set -e
33

4-
workspace=${containerWorkspaceFolder:-${CODESPACE_VSCODE_FOLDER:-.}}
4+
#### Goto repository root
5+
cd "$(git rev-parse --show-toplevel)" >/dev/null
56

67
### Install composer dependencies if composer.json exists
7-
if [ -f $workspace/composer.json ]; then
8+
if [ -f "./composer.json" ]; then
89
zz_log i "Install composer dependencies"
910
composer install --ansi --ignore-platform-reqs --no-interaction --no-progress
1011
fi
1112

1213
### Install npm dependencies if package.json exists
13-
if [ -f $workspace/package.json ]; then
14+
if [ -f "./package.json" ]; then
1415
zz_log i "Install npm dependencies"
1516
npm install --ws --if-present --include-workspace-root || npm install
1617
fi
1718

18-
### Init db if sqlite and not exists
19-
if [ -n "$DB_CONNECTION" ] && [ "$DB_CONNECTION" = "sqlite" ]; then
19+
### Default DB_CONNECTION
20+
if [ -z "$DB_CONNECTION" ]; then
21+
export DB_CONNECTION=sqlite
22+
fi
2023

24+
### Init db if sqlite and not exists
25+
case $DB_CONNECTION in
26+
sqlite)
27+
zz_log i "DB_CONNECTION is {Purple $DB_CONNECTION}"
2128
### Set default sqlite db
2229
if [ -z "$DB_DATABASE" ]; then
2330
export DB_DATABASE=database/database.sqlite
2431
fi
2532

2633
zz_log i "Ensure sqlite db {Purple $DB_DATABASE} exist"
27-
touch $workspace/$DB_DATABASE
28-
fi
34+
touch ./$DB_DATABASE
35+
;;
36+
*)
37+
zz_log w "DB_CONNECTION {Purple $DB_CONNECTION} is not supported yet"
38+
exit 1
39+
;;
40+
esac
2941

3042
### Init env
3143
zz_log i "Init {B dotenv}"
32-
touch $workspace/.env
44+
touch ./.env
3345

3446
### Add APP_KEY to .env if it does not exist, in one line
35-
grep -q "APP_KEY" $workspace/.env || echo APP_KEY= >>$workspace/.env
47+
grep -q "APP_KEY"./.env || echo APP_KEY= >>./.env
3648

3749
### Daytona support (codeanywhere)
38-
if [ -n "DAYTONA_WS_ID" ]; then
39-
echo "Running in Daytona workspace"
40-
41-
# Define if not defined or replace APP_URL in env file
42-
export APP_URL="https://${APP_PORT:-80}-${DAYTONA_WS_ID}.${DAYTONA_WS_DOMAIN}"
43-
44-
# Replace APP_URL in .env file if it exists
45-
grep -q "APP_URL" $workspace/.env || echo "APP_URL=${APP_URL}" >>$workspace/.env && sed -i "s|^APP_URL=.*|APP_URL=${APP_URL}|" $workspace/.env
50+
if [ -n "$DAYTONA_WS_ID" ]; then
51+
zz_log i "Daytona/Codeanywhere support"
52+
fwd daytona
53+
elif [ -n "$CODESPACE_NAME" ]; then
54+
zz_log i "Github codespace support"
55+
fwd github
56+
else
57+
zz_log i "No fwd support"
58+
fwd local
4659
fi

src/larasets/_run.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/bin/sh
22
set -e
33

4-
workspace=${containerWorkspaceFolder:-${CODESPACE_VSCODE_FOLDER:-.}}
54
if [ -n "$LARAVEL_SAIL" ] && [ "$LARAVEL_SAIL" -eq 1 ] && sail ps --status running | grep --after-context=1 -q -; then
65
sail npm run "$@"
76
else

src/larasets/_sail.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/sh
22
set -e
33

4-
workspace=${containerWorkspaceFolder:-${CODESPACE_VSCODE_FOLDER:-.}}
5-
sh $([ -f "sail" ] && echo sail || [ -f "$workspace/vendor/bin/sail" ] && echo $workspace/vendor/bin/sail) "$@"
4+
#### Goto repository root
5+
cd "$(git rev-parse --show-toplevel)" >/dev/null
6+
7+
#### Execute command
8+
sh $([ -f "sail" ] && echo sail || [ -f "./vendor/bin/sail" ] && echo ./vendor/bin/sail) "$@"

src/larasets/_srv.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#!/bin/sh
22
set -e
33

4-
workspace=${containerWorkspaceFolder:-${CODESPACE_VSCODE_FOLDER:-.}}
4+
#### Goto repository root
5+
cd "$(git rev-parse --show-toplevel)" >/dev/null
6+
7+
#### Execute command
58
if [ -n "$LARAVEL_SAIL" ] && [ "$LARAVEL_SAIL" -eq 1 ] && sail ps --status running | grep --after-context=1 -q -; then
69
server='sail npx --yes pm2'
710
else

src/larasets/devcontainer-feature.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "larasets",
33
"name": "Laravel settings",
44
"description": "A feature to add useful laravel settings to your shell.",
5-
"version": "5.0.1",
5+
"version": "5.1.0",
66
"dependsOn": {
77
"ghcr.io/devcontainers/features/docker-in-docker": "",
88
"ghcr.io/devcontainers/features/node:1": "lts",

0 commit comments

Comments
 (0)