-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.js
135 lines (116 loc) · 3.59 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const read = require('read');
var mwApi = require('./MediaWikiAPI.js');
var mwMessageCollection = null;
// List of available commands
var commands = {
help: help,
fetch: fetch,
exit: exit,
login: login,
skip: skip,
translate: translate,
};
function chat() {
input( 'Command:', answer => {
if( commands.hasOwnProperty( answer ) ) {
commands[answer]();
} else {
console.log('Sorry, no such command:', answer);
chat();
};
});
}
exit.doc = 'Just exit this script';
function exit() {
console.log('Bye');
}
fetch.doc = 'will fetch any available messages requiring translation';
function fetch() {
input('LanguageCode: ', languageCode => {
mwApi.getUntranslatedMessages( languageCode, function ( messageCollection ) {
mwMessageCollection = messageCollection;
console.log(`Fetched ${mwMessageCollection.length} untranslated messages`);
// Check if there are any messages to be translated
if( !mwMessageCollection.length > 0 ) {
console.log('No messages to be translated.');
return;
}
console.log('The current message to translate is:')
console.log(mwMessageCollection[currentMessageIndex].definition);
console.log("Type 'translate' to translate it or 'skip' to skip it.");
chat();
} );
} );
};
var currentMessageIndex = 0;
translate.doc = 'Translate the current message: ';
function translate() {
if( mwMessageCollection !== null ) {
var firstMessage = mwMessageCollection[currentMessageIndex];
console.log(firstMessage.definition);
input('Translation: ', answer => {
mwApi.addTranslation(firstMessage.title, answer, 'Added from CLI tool', () => {
// We already have fetched messages at this point
skip();
});
});
}
else {
console.log('Please fetch messages first before attempting to translate.');
chat();
}
};
login.doc = 'Allows you to provide credentials';
function login() {
var user = {};
input('Username: ', answer => {
user.name = answer;
input('Password: ', answer => {
user.password = answer;
mwApi.login(user.name, user.password, err => {
if(err) {
console.log(err);
};
chat();
});
}, true); // password is silent
});
}
function input(prompt, cb, silent) {
console.log("\x1b[32m"); // Switch to green
read({prompt:prompt, silent: !!silent}, (err, answer) => {
console.log("\x1b[0m"); // Reset console color
cb(answer);
});
}
help.doc = 'Shows help message';
function help() {
console.log('Commands:');
for(var command in commands) {
console.log(command + ':', commands[command].doc || '');
};
console.log('\n');
chat();
};
skip.doc = 'Skip and show the next message.';
function skip() {
if( mwMessageCollection !== null ) {
if( currentMessageIndex + 1 >= mwMessageCollection.length ) {
console.log('No more messages to translate.');
return;
}
currentMessageIndex++;
console.log('The next message is: ');
console.log(mwMessageCollection[currentMessageIndex]);
}
else {
console.log('No messages have been fetched yet.');
}
chat();
}
if (require.main === module) { // not required as a module
help();
} else {
console.log( 'module.exports is', chat );
module.exports = chat;
};