mcopy/app/lib/settings/index.js

114 lines
2.4 KiB
JavaScript
Raw Normal View History

'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`);
this.defaultState = {
server: {
port: 1111,
enabled: true
},
devices: [],
profile: 'mcopy',
camera: {},
projector: {},
2022-06-12 17:37:00 +00:00
light: {},
capper: {}
};
this.state = this.freshState();
}
freshState() {
return JSON.parse(JSON.stringify(this.defaultState));
}
/**
*
**/
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);
//console.dir(this.state)
}
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);
}
}
this.state = this.freshState();
this.restore();
}
;
}
module.exports = new Settings();
//# sourceMappingURL=index.js.map