Skip to content

Commit ad01c46

Browse files
committed
refactor
1 parent b0d19bf commit ad01c46

File tree

16 files changed

+126
-109
lines changed

16 files changed

+126
-109
lines changed

cli/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ $ esm.sh --help
3232
Usage: esm.sh [command] <options>
3333
3434
Commands:
35-
add, i [...packages] Add packages to the "importmap" script
36-
update Update packages in the "importmap" script
37-
tidy Tidy up the "importmap" script
38-
init Create a new web application
39-
serve, x Serve a web application
40-
dev Serve a web application in development mode
35+
add, i [...packages] Add specified packages to the "importmap" script in index.html
36+
update Update existing packages in the "importmap" script in index.html
37+
tidy Clean up and optimize the "importmap" script in index.html
38+
init Initialize a new web application
39+
serve Serve the web application in production mode
40+
dev Serve the web application in development mode with live reload
4141
4242
Options:
43-
--help Show help message
43+
--help Display this help message
4444
```

cli/add.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@ import (
1515

1616
const addHelpMessage = "\033[30mesm.sh - A nobuild tool for modern web development.\033[0m" + `
1717
18-
Usage: esm.sh add [...packages] <options>
18+
Usage: esm.sh add [...packages] [options]
1919
2020
Examples:
2121
esm.sh add react@19.0.0
2222
esm.sh add react@19 react-dom@19
2323
esm.sh add react react-dom @esm.sh/router
2424
25+
Arguments:
26+
[...packages] Packages to add, separated by space
27+
2528
Options:
26-
--help Show help message
29+
--help Show help message
2730
`
2831

2932
const htmlTemplate = `<!DOCTYPE html>

cli/init.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ var efs embed.FS
1919

2020
const initHelpMessage = "\033[30mesm.sh - A nobuild tool for modern web development.\033[0m" + `
2121
22-
Usage: esm.sh init <project-name> <options>
22+
Usage: esm.sh init [project-name] [options]
23+
24+
Arguments:
25+
[project-name] Name of the project, default is "esm-app"
2326
2427
Options:
2528
--framework JavaScript framework, Available options: Vanilla, React, Preact, Vue, Svelte

cli/serve.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import (
1414

1515
const serveHelpMessage = "\033[30mesm.sh - A nobuild tool for modern web development.\033[0m" + `
1616
17-
Usage: esm.sh serve <app-dir> <options>
17+
Usage: esm.sh serve [app-dir] [options]
18+
19+
Arguments:
20+
[app-dir] Directory to serve, default is current directory
1821
1922
Options:
2023
--port Port to serve on, default is 3000

cli/tidy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77

88
const tidyHelpMessage = "\033[30mesm.sh - A nobuild tool for modern web development.\033[0m" + `
99
10-
Usage: esm.sh tidy <options>
10+
Usage: esm.sh tidy [options]
1111
1212
Options:
13-
--help Show help message
13+
--help Show help message
1414
`
1515

1616
// Tidy tidies up "importmap" script

