-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (83 loc) · 3.36 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env node
const { input, write, getArg, hasArg, exit } = require('./utils/cli.js');
const { mkdir, jsonFile } = require('./utils/files.js');
const translate = require('./utils/translate.js');
const { exec } = require('child_process');
const path = require('path');
const homeDir = require('os').homedir();
const pkgJson = require('./package.json');
(async () => {
mkdir(`${homeDir}/.vocabul`);
const action = getArg(['--action', '-a'], false);
const help = hasArg(['--help', '-h']);
const version = hasArg(['--version', '-v']);
if (help) {
write([
'Usage: vocabul [...arguments]',
'Available arguments are:',
' --action -a [action]: Required and Should be one of "translate", "random", or "interval"',
' --dictionary -d [dictionary_name=default]: Local dictionary name',
' --from -f [lang_code=auto]: Source language for "translate" action',
' --to -t [lang_code]: Target language for "translate" action',
' --word -w [word]: Input word for "translate" action',
' --add -y [y/n=y]: Add translated word to dictionary',
' --period -p [minute=1]: "interval" action period',
' --command -c [command="notify-send"]: "interval" action run command',
' --version -v: Show installed version',
' --help -h: Show help',
]);
exit(0);
}
if (version) {
write(pkgJson.version);
exit(0);
}
if (!action) {
write('--action arg is missing! Use --help to see how to use!');
exit(-1);
}
const dictionary = getArg(['--dictionary', '-d'], 'default');
const dictionaryFile = jsonFile(path.resolve(homeDir, '.vocabul', `${dictionary}.json`), []);
if (action === 'translate') {
const word = getArg(['--word', '-w']) || (await input('Enter the word:')) || exit(-1, 'Missing "word" variable!');
const from = getArg(['--from', '-f'], 'auto');
const to = getArg(['--to', '-t']) || (await input('Enter the target language code:')) || exit(-1, 'Missing "to" variable!');
const translation = await translate(word, from, to);
write(`${word}: ${translation}`);
let addToDict = null;
if (hasArg(['--add', '-y'])) {
addToDict = !['false', 'no', '0', 'n'].includes(getArg(['--add', '-y']));
} else {
const addToDictQ = await input(`Add "${word}" to "${dictionary}" dictionary? [y/n]`);
addToDict = addToDictQ !== 'n';
}
if (addToDict) {
dictionaryFile.content.push([word, translation]);
dictionaryFile.save();
}
exit(0);
}
if (action === 'random') {
if(dictionaryFile.content.length === 0) {
write(`Dictionary "${dictionary}" is empty!`);
exit(-1);
}
const randomWord = dictionaryFile.content[Math.floor(Math.random() * dictionaryFile.content.length)];
write(`${randomWord[0]}: ${randomWord[1]}`);
exit(0);
}
if (action === 'interval') {
period = getArg(['--period', '-p']) || (await input(`Interval period (minutes)? [1]`)) || '1';
const pmil = Number(period) * 60000;
const command = getArg(['--command', '-c'], 'notify-send');
const run = () => {
if(dictionaryFile.content.length > 0) {
const randomWord = dictionaryFile.content[Math.floor(Math.random() * dictionaryFile.content.length)];
write(`${randomWord[0]}: ${randomWord[1]}`);
exec(`${command} "${randomWord[0]}: ${randomWord[1]}"`);
}
}
run();
setInterval(run, pmil);
}
})();