-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·83 lines (70 loc) · 2.18 KB
/
cli.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
82
83
#!/usr/bin/env node
'use strict';
var program = require('commander');
var subfil = require('./');
var pkg = require('./package');
var allLanguages = require('./lib/data/languages');
var updateNotifier = require('update-notifier');
var chalk = require('chalk');
var path = require('path');
var Spinner = require('cli-spinner').Spinner;
updateNotifier({pkg: pkg}).notify();
program
.version(pkg.version)
.usage('subfil <path> [<path> ...]')
.option('-r, --recursive', 'download subtitles for files recursively')
.option('-l, --language <language>', 'download subtitles in given language')
.option('-d, --destination <destination>', 'download to custom destination')
.parse(process.argv);
if (program.args.length === 0) {
console.error('You need to specify at least one path');
process.exit(1);
}
if (typeof program.language === 'string') {
if (allLanguages.indexOf(program.language.toLowerCase()) === -1) {
console.log('Invalid language provided');
console.log('Please choose a language from the following:');
console.log(allLanguages.join(', ').join('\n'));
process.exit(1);
}
}
var spinner = new Spinner(chalk.blue('Downloading subtitles(s) %s'));
spinner.setSpinnerString('|/-\\');
spinner.start();
if (program.args.length === 1) {
program.args = program.args[0];
}
subfil.download(program.args, program, function (err, status, dests, files) {
if (err) {
throw err;
}
spinner.stop();
console.log('');
if (typeof status === 'string') {
if (status === 'OK') {
console.log(chalk.green('Downloaded a total of 1 subtitle(s)'));
} else {
console.log(path.basename(program.args[0]) + ' : ' + chalk.red(status));
}
return;
}
var i, count = 0;
status.forEach(function (stat) {
if (stat === 'OK') {
count += 1;
}
});
var green = chalk.green;
var red = chalk.red;
if (count != 0) {
console.log(green('Downloaded a total of ' + count + ' subtitle(s)'));
}
if (count !== status.length) {
console.log('Couldn\'t download subtitles for the following files:');
for (i in status) {
if (status[i] !== 'OK') {
console.log(path.basename(files[i]) + ' : ' + red(status[i]));
}
}
}
});