@wbg-mde/repository
Version:
Managing all common method for file system CRUD operations.
96 lines (95 loc) • 3.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const project_configuration_1 = require("../configuration/project.configuration");
const resource_path_1 = require("../resource-path/resource-path");
const model_1 = require("@wbg-mde/model");
const fs = require("fs");
const path = require("path");
const mkdirp = require("mkdirp");
class VariableRepository {
constructor() {
this.resourcePath = new resource_path_1.ResourcePath();
this.createDefaultLocalRpository();
}
getLocalRepository() {
project_configuration_1.configuaration.refresh();
let filePath = project_configuration_1.configuaration.variableRepoConfig.localPath;
if (!filePath) {
filePath = this.resourcePath.getDefaultLocalRepository();
}
try {
let data = fs.readFileSync(filePath);
data = JSON.parse(data.toString());
return data;
}
catch (e) {
let data = new model_1.CategoryRepository({});
;
if (e.code === 'ENOENT') {
this.createRepositoryFile(filePath, data);
}
return data;
}
}
getGlobalRepository() {
project_configuration_1.configuaration.refresh();
if (project_configuration_1.configuaration.variableRepoConfig.donotUseGlobal === true) {
return undefined;
}
let filePath = project_configuration_1.configuaration.variableRepoConfig.globalPath;
if (!filePath) {
return false;
}
try {
let data = fs.readFileSync(filePath);
data = JSON.parse(data.toString());
return data;
}
catch (e) {
let data = new model_1.CategoryRepository({});
;
if (e.code === 'ENOENT') {
this.createRepositoryFile(filePath, data);
}
return data;
}
}
createRepositoryFile(filePath, data, force) {
if (force === true || !fs.existsSync(filePath)) {
if (!data) {
data = new model_1.CategoryRepository({});
}
let directory = path.dirname(filePath);
mkdirp.sync(directory);
fs.writeFileSync(filePath, JSON.stringify(data));
}
}
saveRepository(data, isLocalRepository) {
let filePath;
if (isLocalRepository === true) {
filePath = project_configuration_1.configuaration.variableRepoConfig.localPath;
if (!filePath) {
filePath = this.resourcePath.getDefaultLocalRepository();
}
}
else {
if (project_configuration_1.configuaration.variableRepoConfig.donotUseGlobal === true || !data) {
return undefined;
}
filePath = project_configuration_1.configuaration.variableRepoConfig.globalPath;
if (!filePath) {
return false;
}
}
this.createRepositoryFile(filePath, data, true);
}
createDefaultLocalRpository() {
let filePath = this.resourcePath.getDefaultLocalRepository();
let directory = path.dirname(filePath);
mkdirp.sync(directory);
if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, JSON.stringify(new model_1.CategoryRepository({})));
}
}
}
exports.VariableRepository = VariableRepository;