-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
72 lines (62 loc) · 1.82 KB
/
app.js
File metadata and controls
72 lines (62 loc) · 1.82 KB
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
#!/usr/bin/env node
import inquirer from 'inquirer';
import encoding from 'encoding';
import iconvlite from 'iconv-lite';
import path from 'path';
import ora from 'ora';
import { readFile, writeFile } from 'fs';
import { glob } from 'glob';
export async function getFiles() {
return glob('*.{txt,srt}', { nocase: true });
}
export async function promptUser(files) {
return inquirer.prompt({
type: 'checkbox',
name: 'files',
message: 'Select files to convert',
pageSize: 30,
choices: files
});
}
export function processFile(file) {
return new Promise((resolve, reject) => {
readFile(file, (error, data) => {
if (error) {
reject(error);
} else {
const translated = encoding.convert(data, 'UTF-8', 'CP1250');
const converted = iconvlite.encode(translated, 'utf8').toString();
const spinner = ora({
text: `${path.basename(file)} - processing...`,
spinner: 'dots2'
}).start();
writeFile(file, converted, (errorFS) => {
if (errorFS) {
spinner.fail(`${path.basename(file)} - failed`);
console.error(errorFS);
} else {
spinner.succeed(`${path.basename(file)} - DONE`);
}
spinner.stop();
resolve(converted);
});
}
});
});
}
export async function run() {
const foundFiles = await getFiles();
if (foundFiles.length === 0) {
console.log('No .txt or .srt files found in the current directory.');
return;
}
const answers = await promptUser(foundFiles);
if (answers && answers.files && answers.files.length > 0) {
const files = answers.files.map(processFile);
await Promise.all(files);
}
}
// This allows the script to be executed directly, but also imported for testing.
if (process.env.NODE_ENV !== 'test') {
run();
}