2018-01-05 04:42:16 +00:00
|
|
|
const os = require('os');
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const settings = {};
|
|
|
|
|
|
|
|
settings.file = path.join(os.homedir(), `/.mcopy/settings.json`);
|
|
|
|
settings.state = {
|
|
|
|
server : {
|
|
|
|
port : 1111,
|
|
|
|
enabled : true
|
|
|
|
},
|
2018-01-21 23:53:15 +00:00
|
|
|
devices : [],
|
2018-01-05 04:42:16 +00:00
|
|
|
camera : {},
|
|
|
|
projector : {},
|
|
|
|
light : {}
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.checkDir = function () {
|
2018-01-21 23:53:15 +00:00
|
|
|
'use strict'
|
|
|
|
const dir = path.join(os.homedir(), '.mcopy/');
|
2018-01-05 04:42:16 +00:00
|
|
|
if (!fs.existsSync(dir)) {
|
|
|
|
fs.mkdirSync(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.save = function () {
|
2018-01-21 23:53:15 +00:00
|
|
|
'use strict'
|
2018-01-05 04:42:16 +00:00
|
|
|
const str = JSON.stringify(settings.state, null, '\t');
|
2018-01-21 23:53:15 +00:00
|
|
|
settings.checkDir();
|
2018-01-05 04:42:16 +00:00
|
|
|
fs.writeFile(settings.file, str, 'utf8', (err) => {
|
|
|
|
if (err) console.error(err);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
settings.update = function (key, val) {
|
2018-01-21 23:53:15 +00:00
|
|
|
'use strict'
|
|
|
|
settings.state[key] = val;
|
2018-01-05 04:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
settings.get = function (key) {
|
2018-01-21 23:53:15 +00:00
|
|
|
'use strict'
|
|
|
|
return settings.state[key];
|
2018-01-05 04:42:16 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 17:23:23 +00:00
|
|
|
settings.all = function () {
|
2018-01-21 23:53:15 +00:00
|
|
|
'use strict'
|
|
|
|
return settings.state;
|
2018-01-17 17:23:23 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 04:42:16 +00:00
|
|
|
settings.restore = function () {
|
2018-01-21 23:53:15 +00:00
|
|
|
'use strict'
|
|
|
|
let str;
|
|
|
|
settings.checkDir();
|
2018-01-05 04:42:16 +00:00
|
|
|
if (fs.existsSync(settings.file)) {
|
|
|
|
str = fs.readFileSync(settings.file, 'utf8')
|
2018-01-21 23:53:15 +00:00
|
|
|
settings.state = JSON.parse(str);
|
2018-01-05 04:42:16 +00:00
|
|
|
} else {
|
2018-01-21 23:53:15 +00:00
|
|
|
settings.save();
|
2018-01-05 04:42:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 23:53:15 +00:00
|
|
|
settings.reset = function () {
|
|
|
|
'use strict'
|
|
|
|
if (fs.existsSync(settings.file)) {
|
|
|
|
fs.unlinkSync(settings.file);
|
|
|
|
}
|
|
|
|
settings.restore();
|
|
|
|
};
|
|
|
|
|
2018-01-05 04:42:16 +00:00
|
|
|
module.exports = settings
|