cli/update.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ import (
77

88
const updateHelpMessage = "\033[30mesm.sh - A nobuild tool for modern web development.\033[0m" + `
99
10-
Usage: esm.sh update [...packages] <options>
10+
Usage: esm.sh update [...packages] [options]
1111
1212
Examples:
13-
esm.sh update # update all packages in "importmap" script
13+
esm.sh update # update all packages in the "importmap" script
1414
esm.sh update react # update a specific package
1515
16+
Arguments:
17+
[...packages] Packages to update, separated by space
18+
1619
Options:
17-
--help Show help message
20+
--help Show help message
1821
`
1922

2023
// Update updates packages in "importmap" script

internal/fetch/fetch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ type FetchClient struct {
2121
}
2222

2323
// NewClient creates a new FetchClient.
24-
func NewClient(timeout int, userAgent string, reserveRedirect bool) (client *FetchClient, recycle func()) {
24+
func NewClient(userAgent string, timeout int, reserveRedirect bool) (client *FetchClient, recycle func()) {
2525
client = clientPool.Get().(*FetchClient)
26+
client.userAgent = userAgent
2627
client.Timeout = time.Duration(timeout) * time.Second
2728
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
2829
if reserveRedirect && len(via) > 0 {
@@ -33,7 +34,6 @@ func NewClient(timeout int, userAgent string, reserveRedirect bool) (client *Fet
3334
}
3435
return nil
3536
}
36-
client.userAgent = userAgent
3737
return client, func() { clientPool.Put(client) }
3838
}
3939

main.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ import (
77
"github.com/esm-dev/esm.sh/cli"
88
)
99

10-
const helpMessage = "\033[30mesm.sh - A nobuild tool for modern web development.\033[0m" + `
10+
const helpMessage = "\033[30mesm.sh - A no-build tool for modern web development.\033[0m" + `
1111
12-
Usage: esm.sh [command] <options>
12+
Usage: esm.sh [command] [options]
1313
1414
Commands:
15-
add, i [...packages] Add packages to the "importmap" script
16-
update Update packages in the "importmap" script
17-
tidy Tidy up the "importmap" script
18-
init Create a new web application
19-
serve, x Serve a web application
20-
dev Serve a web application in development mode
15+
add, i [...packages] Add specified packages to the "importmap" script in index.html
16+
update Update existing packages in the "importmap" script in index.html
17+
tidy Clean up and optimize the "importmap" script in index.html
18+
init Initialize a new web application
19+
serve Serve the web application in production mode
20+
dev Serve the web application in development mode with live reload
2121
2222
Options:
23-
--help Show help message
23+
--help Display this help message
2424
`
2525

2626
func main() {
@@ -37,7 +37,7 @@ func main() {
3737
cli.Tidy()
3838
case "init":
3939
cli.Init()
40-
case "serve", "x":
40+
case "serve":
4141
cli.Serve(false)
4242
case "dev":
4343
cli.Serve(true)

scripts/deploy-ci.sh

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,20 @@ echo " User ${DEPLOY_SSH_USER}" >> ~/.ssh/config
1111
echo " IdentityFile ~/.ssh/id_ed25519" >> ~/.ssh/config
1212
echo " IdentitiesOnly yes" >> ~/.ssh/config
1313

14+
src=$(dirname $0)/esmd.go
15+
if [ ! -f \$src ]; then
16+
echo "package main" >> $src
17+
echo "import \"github.com/esm-dev/esm.sh/server\"" >> $src
18+
echo "func main() { server.Serve() }" >> $src
19+
fi
20+
1421
echo "--- building server..."
15-
go build -ldflags="-s -w -X 'github.com/esm-dev/esm.sh/server.VERSION=${SERVER_VERSION}'" -o esmd $(dirname $0)/../server/esmd/main.go
22+
go build -ldflags="-s -w -X 'github.com/esm-dev/esm.sh/server.VERSION=${SERVER_VERSION}'" -o esmd $src
1623
if [ "$?" != "0" ]; then
24+
rm -f $src
1725
exit 1
1826
fi
27+
rm -f $src
1928
du -h esmd
2029

2130
echo "--- uploading server build..."
@@ -32,8 +41,8 @@ ssh esm.sh << EOF
3241
if [ "\$?" != "0" ]; then
3342
exit 1
3443
fi
35-
chmod +x esmd
3644
rm -f esmd.tar.gz
45+
chmod +x esmd
3746
3847
git version
3948
if [ "\$?" == "127" ]; then
@@ -44,46 +53,46 @@ ssh esm.sh << EOF
4453
ufw version
4554
if [ "\$?" == "0" ]; then
4655
ufw allow http
56+
ufw allow https
4757
fi
4858
49-
configfile=/etc/esmd/config.json
50-
servicefile=/etc/systemd/system/esmd.service
59+
configjson=/etc/esmd/config.json
60+
servicerc=/etc/systemd/system/esmd.service
5161
reload=no
52-
if [ ! -f \$servicefile ]; then
62+
63+
if [ ! -f \$servicerc ]; then
5364
addgroup esm
5465
adduser --ingroup esm --home /esm --disabled-login --disabled-password --gecos "" esm
55-
if [ "\$?" != "0" ]; then
56-
echo "Failed to add user 'esm'"
57-
exit 1
58-
fi
59-
mkdir /etc/esmd
60-
echo "[Unit]" >> \$servicefile
61-
echo "Description=esm.sh service" >> \$servicefile
62-
echo "After=network.target" >> \$servicefile
63-
echo "StartLimitIntervalSec=0" >> \$servicefile
64-
echo "[Service]" >> \$servicefile
65-
echo "Type=simple" >> \$servicefile
66-
echo "ExecStart=/usr/local/bin/esmd --config=\$configfile" >> \$servicefile
67-
echo "WorkingDirectory=/esm" >> \$servicefile
68-
echo "Group=esm" >> \$servicefile
69-
echo "User=esm" >> \$servicefile
70-
echo "AmbientCapabilities=CAP_NET_BIND_SERVICE" >> \$servicefile
71-
echo "Restart=always" >> \$servicefile
72-
echo "RestartSec=5" >> \$servicefile
73-
echo "Environment=\"ESMDIR=/esm\"" >> \$servicefile
74-
echo "[Install]" >> \$servicefile
75-
echo "WantedBy=multi-user.target" >> \$servicefile
66+
echo "[Unit]" >> \$servicerc
67+
echo "Description=esm.sh service" >> \$servicerc
68+
echo "After=network.target" >> \$servicerc
69+
echo "StartLimitIntervalSec=0" >> \$servicerc
70+
echo "[Service]" >> \$servicerc
71+
echo "Type=simple" >> \$servicerc
72+
echo "ExecStart=/usr/local/bin/esmd --config=\$configjson" >> \$servicerc
73+
echo "WorkingDirectory=/esm" >> \$servicerc
74+
echo "Group=esm" >> \$servicerc
75+
echo "User=esm" >> \$servicerc
76+
echo "AmbientCapabilities=CAP_NET_BIND_SERVICE" >> \$servicerc
77+
echo "Restart=always" >> \$servicerc
78+
echo "RestartSec=5" >> \$servicerc
79+
echo "Environment=\"ESMDIR=/esm\"" >> \$servicerc
80+
echo "[Install]" >> \$servicerc
81+
echo "WantedBy=multi-user.target" >> \$servicerc
7682
reload=yes
7783
else
7884
systemctl stop esmd.service
7985
echo "Stopped esmd.service."
8086
fi
8187
82-
rm -f \$configfile
88+
mv -f esmd /usr/local/bin/esmd
89+
90+
rm -f \$configjson
91+
mkdir -p /etc/esmd
8392
if [ "$SERVER_CONFIG" != "" ]; then
84-
echo '${SERVER_CONFIG}' >> \$configfile
93+
echo '${SERVER_CONFIG}' >> \$configjson
8594
else
86-
echo "{}" >> \$configfile
95+
echo "{}" >> \$configjson
8796
fi
8897
8998
if [ "$RESET_ON_DEPLOY" == "yes" ]; then
@@ -92,8 +101,6 @@ ssh esm.sh << EOF
92101
nohup rm -rf /tmp/.esm &
93102
fi
94103
95-
mv -f esmd /usr/local/bin/esmd
96-
97104
if [ "\$reload" == "yes" ]; then
98105
systemctl daemon-reload
99106
systemctl enable esmd.service

scripts/deploy.sh

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,23 @@ if [ "$v" != "" ]; then
4848
sshPort="$v"
4949
fi
5050

51+
src=$(dirname $0)/esmd.go
52+
if [ ! -f \$src ]; then
53+
echo "package main" >> $src
54+
echo "import \"github.com/esm-dev/esm.sh/server\"" >> $src
55+
echo "func main() { server.Serve() }" >> $src
56+
fi
57+
5158
echo "--- building(${goos}_$goarch)..."
5259
export GOOS=$goos
5360
export GOARCH=$goarch
54-
go build -ldflags="-s -w" -o esmd $(dirname $0)/../server/esmd/main.go
61+
go build -ldflags="-s -w" -o esmd $src
5562
if [ "$?" != "0" ]; then
63+
rm -f $src
5664
exit
5765
fi
66+
rm -f $src
67+
du -h esmd
5868

5969
echo "--- uploading..."
6070
tar -czf esmd.tar.gz esmd
@@ -72,54 +82,51 @@ ssh -p $sshPort ${user}@${host} << EOF
7282
if [ "\$?" != "0" ]; then
7383
exit 1
7484
fi
75-
chmod +x esmd
7685
rm -f esmd.tar.gz
86+
chmod +x esmd
7787
7888
git version
7989
if [ "\$?" == "127" ]; then
8090
apt-get update
8191
apt-get install -y git
8292
fi
8393
94+
ufw version
95+
if [ "\$?" == "0" ]; then
96+
ufw allow http
97+
ufw allow https
98+
fi
99+
100+
configjson=/etc/esmd/config.json
101+
servicerc=/etc/systemd/system/esmd.service
102+
84103
if [ "$init" == "yes" ]; then
85-
servicefile=/etc/systemd/system/esmd.service
86-
if [ -f \$servicefile ]; then
87-
rm -f servicefile
88-
fi
89104
addgroup esm
90105
adduser --ingroup esm --home=/esm --disabled-login --disabled-password --gecos "" esm
91-
if [ "\$?" != "0" ]; then
92-
echo "Failed to add user 'esm'"
93-
exit 1
94-
fi
95-
ufw version
96-
if [ "\$?" == "0" ]; then
97-
ufw allow http
98-
fi
99-
echo "[Unit]" >> \$servicefile
100-
echo "Description=esm.sh service" >> \$servicefile
101-
echo "After=network.target" >> \$servicefile
102-
echo "StartLimitIntervalSec=0" >> \$servicefile
103-
echo "[Service]" >> \$servicefile
104-
echo "Type=simple" >> \$servicefile
106+
rm -f \$servicerc
107+
echo "[Unit]" >> \$servicerc
108+
echo "Description=esm.sh service" >> \$servicerc
109+
echo "After=network.target" >> \$servicerc
110+
echo "StartLimitIntervalSec=0" >> \$servicerc
111+
echo "[Service]" >> \$servicerc
112+
echo "Type=simple" >> \$servicerc
105113
if [ "$config" != "" ]; then
106-
configfile=/etc/esmd/config.json
114+
rm -f \$configjson
107115
mkdir -p /etc/esmd
108-
rm -f \$configfile
109-
echo '$config' >> \$configfile
110-
echo "ExecStart=/usr/local/bin/esmd --config=\$configfile" >> \$servicefile
116+
echo '$config' >> \$configjson
117+
echo "ExecStart=/usr/local/bin/esmd --config=\$configjson" >> \$servicerc
111118
else
112-
echo "ExecStart=/usr/local/bin/esmd" >> \$servicefile
119+
echo "ExecStart=/usr/local/bin/esmd" >> \$servicerc
113120
fi
114-
echo "WorkingDirectory=/esm" >> \$servicefile
115-
echo "Group=esm" >> \$servicefile
116-
echo "User=esm" >> \$servicefile
117-
echo "AmbientCapabilities=CAP_NET_BIND_SERVICE" >> \$servicefile
118-
echo "Restart=always" >> \$servicefile
119-
echo "RestartSec=5" >> \$servicefile
120-
echo "Environment=\"ESMDIR=/esm\"" >> \$servicefile
121-
echo "[Install]" >> \$servicefile
122-
echo "WantedBy=multi-user.target" >> \$servicefile
121+
echo "WorkingDirectory=/esm" >> \$servicerc
122+
echo "Group=esm" >> \$servicerc
123+
echo "User=esm" >> \$servicerc
124+
echo "AmbientCapabilities=CAP_NET_BIND_SERVICE" >> \$servicerc
125+
echo "Restart=always" >> \$servicerc
126+
echo "RestartSec=5" >> \$servicerc
127+
echo "Environment=\"ESMDIR=/esm\"" >> \$servicerc
128+
echo "[Install]" >> \$servicerc
129+
echo "WantedBy=multi-user.target" >> \$servicerc
123130
else
124131
systemctl stop esmd.service
125132
echo "Stopped esmd.service."

server/esmd/main.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)