This repository has been archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gce-cli.js
executable file
·331 lines (295 loc) · 9.66 KB
/
gce-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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env node
const fs = require('fs');
const argv = require('minimist')(process.argv.slice(2));
const prompts = require('prompts');
const axios = require('axios');
const log = {
blue: message => console.log('\x1b[34m', message, '\x1b[0m'),
green: message => console.log('\x1b[32m', message, '\x1b[0m'),
red: message => console.log('\x1b[31m', message, '\x1b[0m'),
}
const onCancel = prompt => {
log.red('Aborted by user!');
process.exit(0);
}
const deleteFolderRecursive = async path => {
if (fs.existsSync(path)) {
for (let entry of await fs.readdirSync(path)) {
const curPath = path + "/" + entry;
if ((fs.lstatSync(curPath)).isDirectory())
await deleteFolderRecursive(curPath);
else await fs.unlinkSync(curPath);
}
await fs.rmdirSync(path);
}
};
const GCE_LINK = 'https://raw.githubusercontent.com/Toinane/Google-Chat-Extender/master/googleChatExtender.js';
const GCE_PLUGIN_LINK = 'https://raw.githubusercontent.com/Toinane/Google-Chat-Extender/master/googleChatExtenderPlugin.js';
const THEME_PLUGIN_LINK = 'https://raw.githubusercontent.com/Toinane/Google-Chat-Extender/master/plugins/themeLoader.js';
const PLUGIN_LIST = 'https://raw.githubusercontent.com/Toinane/Google-Chat-Extender/master/pluginList.json';
let globalPath = '/Applications/Chat.app';
function getArguments() {
if(argv.p === true) {
log.red(`
Invalid options: -p
Usage: gce -p <path> The relative/absolute path of Chat.app
Use "gce help" for complete list of options
`);
process.exit(0);
}
if(argv.p) globalPath = argv.p;
}
async function checkPath(path) {
try {
fs.readdirSync('/Applications/' + path);
log.blue('Chat.app found!');
return '/Applications/' + path;
} catch(err) {}
try {
log.blue(`Searching ${path}...`);
fs.readdirSync(path);
log.blue('Chat.app found!');
return path;
} catch(err) {
response = await prompts({
type: 'text',
name: 'path',
message: 'it seems we can\'t find Chat.app. Can you give the relative/absolute path'
}, {onCancel: onCancel});
return checkPath(response.path);
}
}
async function checkGCE(path) {
try {
const dir = fs.readFileSync(path + '/Contents/Resources/app/googleChatExtender.js');
return true;
} catch(err) {
return false;
}
}
async function installGCE(path) {
log.blue('Fetch GCE files..');
const gce = await axios.get(GCE_LINK);
const gcePlugin = await axios.get(GCE_PLUGIN_LINK);
const themePlugin = await axios.get(THEME_PLUGIN_LINK);
log.blue('Files fetched!');
try {
fs.chmodSync(path, '777');
log.blue('Trying to update Chat\'s files..');
const main = fs.readFileSync(path + '/main.js');
fs.writeFileSync(path + '/main.js', 'require(__dirname + \'/googleChatExtender.js\');' + main);
log.blue('Chat\'s files updated!');
} catch(err) { throw new Error('It seem we cannot modify Chat\'s files.. Retry with sudo!'); }
try {
log.blue('Installing GCE files..');
fs.writeFileSync(path + '/googleChatExtender.js', gce.data);
fs.writeFileSync(path + '/googleChatExtenderPlugin.js', gcePlugin.data);
log.blue('GCE files installed!');
try {
log.blue('Installing ThemeLoader Plugin..');
fs.readdirSync(path + '/plugins');
} catch(err) { fs.mkdirSync(path + '/plugins'); }
fs.writeFileSync(path + '/plugins/themeLoader.js', themePlugin.data);
log.blue('ThemeLoader Plugin installed!');
}
catch(err) { throw new Error(err); }
}
async function uninstallGCE(path) {
try {
log.blue('Trying to restore Chat\'s files..');
const main = fs.readFileSync(path + '/main.js');
fs.writeFileSync(path + '/main.js', main.slice(46));
log.blue('Chat\'s files restored!');
} catch(err) { log.red(err); throw new Error('It seem we cannot modify Chat\'s files.. Retry with sudo!');}
try {
log.blue('Unistall GCE files..');
fs.unlinkSync(path + '/googleChatExtender.js');
fs.unlinkSync(path + '/googleChatExtenderPlugin.js');
await deleteFolderRecursive(path + '/plugins');
}
catch(err) { throw new Error(err); }
}
async function updateGCE(path) {
try {
log.blue('Fetch GCE files..');
const gce = await axios.get(GCE_LINK);
const gcePlugin = await axios.get(GCE_PLUGIN_LINK);
log.blue('Files fetched!');
log.blue('Installing GCE files..');
fs.writeFileSync(path + '/googleChatExtender.js', gce.data);
fs.writeFileSync(path + '/googleChatExtenderPlugin.js', gcePlugin.data);
log.blue('GCE files installed!');
}
catch(err) { throw new Error(err); }
}
async function info() {
log.green(`
###################
# GCE Information #
###################
`);
try {
let path = await checkPath(globalPath);
let check = await checkGCE(path);
if(!check) { return log.red('GCE is not installed. You can install it with "gce install".'); }
log.green('Google Chat path: ' + path);
log.blue('Plugins installed:')
for (let entry of await fs.readdirSync(path + '/Contents/Resources/app/plugins')) {
log.green('- ' + entry);
}
}
catch(err) { log.red(err); }
}
async function install() {
log.green(`
####################
# GCE Installation #
####################
`);
try {
let path = await checkPath(globalPath);
let check = await checkGCE(path);
if(check) {
response = await prompts({
type: 'confirm',
name: 'value',
message: 'it seems GCE is already installed. Should overwrite it?'
}, {onCancel: onCancel});
if(!response.value) onCancel();
} else {
log.blue('GCE isn\'t already installed.');
}
await installGCE(path + '/Contents/Resources/app');
log.green('GCE is now installed! You can launch Google Chat.');
} catch(err) {
log.red(err);
}
}
async function uninstall() {
log.green(`
######################
# GCE Uninstallation #
######################
`);
try {
let path = await checkPath(globalPath);
let check = await checkGCE(path);
if(!check) { return log.green('GCE is not installed :)'); }
await uninstallGCE(path + '/Contents/Resources/app');
log.green('GCE is now removed from Google Chat!');
} catch(err) {
log.red(err);
}
}
async function update() {
log.green(`
################
# GCE Updating #
################
`);
try {
let path = await checkPath(globalPath);
let check = await checkGCE(path);
if(!check) { return log.green('GCE is not installed :)'); }
await updateGCE(path + '/Contents/Resources/app');
log.green('GCE is now updated to latest version! You can launch Google Chat.');
} catch(err) {
log.red(err);
}
}
async function add() {
const url = 'https://raw.githubusercontent.com/Toinane/Google-Chat-Extender/master/plugins/';
log.green(`
#####################
# GCE Plugin Adding #
#####################
`);
try {
if(!argv._[1]) { return log.red('You have to write the name of the plugin you want install'); }
let name = argv._[1];
let path = await checkPath(globalPath);
let check = await checkGCE(path);
if(!check) { return log.red('GCE is not installed. Install it before with "gce install"'); }
try {
const plugin = await axios.get(url + name + '.js');
fs.writeFileSync(path + `/Contents/Resources/app/plugins/${name}.js`, plugin.data);
log.green(name + ' is now installed on Google Chat!');
}
catch(err) { return log.red('It seem this plugin doesn\'t exist.. You can see the plugin list with "gce list"'); }
}
catch(err) {
log.red(err);
}
}
async function remove() {
log.green(`
#######################
# GCE Plugin Removing #
#######################
`);
try {
if(!argv._[1]) { return log.red('You have to write the name of the plugin you want install'); }
let name = argv._[1];
let path = await checkPath(globalPath);
let check = await checkGCE(path);
if(!check) { return log.red('GCE is not installed. Install it before with "gce install"'); }
try {
fs.unlinkSync(path + `/Contents/Resources/app/plugins/${name}.js`);
log.green(name + ' is now removed from Google Chat!');
}
catch(err) {
log.red(err)
return log.red('It seem this plugin is not installed!');
}
}
catch(err) {
log.red(err);
}
}
async function list() {
log.green(`
##########################
# GCE Plugins availables #
##########################
`);
try {
const plugins = await axios.get(PLUGIN_LIST);
plugins.data.plugins.map(plugin => {
log.green('- ' + plugin.name);
log.blue(plugin.description);
log.blue('');
});
}
catch(err) {
log.red('Cannot get plugin list. Are you connected to internet?');
}
}
function help() {
console.log(`
gce controls the Google Chat Extender.
Find more information at https://github.com/Toinane/Google-Chat-Extender.
Commands:
install [arguments] Launch the installation of GCE
uninstall [arguments] Launch the uninstallation of GCE
update [arguments] Update to latest version of GCE
info [arguments] Show informations about your GCE's plugins
add <plugin_name> [arguments] Add a plugin for your GCE installation
remove <plugin_name> [arguments] Remove a plugin of your GCE installation
list List all plugins availables
help Show this help
Arguments:
-p <path> The relative/absolute path of Chat.app
`);
}
/* Start CLI */
getArguments();
switch(argv._[0]) {
case 'info': info(); break;
case 'install': install(); break;
case 'uninstall': uninstall(); break;
case 'update': update(); break;
case 'add': add(); break;
case 'remove': remove(); break;
case 'list': list(); break;
default: help(); break;
}