-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·59 lines (51 loc) · 1.68 KB
/
index.js
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
#!/usr/bin/env node
import { promisify } from 'node:util';
import stream from 'node:stream';
import ProgressBar from 'progress'
import fs from 'node:fs';
import got from 'got';
import chalk from 'chalk';
const log = console.log;
const downloadUrl = process.argv[2]
if (downloadUrl) {
const pointArr = downloadUrl.split('.')
let suffix = pointArr[pointArr.length - 1]
suffix = suffix.indexOf('?') !== -1 ? suffix.slice(0, suffix.indexOf('?')) : suffix
const pathArr = pointArr[pointArr.length - 2].split('/')
const filename = pathArr[pathArr.length - 1]
const pipeline = promisify(stream.pipeline);
const readStream = got.stream(downloadUrl)
if(!filename || !suffix) {
log(chalk.red('暂时只支持文件名结尾的url地址!'))
}
const onError = (err) => {
log(chalk.red('download failed'));
console.log(err)
}
readStream.on('response', async res => {
let file = filename + '.' + suffix
try {
await pipeline(
readStream,
fs.createWriteStream(file)
);
log(chalk.green('download success!!!'));
log(chalk.green('filename:' + file));
} catch (error) {
onError(error);
}
})
readStream.off('error', onError);
readStream.on('downloadProgress', async progress => {
let bar = new ProgressBar(' downloading [:bar] :percent', {
complete: '+',
incomplete: ' ',
width: 40,
total: progress.total,
renderThrottle: 16
});
bar.tick(progress.transferred);
})
} else {
log(chalk.red('请输入有效的文件地址!'))
}