-
Notifications
You must be signed in to change notification settings - Fork 0
/
testListr.js
75 lines (72 loc) · 2.1 KB
/
testListr.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* @Description:
* @Author: linchaoting
* @Date: 2020-08-01 10:07:33
* @LastEditTime: 2021-02-03 16:26:33
*/
const path = require('path')
const Listr = require('listr');
const download = require('./lib/download')
const upload = require('./lib/upload')
const { fileDisplay } = require('./lib/file')
const outputPath = path.resolve(__dirname, './impressed/name.jpg')
const getUrl = 'https://tinypng.com/web/output/qtyyqbrdw020xka7f3k8y5u3eyc0z1ga'
const mockDownload = (time) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, time);
})
}
const compressTask = (imgList) => {
const uploadTaskParams = []
imgList.forEach(fileInfo => {
uploadTaskParams.push({
title: fileInfo.basename,
task: async (ctx, task) => {
const basicTitle = task.title
task.title = `upload ${basicTitle}`
const res = await upload(fileInfo.path)
const resObj = JSON.parse(res)
fileInfo.compressInfo=resObj.output
ctx.uploadResList.push(res)
if (resObj.output.ratio > 0.9) {
task.skip(`unnecessary compress ${resObj.output.ratio}`)
task.title = `unnecessary compress ${basicTitle}`
} else {
task.title = `download ${basicTitle}`
// await mockDownload(Math.random() * 5000 + 3000)
download(resObj.output.url,fileInfo.path)
task.title = `success ${basicTitle}`
}
}
})
})
return {
title: 'compress',
task: () => new Listr(uploadTaskParams, { concurrent: true })
}
}
const tinyTask = new Listr([
{
title: 'found 0 files',
task: (ctx, task) => {
const imgPathList = fileDisplay('./', fileInfo => {
return fileInfo.extname ==='.png'
})
ctx.imgPathList = imgPathList
ctx.uploadResList = []
task.title = `found ${imgPathList.length} files`
tinyTask.add(compressTask(imgPathList))
}
},
// {
// title: 'compress',
// task: () => new Listr(uploadTaskParams, { concurrent: true })
// },
]);
tinyTask.run().then(res => {
console.log(res)
}).catch(err => {
console.log('捕获错误')
})