2019-03-21 22:03:53 +00:00
|
|
|
'use strict';
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const os = require("os");
|
|
|
|
const path = require("path");
|
|
|
|
const fs = require("fs-extra");
|
|
|
|
class Settings {
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
constructor() {
|
|
|
|
this.file = path.join(os.homedir(), `/.mcopy/settings.json`);
|
2019-04-15 20:13:28 +00:00
|
|
|
this.defaultState = {
|
2019-03-21 22:03:53 +00:00
|
|
|
server: {
|
|
|
|
port: 1111,
|
|
|
|
enabled: true
|
|
|
|
},
|
|
|
|
devices: [],
|
|
|
|
profile: 'mcopy',
|
|
|
|
camera: {},
|
|
|
|
projector: {},
|
|
|
|
light: {}
|
|
|
|
};
|
2019-04-15 20:13:28 +00:00
|
|
|
this.state = this.freshState();
|
|
|
|
}
|
|
|
|
freshState() {
|
|
|
|
return JSON.parse(JSON.stringify(this.defaultState));
|
2019-03-21 22:03:53 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
async checkDir() {
|
|
|
|
const dir = path.join(os.homedir(), '.mcopy/');
|
|
|
|
const exists = await fs.exists(dir);
|
|
|
|
if (!exists) {
|
|
|
|
try {
|
|
|
|
await fs.mkdir(dir);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
if (err.code === 'EEXIST')
|
|
|
|
return true;
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
async save() {
|
|
|
|
const str = JSON.stringify(this.state, null, '\t');
|
|
|
|
this.checkDir();
|
|
|
|
try {
|
|
|
|
await fs.writeFile(this.file, str, 'utf8');
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
update(key, val) {
|
|
|
|
this.state[key] = val;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
get(key) {
|
|
|
|
return this.state[key];
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
all() {
|
|
|
|
return this.state;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
async restore() {
|
|
|
|
let exists;
|
|
|
|
let str;
|
|
|
|
this.checkDir();
|
|
|
|
exists = await fs.exists(this.file);
|
|
|
|
if (exists) {
|
|
|
|
str = await fs.readFile(this.file, 'utf8');
|
|
|
|
this.state = JSON.parse(str);
|
2019-03-24 01:58:22 +00:00
|
|
|
//console.dir(this.state)
|
2019-03-21 22:03:53 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
async reset() {
|
|
|
|
const exists = await fs.exists(this.file);
|
|
|
|
if (exists) {
|
|
|
|
try {
|
|
|
|
await fs.unlink(this.file);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 20:13:28 +00:00
|
|
|
this.state = this.freshState();
|
2019-03-21 22:03:53 +00:00
|
|
|
this.restore();
|
|
|
|
}
|
|
|
|
;
|
2018-01-05 04:42:16 +00:00
|
|
|
}
|
2019-03-21 22:03:53 +00:00
|
|
|
module.exports = new Settings();
|
|
|
|
//# sourceMappingURL=index.js.map
|