-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
74 lines (64 loc) · 1.89 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
'use strict';
const
cli = require('commander');
cli.
usage('[options] <file>').
description('Split a movie script into files for each character removing ' +
'comments.').
option('-n, --dry-run', 'don\'t write output files').
option('-o, --output <path>',
'specify output directory, default is input file without extension').
parse(process.argv);
const
fs = require('./lib/fs-promise'),
path = require('path'),
throwFromPromise = (err) => {
process.nextTick(() => {
throw err;
});
};
let file = cli.args[0],
// create output directory
extension = path.extname(file),
Parser = require('./lib/parser-' + extension.substr(1)),
parser = new Parser(),
dir = path.basename(file, extension);
dir = cli.output || path.join(path.dirname(file), dir);
let mkOutputDir = fs.stat(dir);
if (!cli.dryRun) {
mkOutputDir.
then((stats) => {
if (!stats.isDirectory()) {
throwFromPromise(new Error('Output directory name already exists, ' +
'please remove it or specify another directory.'));
}
}, (err) => {
if (err.code === 'ENOENT') { // no such file or directory
mkOutputDir = fs.mkdir(dir); // promises work weird
return mkOutputDir;
}
throwFromPromise(err);
});
}
fs.readFile(file, { encoding: 'utf8' }).then((data) => {
parser.process(data);
// write files
if (!cli.dryRun) {
for (let character of parser.characters) {
mkOutputDir.then(() => {
let text = parser.dialogs[character];
fs.writeFile(path.join(dir, character + '.txt'), text);
});
}
}
// print character lines in order
let arrLines = [];
for (let character of parser.characters) {
arrLines.push([parser.lines[character], character]);
}
arrLines.
sort((a, b) => a[0] - b[0] || a[1].localeCompare(b[1])).
forEach((lines) => {
console.log(lines[1] + ' has ' + lines[0] + ' lines.');
});
});