forked from enricostara/termgram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
termgram.js
128 lines (114 loc) · 3.39 KB
/
termgram.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
// Termgram
// Copyright 2015 Enrico Stara 'enrico.stara@gmail.com'
// Released under the MIT License
// http://termgram.me
// clear the term
clearTerminal();
// setup the fs
var fs = require('fs');
var userHome = (process.env.HOME || process.env.USERPROFILE) + '/.termgram';
var logFolder = userHome + '/log';
try {
fs.mkdirSync(userHome, '0770');
fs.mkdirSync(logFolder, '0770');
} catch (e) {
}
// setup the logger
process.env.LOGGER_FILE = logFolder + '/termgram';
var getLogger = require('get-log');
getLogger.PROJECT_NAME = 'termgram';
var logger = getLogger('main');
// import other dependencies
require('colors');
require('@goodmind/telegram.link')(getSignature());
var clientProxy = require('./lib/client-proxy');
var ui = require('./lib/user-interface');
var i18n = require('./i18n/en-US');
var signUp = require('./lib/use-case/sign-up');
var signIn = require('./lib/use-case/sign-in');
var Updates = require('./lib/updates');
var selectChat = require('./lib/use-case/select-chat');
var chat = require('./lib/use-case/chat');
var userData = require('./lib/user-data');
userData.setBaseFolder(userHome);
// begin
function main() {
var users = userData.retrieveUsernameList();
console.log(i18n.welcome);
ui.spacer();
function doSignUp() {
signUp(users).then(function (res) {
logger.info('signUp res: %s', res);
home();
}, function (error) {
console.log('signUp error: ', error.stack);
shutdown();
});
}
// if no users
if (users.length == 0) {
logger.info('User list is empty, sign up a new user.');
doSignUp();
} else {
signIn(users).then(function (res) {
logger.info('signIn res:', res);
home();
}, function (error) {
if (error) {
console.log('signIn error: ', error.stack);
shutdown();
} else {
doSignUp();
}
});
}
ui.events.on(ui.EVENT.EXIT, function () {
ui.askConfirmationInput(i18n.exit, true).then(shutdown, function () {
console.log('nothing to do, again...')
});
});
}
// userHome page
function home() {
var updates = Updates.getInstance();
updates.start().then(function() {
ui.spacer();
selectChat().then(function (peer) {
if (peer) {
ui.spacer();
chat(peer).then(function () {
console.log('nothing to do, now...');
shutdown();
}, function (error) {
console.log('chat error: ', error.stack);
shutdown();
});
} else {
console.log('No chat selected');
console.log('nothing to do, now...');
shutdown();
}
}, function (error) {
console.log('selectChat error: ', error.stack);
shutdown();
});
}, function (error) {
console.log('update-emitter error: ', error.stack);
shutdown();
});
}
// end
function shutdown() {
ui.close();
Updates.getInstance().stop();
}
// clear the term
function clearTerminal() {
process.stdout.write('\033c');
}
// get the application signature
function getSignature() {
return (' T E R M G R A M '.bold + ' ' + require('./package.json').version ).cyan;
}
// run
main();