-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (66 loc) · 2.74 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env node
const { Worker: JestWorker } = require('jest-worker');
const { range } = require('lodash')
const prompt = require('./cli')
const { Client, AllScheduler, ArchiveScheduler } = require('./src')
const Bluebird = require('bluebird')
Bluebird.config({ longStackTraces: true })
global.Promise = Bluebird;
const ora = require("ora");
const logger = ora();
//const concurrency = Math.min(8, cpus)//8
(async () => {
let workers;
try {
let options = await prompt();
// console.log('options', options);
//prepare workers
workers = new JestWorker(require.resolve('./src/worker'), { numWorkers: options.concurrency });
const client = new Client()
// for worker
const initMsg = ora(`preparing workers with concurrency: ${options.concurrency}..`).start()
await Promise.all(
range(options.concurrency)
.map(_ => workers.init({
saved : client.save(),
baseDir : options.downDir, //path.resolve(process.cwd(), 'videos/')
//overwrite: true
}))
)
initMsg.succeed('wake up workers.')
if (options?.courses.length === 0) {
throw new Error('No courses found!!!')
}
//videos download
if (options.videos) {
const lessonMsg = ora('start videos download..').start()
const scheduler = new AllScheduler(workers, { perPage: 1,...options })
const interval = setInterval(async _ => {
const stats = scheduler.stats
lessonMsg.text = `videos downloading.. [${stats.completed}/${stats.totals}] -`// Course: ${stats?.video.join(', ')} Concurrency ${stats.page}
}, 250)
const count = await scheduler.run()
clearInterval(interval)
lessonMsg.succeed(`complete lesson download.. (${count})`)
}
// code and archive download
if (options.code || options.zip) {
const codeMsg = ora('start archive zip download..').start()
const scheduler = new ArchiveScheduler(workers, { perPage: 1, ...options })
const interval = setInterval(_ => {
const stats = scheduler.stats
codeMsg.text = `code and archive downloading.. [${stats.completed}/${stats.totals}]`
}, 250)
const count = await scheduler.run()
clearInterval(interval)
codeMsg.succeed(`complete zip download.. (${count})`)
}
logger.succeed('All done!! good bye~')
} catch (err) {
console.error('MAIN ERROR:', err);
logger.fail('unknown error occurred! T-T')
process.exit(1)
} finally {
await workers.end()
}
})()