-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (89 loc) · 2.71 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
99
100
101
102
103
104
105
106
import fs from 'fs';
import Config from './config';
import EmulatorApp from './application';
let app;
let errorsOccurred = false;
process.on('uncaughtException', function(err) {
errorsOccurred = true;
const currentDate = new Date();
const currentDay = currentDate.toDateString();
const currentTime = currentDate.toTimeString();
let stack = "";
try {
stack = err.stack;
}
catch (error) {
stack = err.toString();
}
fs.appendFileSync('error.log', `[${currentDay} ${currentTime}] ${stack}\n`);
if (app) {
// Not a best practice
app._error(err);
}
});
process.on('exit', function() {
if (errorsOccurred) {
console.error("There were errors. See error.log for more information");
}
});
const config = new Config();
app = new EmulatorApp(config);
global.debug = app.screen.debug.bind(app.screen);
const connectionOptions = Object.assign({}, config.CLIENT_CONNECTION_OPTIONS.default);
if (process.argv.length > 2) {
const args = process.argv.slice(2);
parseArgs(args, connectionOptions);
// Hack because shiftr doesn't support multiple same usernames across
// namespaces and we need the usernames to be 'sculpture0', etc.
config.username = connectionOptions.username.split('-')[0];
}
app.connectAndSetup(connectionOptions);
app.render();
function parseArgs(args, options) {
// Expects at least 1 argument in args
// Allows for:
// * Password from environment and username:
// <env alias> <username>
// * Any username from environment:
// <env alias>
// * Host from environment:
// <env alias> <username> <password>
// * Just credentials on default host:
// - <username> <password>
// * Complete credentials:
// <host> <username> <password>
let credentials;
try {
credentials = JSON.parse(fs.readFileSync('.credentials'));
}
catch (e) {
console.warn("Warning: No parsable .credentials file found, trying reasonable defaults");
credentials = {};
}
let [env, username, password] = args;
let host = options.host;
if (env !== '-') {
console.log("Using authentication information provided by .credentials");
if (credentials.hasOwnProperty(env)) {
const envInfo = credentials[env];
host = envInfo.host;
username = username || Object.keys(envInfo.users)[0];
password = password || envInfo.users[username];
}
else {
console.log("Assuming given environment argument is host");
host = env;
}
}
else {
console.log("Using authentication information provided by command arguments");
}
if (username && password) {
options.host = host;
options.username = username;
options.password = password;
}
else {
console.error("Not enough information. Continuing with defaults.");
}
}