-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
109 lines (95 loc) · 3.14 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict';
var libfs = require('fs'),
glob = require("glob"),
nopt = require("nopt"),
libpath = require('path'),
log = require('./lib/log'),
parser = require('subtitles-parser'),
iconvlite = require('iconv-lite');
function generator(data) {
var i,
lines = [],
id = 0;
for (i in data) {
id += 1;
lines.push(id);
lines.push(data[i].startTime + ' --> ' + data[i].endTime);
lines.push(data[i].text || '');
lines.push('');
}
return lines.join('\n');
}
function main(argv, cwd, cb) {
var output, original, data, files, i,
options = nopt({
"pattern": [String, null],
"encoding": [String, null],
"backup": [Boolean, null],
"from-backup": [Boolean, null]
});
options.encoding = options.encoding || 'latin1';
options.backup = options.backup !== false ? true : false;
options.pattern = options.pattern || "**/*.srt";
log.info('Encoding in [' + options.encoding + ']');
log.info('Folder [' + cwd + ']');
log.info('Looking for [' + options.pattern + ']');
// options is optional
files = glob.sync(options.pattern);
if (files.length === 0) {
log.info('No file found: ' + cwd);
return cb();
}
for (i = 0; i < files.length; i += 1) {
log.info('\nFile [' + files[i] + ']');
try {
original = iconvlite.decode(libfs.readFileSync(libpath.join(cwd, files[i] + (options["from-backup"] ? '.bak' : ''))), options.encoding);
} catch (e) {
log.error(' -> Reading error: ' + (e.stack || e));
continue;
}
try {
data = parser.fromSrt(original);
} catch (e) {
log.error(' -> Parser error: ' + (e.stack || e));
continue;
}
try {
output = generator(data);
} catch (e) {
log.error(' -> Generator error: ' + (e.stack || e));
continue;
}
if (output === original) {
log.info(' -> Valid SRT format, skipping.');
continue;
}
if (output.indexOf('�') > -1) {
log.error(' -> Invalid encoding, set a valid encoding value `--enconding <value>`.');
continue;
}
if (options.backup && !options['from-backup']) {
if (!libfs.existsSync(libpath.join(cwd, files[i] + '.bak'))) {
log.info(' -> Backing up file (*.bak)');
try {
libfs.writeFileSync(libpath.join(cwd, files[i] + '.bak'), output);
} catch (e) {
log.error(' -> Backup error: ' + (e.stack || e));
continue;
}
} else {
log.info(' -> Backup already exists');
}
}
log.info(' -> Overwritting file');
try {
libfs.writeFileSync(libpath.join(cwd, files[i]), output);
} catch (e) {
log.error(' -> Write error: ' + (e.stack || e));
continue;
}
log.info(' -> Done');
}
log.info('\nOK');
return cb(null);
}
module.exports = main;