ordigi/app/modules/config.js

39 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

var exports = module.exports = {};
var fs = require('fs'),
2016-01-27 20:38:08 +01:00
os = require('os'),
defaultConfigFile = (function() {
var f = __dirname;
for(var i=0; i<2; i++) {
2016-01-27 20:38:08 +01:00
f = f.substr(0, f.lastIndexOf(os.platform() == 'win32' ? '\\' : '/'));
}
2016-01-27 20:38:08 +01:00
return f + (os.platform() == 'win32' ? '\\config.ini-sample': '/config.ini-sample');
})(),
2016-01-27 20:38:08 +01:00
configFile = (process.env.HOME || process.env.USERPROFILE) + (os.platform() == 'win32' ? '\\.elodie\\config.ini' : '/.elodie/config.ini'),
hasConfig,
setConfig;
exports.hasConfig = function() {
console.log(defaultConfigFile);
console.log(configFile);
return fs.existsSync(configFile);
};
exports.writeConfig = function(params) {
var contents;
try {
if(exports.hasConfig()) {
contents = fs.readFileSync(configFile).toString();
} else {
contents = fs.readFileSync(defaultConfigFile).toString();
}
console.log(contents);
contents = contents.replace(/key=[\s\S]+$/,'key='+params['mapQuestKey']);
fs.writeFileSync(configFile, contents);
return true;
} catch(e) {
console.log(e);
return false;
}
};