signalk-server
Version:
An implementation of a [Signal K](http://signalk.org) server for boats.
61 lines (60 loc) • 2.75 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Store = exports.SERVERSTATEDIRNAME = void 0;
const fs_1 = require("fs");
const promises_1 = require("fs/promises");
const path_1 = __importDefault(require("path"));
exports.SERVERSTATEDIRNAME = 'serverState';
class Store {
constructor(server, storePath, fileName = 'settings.json') {
this.filePath = '';
this.fileName = '';
this.filePath = path_1.default.join(server.config.configPath, exports.SERVERSTATEDIRNAME, storePath);
this.fileName = fileName;
this.init().catch((error) => {
console.log(`Could not initialise ${path_1.default.join(this.filePath, this.fileName)}`);
console.log(error);
});
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
read() {
return __awaiter(this, void 0, void 0, function* () {
const data = yield (0, promises_1.readFile)(path_1.default.join(this.filePath, this.fileName), 'utf8');
return JSON.parse(data);
});
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
write(data) {
return (0, promises_1.writeFile)(path_1.default.join(this.filePath, this.fileName), JSON.stringify(data));
}
init() {
return __awaiter(this, void 0, void 0, function* () {
try {
/* tslint:disable:no-bitwise */
yield (0, promises_1.access)(this.filePath, fs_1.constants.R_OK | fs_1.constants.W_OK);
/* tslint:enable:no-bitwise */
}
catch (_error) {
try {
yield (0, promises_1.mkdir)(this.filePath, { recursive: true });
}
catch (_error) {
console.log(`Error: Unable to create ${this.filePath}`);
}
}
});
}
}
exports.Store = Store;