@canboat/canboatjs
Version:
Native javascript version of canboat
61 lines • 2.19 kB
JavaScript
/**
* Copyright 2018 Scott Bender (scott@scottbender.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.savePersistedData = exports.getPersistedData = void 0;
const fs_1 = __importDefault(require("fs"));
const getDataPath = (options) => {
if (options.app?.config?.configPath !== undefined) {
return `${options.app.config.configPath}/canboatjs-data.json`;
}
};
const getPersistedData = (options, id, key) => {
const path = getDataPath(options);
if (path !== undefined) {
const content = fs_1.default.readFileSync(path);
const data = JSON.parse(content.toString());
return data[id] !== undefined && data[id][key];
}
};
exports.getPersistedData = getPersistedData;
const savePersistedData = (options, id, key, value) => {
const path = getDataPath(options);
if (path !== undefined) {
let content;
try {
content = fs_1.default.readFileSync(path).toString();
}
catch (err) {
if (err.code === 'ENOENT') {
content = '{}';
}
else {
throw err;
}
}
const data = JSON.parse(content.toString());
if (data[id] === undefined) {
data[id] = {};
}
data[id][key] = value;
fs_1.default.writeFileSync(path, JSON.stringify(data, null, 2));
}
};
exports.savePersistedData = savePersistedData;
//# sourceMappingURL=persist.js.map
;