-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.js
156 lines (135 loc) · 4.97 KB
/
utils.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
const fs = require('fs.extra');
const color = require('colors');
const os = require('os');
const path = require('path');
const crypto = require('crypto');
const async = require('async');
const request = require('request');
const spawn = require('child_process').spawn;
const _ = require('lodash');
const bluebird = require('bluebird');
const rimraf = require('rimraf');
const Datastore = require('nedb');
bluebird.promisifyAll(Datastore.prototype);
let db = {};
let getHome = () => process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
let lodir = (...dir) => {
let newArgs = [__dirname].concat(dir);
return path.join(...newArgs);
};
let loadDB = () => {
try {
fs.mkdirSync(path.join(getHome(), '.randomapi'));
fs.mkdirSync(path.join(getHome(), '.randomapi', 'db'));
fs.mkdirSync(path.join(getHome(), '.randomapi', 'data'));
fs.mkdirSync(path.join(getHome(), '.randomapi', 'data', 'api'));
fs.mkdirSync(path.join(getHome(), '.randomapi', 'data', 'list'));
fs.mkdirSync(path.join(getHome(), '.randomapi', 'data', 'require'));
} catch(e) {}
db.api = new Datastore({filename: path.join(getHome(), '.randomapi', 'db', 'api.db'), autoload: true});
db.list = new Datastore({filename: path.join(getHome(), '.randomapi', 'db', 'list.db'), autoload: true});
db.require = new Datastore({filename: path.join(getHome(), '.randomapi', 'db', 'require.db'), autoload: true});
}
let emptyDB = done => {
db.api.removeAsync({}, { multi: true })
.then(db.list.removeAsync({}, { multi: true }))
.then(db.require.removeAsync({}, { multi: true }))
.then(done);
};
let deleteFiles = done => {
async.series([
cb => rimraf(path.join(getHome(), '.randomapi', 'data', 'api', '*'), cb),
cb => rimraf(path.join(getHome(), '.randomapi', 'data', 'list', '*'), cb),
cb => rimraf(path.join(getHome(), '.randomapi', 'data', 'require', '*'), cb)
], done);
};
let getConfig = () => {
let config;
try {
config = JSON.parse(fs.readFileSync(path.join(getHome(), '.randomapi', 'config.json')));
} catch (e) {
config = require(lodir('./config.json'));
fs.writeFileSync(path.join(getHome(), '.randomapi', 'config.json'), JSON.stringify(config, null, 2));
}
return config;
};
let getUsername = () => getConfig().username;
let setUsername = username => {
let config = JSON.parse(fs.readFileSync(path.join(getHome(), '.randomapi', 'config.json')));
config.username = username;
fs.writeFileSync(path.join(getHome(), '.randomapi', 'config.json'), JSON.stringify(config, null, 2));
};
let getToken = () => getConfig().clientToken;
let setToken = clientToken => {
let config = JSON.parse(fs.readFileSync(path.join(getHome(), '.randomapi', 'config.json')));
config.clientToken = clientToken;
fs.writeFileSync(path.join(getHome(), '.randomapi', 'config.json'), JSON.stringify(config, null, 2));
};
let warning = txt => console.log(color.yellow(txt));
let error = txt => console.log(color.red(txt));
let success = txt => console.log(color.green(txt));
let fingerprint = () => {
try {
let fingerprint = JSON.stringify({
eol: os.EOL,
arch: os.arch(),
constants: os.constants,
home: os.homedir(),
platform: os.platform(),
type: os.type()
});
return crypto.createHash('sha256').update(fingerprint).digest('hex');
} catch(e) {
return null;
}
};
let getDB = () => db;
let stopServer = (cb = () => {}) => {
request(`http://localhost:${getConfig().port}/shutdown`, function (error, response, body) {
if (!error && response.statusCode == 200) {
success("Shutting down OfflineAPI Server.");
} else {
warning("No response received from server...maybe it is already shutdown?");
}
cb();
});
};
let checkServer = cb => {
request(`http://localhost:${getConfig().port}/online`, function (error, response, body) {
cb(!error && response.statusCode == 200);
});
};
let startServer = (cb = () => {}) => {
checkServer(res => {
if (res) {
warning("Server appears to be online already.");
} else {
spawn('node', [lodir('api', 'server')]);
success(`Starting OfflineAPI Server on port ${getConfig().port}.`);
}
cb();
});
};
let restart = () => {
stopServer(startServer);
};
loadDB();
module.exports.lodir = lodir;
module.exports.getDB = getDB;
module.exports.loadDB = loadDB;
module.exports.deleteFiles = deleteFiles;
module.exports.emptyDB = emptyDB;
module.exports.getConfig = getConfig;
module.exports.getUsername = getUsername;
module.exports.setUsername = setUsername;
module.exports.getToken = getToken;
module.exports.setToken = setToken;
module.exports.getHome = getHome;
module.exports.warning = warning;
module.exports.error = error;
module.exports.success = success;
module.exports.fingerprint = fingerprint;
module.exports.startServer = startServer;
module.exports.stopServer = stopServer;
module.exports.restart = restart;
module.exports.checkServer = checkServer;