Skip to content

Commit 4b5fe03

Browse files
committed
使用 listr 来处理任务
1 parent baa727c commit 4b5fe03

File tree

3 files changed

+90
-44
lines changed

3 files changed

+90
-44
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
"cheerio": "^1.0.0-rc.3",
2020
"download": "^7.1.0",
2121
"got": "^9.6.0",
22+
"listr": "^0.14.3",
2223
"log-symbols": "^2.2.0",
2324
"lz-string": "^1.4.4",
2425
"meow": "^5.0.0",
25-
"ora": "^3.4.0",
2626
"ow": "^0.12.0"
2727
},
2828
"keywords": [

source/amanga.js

Lines changed: 87 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,99 @@
11
const download = require('download');
2-
const ora = require('ora');
2+
const Listr = require('listr');
3+
const {existsSync} = require('fs');
34

45
module.exports = async (input, flags) => {
56
const {type, outputDir} = flags;
6-
const parser = require(`./lib/${type}`);
7-
const spinner = ora(`Loading ${type}`).start();
8-
9-
const {title, images, options} = await parser(input, flags);
10-
11-
spinner.info(`Title: ${title}`);
12-
spinner.info(`Images: ${images.length}`);
13-
14-
// 开启info的话只输出信息
15-
if (!flags.info) {
16-
for (const image of images) {
17-
let url = image;
18-
const imgOptions = {};
19-
if (typeof image !== 'string') {
20-
url = image.url;
21-
if (image.name) {
22-
imgOptions.filename = image.name;
7+
8+
const tasks = new Listr([
9+
{
10+
title: '检查类型是否支持',
11+
task: () => {
12+
if (!existsSync(`${__dirname}/lib/${type}.js`)) {
13+
throw new Error(`${type} 类型不支持`);
2314
}
2415
}
25-
26-
try {
27-
spinner.start(`Downloading ${url}`);
28-
await download(
29-
encodeURI(url),
30-
outputDir || `amanga/${type}/${title}`,
31-
{
32-
// 10s超时
33-
timeout: 10000,
34-
...options,
35-
...imgOptions
36-
}
37-
).on('downloadProgress', progress => {
38-
spinner.text = `(${
39-
progress.transferred
40-
}) Downloading ${url}`;
41-
});
42-
spinner.succeed(`Done ${url}`);
43-
} catch (error) {
44-
spinner.fail(`${error.message} ${url}`);
16+
},
17+
{
18+
title: '获取数据',
19+
task: async ctx => {
20+
const parser = require(`./lib/${type}`);
21+
const data = await parser(input, flags);
22+
ctx.data = data;
4523
}
4624
}
25+
]);
26+
27+
if (flags.info) {
28+
tasks.add({
29+
title: '格式化数据',
30+
task: ctx => {
31+
const {title, images} = ctx.data;
32+
33+
const info = `
34+
标题: ${title}
35+
数量: ${images.length}
36+
图片链接:
37+
${
38+
typeof images[0] === 'string'
39+
? images.join('\n')
40+
: images.map(img => img.url).join('\n')
41+
}
42+
43+
`;
44+
45+
ctx.info = info;
46+
}
47+
});
4748
} else {
48-
console.log();
49-
console.dir({type, title, images});
50-
console.log();
49+
tasks.add({
50+
title: '下载图片',
51+
skip: () => flags.info,
52+
task: async (ctx, task) => {
53+
const {title, images, options} = ctx.data;
54+
55+
let count = 0;
56+
task.title = `下载图片 [${count}/${images.length}]`;
57+
58+
for (const image of images) {
59+
let url = image;
60+
const imgOptions = {};
61+
if (typeof image !== 'string') {
62+
url = image.url;
63+
if (image.name) {
64+
imgOptions.filename = image.name;
65+
}
66+
}
67+
68+
task.output = `下载 ${url}`;
69+
await download(
70+
encodeURI(url),
71+
outputDir || `amanga/${type}/${title}`,
72+
{
73+
// 10s
74+
timeout: 10000,
75+
...options,
76+
...imgOptions
77+
}
78+
).on(
79+
'downloadProgress',
80+
({transferred, total, percent}) => {
81+
task.output = `下载 ${url} [${transferred}/${total}]`;
82+
if (percent === 1) {
83+
task.title = `下载图片 [${(count += 1)}/${
84+
images.length
85+
}]`;
86+
}
87+
}
88+
);
89+
}
90+
}
91+
});
5192
}
5293

53-
spinner.info('All Done 🎉');
54-
spinner.stop();
94+
await tasks.run().then(ctx => {
95+
if (ctx.info) {
96+
console.log(ctx.info);
97+
}
98+
});
5599
};

source/cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ if (cli.input < 1 || !cli.flags.type) {
4646
// cli -> lib(parser) -> download -> done
4747
(async () => {
4848
await amanga(cli.input, cli.flags);
49+
50+
console.log(`\n${logSymbols.success} All Done 🎉`);
4951
})().catch(error => {
5052
console.error(`\n${logSymbols.error} ${error.message}`);
5153
process.exit(1);

0 commit comments

Comments
 (0)