Skip to content

Commit 5cfa159

Browse files
committed
feat: add download caddy progress tip
1 parent cc0b414 commit 5cfa159

File tree

4 files changed

+144
-10
lines changed

4 files changed

+144
-10
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
"http-proxy-agent": "^7.0.0",
123123
"https-proxy-agent": "^7.0.2",
124124
"kill-port": "^2.0.1",
125+
"ora": "5",
125126
"picocolors": "^1.0.0",
126127
"unplugin": "^1.4.0"
127128
},

pnpm-lock.yaml

Lines changed: 105 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/caddy/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as hostile from 'hostile'
99
import kill from 'kill-port'
1010
import { consola } from '../utils'
1111
import { caddyPath, supportList } from './constants'
12-
import { tryPort } from './utils'
12+
import { logProgress, logProgressOver, tryPort } from './utils'
1313

1414
export async function download() {
1515
if (await testCaddy())
@@ -47,7 +47,10 @@ export async function download() {
4747
http: httpAgent,
4848
https: httpsAgent,
4949
},
50+
}).on('downloadProgress', (progress) => {
51+
logProgress(progress.percent)
5052
}).pipe(file).on('finish', () => {
53+
logProgressOver()
5154
if (process.platform === 'win32')
5255
return resolve(caddyPath)
5356
// chmod +x

src/caddy/utils.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { createServer } from 'node:net'
2+
import ora from 'ora'
3+
4+
let spinner: ora.Ora
25

36
/**
47
* @returns whether the port is in use
@@ -23,3 +26,34 @@ export function tryPort(port: number) {
2326
}
2427
})
2528
}
29+
30+
function getNumber(number: number) {
31+
if (number <= 0)
32+
return '0%'
33+
else if (number < 100)
34+
return `${number.toFixed(2)}%`
35+
else
36+
return '100%'
37+
}
38+
39+
export function logProgress(percent: number | string) {
40+
if (!spinner) {
41+
spinner = ora({
42+
text: 'Start Download Caddy: 0%',
43+
indent: 2,
44+
})
45+
spinner.start()
46+
}
47+
else { spinner.text = `Download Caddy: ${getNumber(Number(percent) * 100)}` }
48+
}
49+
50+
export function logProgressOver() {
51+
if (!spinner) {
52+
spinner = ora({
53+
text: 'Start Download Caddy: 100%',
54+
indent: 2,
55+
})
56+
spinner.start()
57+
}
58+
else { spinner.succeed() }
59+
}

0 commit comments

Comments
 (0)