-
Notifications
You must be signed in to change notification settings - Fork 17
/
settings.js
71 lines (69 loc) · 2.28 KB
/
settings.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
var settingsJSON = require("./settings.json");
var package = require("./package.json");
var watchFile = true;
const osHomedir = require('os').homedir(),
fs = require('fs'),
path = require('path');
var altSettings = {},
altSettingsName = path.resolve(osHomedir,"."+package.name+".json"),
getDir = function (dirPath){
var pwd = process.cwd();
if(typeof __dirname != "undefined")
pwd = __dirname;
return String(dirPath).search(/^app:\/\//i) === 0 ?
path.resolve.apply(path,[path.sep].concat(String(dirPath).replace(/\//g,path.sep).replace("app:"+(path.sep),pwd).split(path.sep).slice(1)))
: dirPath
};
if(fs.existsSync(altSettingsName)){
try{
altSettings = require(altSettingsName);
}catch(e){
altSettings = {};
}
}else{
if(process.argv[2] !== "config"){
console.log("Please run \""+package.name+" config\" before");
process.exit(1);
}else{
watchFile = false;
altSettings={};
}
}
settings = {};
settings = Object.assign(settingsJSON,altSettings);
// Example when handled through fs.watch listener
if(watchFile)
fs.watch(altSettingsName, { encoding: 'buffer' }, (eventType, filename) => {
if (filename) {
try{
altSettings = require(altSettingsName);
settings = Object.assign(settingsJSON,altSettings);
}catch(e){
console.error(e);
}
}
});
// check and trow if fails
switch(settingsJSON.dbType){
case "tingodb":
if (!fs.existsSync(getDir(settingsJSON.dbPath))) {
try{
fs.mkdirSync(getDir(settingsJSON.dbPath));
console.log(getDir(settingsJSON.dbPath), "Created!")
}catch(e){
throw new Error('Database directory not exists ' + settingsJSON.dbPath + ', please create!');
}
}
break;
}
switch(settingsJSON.dbProType){
case "tingodb":
if (!fs.existsSync(getDir(settingsJSON.dbProPath))) {
try{
fs.mkdirSync(getDir(settingsJSON.dbProPath))
}catch(e){
throw new Error('Database directory not exists ' + settingsJSON.dbProPath + ', please create!');
}
}
break;
}