bailiff
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -1,11 +0,32 @@ | ||
import CentralStore from "./stores/centralStore"; | ||
import DotenvStore from "./stores/dotenvStore"; | ||
import CustomStore from "./stores/customStore"; | ||
import callsite from 'callsite'; | ||
import path from 'path'; | ||
import * as _ from 'lodash'; | ||
export default class Bailiff { | ||
"use strict"; | ||
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 }; | ||
}; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const centralStore_1 = __importDefault(require("./stores/centralStore")); | ||
const dotenvStore_1 = __importDefault(require("./stores/dotenvStore")); | ||
const customStore_1 = __importDefault(require("./stores/customStore")); | ||
const callsite_1 = __importDefault(require("callsite")); | ||
const path_1 = __importDefault(require("path")); | ||
const _ = __importStar(require("lodash")); | ||
class Bailiff { | ||
static addStore(config) { | ||
const stack = callsite(), requester = stack[1].getFileName(); | ||
CustomStore.add(config, path.dirname(requester)); | ||
const stack = callsite_1.default(), requester = stack[1].getFileName(); | ||
customStore_1.default.add(config, path_1.default.dirname(requester)); | ||
return this; | ||
@@ -18,11 +39,14 @@ } | ||
} | ||
static async loadCentralConfigs() { | ||
await CentralStore.loadCentralConfigs(); | ||
return this; | ||
static loadCentralConfigs() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield centralStore_1.default.loadCentralConfigs(); | ||
return this; | ||
}); | ||
} | ||
} | ||
exports.default = Bailiff; | ||
Bailiff.__configStoresPrioritized = [ | ||
CustomStore, | ||
DotenvStore, | ||
CentralStore | ||
customStore_1.default, | ||
dotenvStore_1.default, | ||
centralStore_1.default | ||
]; |
@@ -1,2 +0,4 @@ | ||
export default { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = { | ||
"001": "Bailiff: Can't connect to mongo", | ||
@@ -3,0 +5,0 @@ "002": "Bailiff: Custom file path is invalid", |
@@ -1,4 +0,21 @@ | ||
import Bailiff from "./bailiff"; | ||
export default async function bailiffPromise() { | ||
return await Bailiff.loadCentralConfigs(); | ||
"use strict"; | ||
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 }); | ||
const bailiff_1 = __importDefault(require("./bailiff")); | ||
function bailiffPromise() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield bailiff_1.default.loadCentralConfigs(); | ||
}); | ||
} | ||
exports.default = bailiffPromise; |
@@ -1,8 +0,29 @@ | ||
import DotenvStore from "./dotenvStore"; | ||
import * as fs from "fs"; | ||
import Errors from '../errors'; | ||
import * as _ from "lodash"; | ||
import { MongoClient } from 'mongodb'; | ||
import NodeCache from 'node-cache'; | ||
export default class CentralStore { | ||
"use strict"; | ||
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 }; | ||
}; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const dotenvStore_1 = __importDefault(require("./dotenvStore")); | ||
const fs = __importStar(require("fs")); | ||
const errors_1 = __importDefault(require("../errors")); | ||
const _ = __importStar(require("lodash")); | ||
const mongodb_1 = require("mongodb"); | ||
const node_cache_1 = __importDefault(require("node-cache")); | ||
class CentralStore { | ||
static get(name) { | ||
@@ -16,21 +37,23 @@ let value = this.nodeCache.get(`bailiff.config.${name}`); | ||
} | ||
static async loadCentralConfigs() { | ||
let connection; | ||
try { | ||
connection = await MongoClient.connect(this.__mongoUrl(), { promiseLibrary: Promise, useUnifiedTopology: true }); | ||
} | ||
catch (e) { | ||
console.error(Errors["001"]); | ||
return; | ||
} | ||
const db = connection.db(DotenvStore.get("BAILIFF_MONGO_DB")); | ||
const result = await db.collection(String(DotenvStore.get("BAILIFF_MONGO_COLLECTION"))).find({ "status": 1 }).toArray(); | ||
connection.close(); | ||
const jsonConfig = this.__parseResultAsJsonString(result); | ||
const jsongStringConfig = JSON.stringify(jsonConfig); | ||
if (jsongStringConfig != "{}") { | ||
fs.writeFileSync(this.jsonConfigFile, jsongStringConfig); | ||
} | ||
_.forEach(jsonConfig, (value, key) => { | ||
this.nodeCache.set(`bailiff.config.${key}`, value); | ||
static loadCentralConfigs() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let connection; | ||
try { | ||
connection = yield mongodb_1.MongoClient.connect(this.__mongoUrl(), { promiseLibrary: Promise, useUnifiedTopology: true }); | ||
} | ||
catch (e) { | ||
console.error(errors_1.default["001"]); | ||
return; | ||
} | ||
const db = connection.db(dotenvStore_1.default.get("BAILIFF_MONGO_DB")); | ||
const result = yield db.collection(String(dotenvStore_1.default.get("BAILIFF_MONGO_COLLECTION"))).find({ "status": 1 }).toArray(); | ||
connection.close(); | ||
const jsonConfig = this.__parseResultAsJsonString(result); | ||
const jsongStringConfig = JSON.stringify(jsonConfig); | ||
if (jsongStringConfig != "{}") { | ||
fs.writeFileSync(this.jsonConfigFile, jsongStringConfig); | ||
} | ||
_.forEach(jsonConfig, (value, key) => { | ||
this.nodeCache.set(`bailiff.config.${key}`, value); | ||
}); | ||
}); | ||
@@ -44,4 +67,5 @@ } | ||
} | ||
exports.default = CentralStore; | ||
CentralStore.jsonConfigFile = "./.centralConfig.json"; | ||
CentralStore.nodeCache = new NodeCache(); | ||
CentralStore.nodeCache = new node_cache_1.default(); | ||
CentralStore.__parseResultAsJsonString = (data) => { | ||
@@ -54,9 +78,9 @@ return _.reduce(data, (result, entry) => { | ||
CentralStore.__mongoUrl = () => { | ||
return DotenvStore.get("BAILIFF_MONGO_URI") || (function (connString) { | ||
connString += DotenvStore.get("BAILIFF_MONGO_USER") ? DotenvStore.get("BAILIFF_MONGO_USER") + ":" : ""; | ||
connString += DotenvStore.get("BAILIFF_MONGO_PASS") ? DotenvStore.get("BAILIFF_MONGO_PASS") + "@" : ""; | ||
connString += DotenvStore.get("BAILIFF_MONGO_HOST") ? DotenvStore.get("BAILIFF_MONGO_HOST") : ""; | ||
connString += DotenvStore.get("BAILIFF_MONGO_PORT") ? ":" + DotenvStore.get("BAILIFF_MONGO_PORT") : ""; | ||
return dotenvStore_1.default.get("BAILIFF_MONGO_URI") || (function (connString) { | ||
connString += dotenvStore_1.default.get("BAILIFF_MONGO_USER") ? dotenvStore_1.default.get("BAILIFF_MONGO_USER") + ":" : ""; | ||
connString += dotenvStore_1.default.get("BAILIFF_MONGO_PASS") ? dotenvStore_1.default.get("BAILIFF_MONGO_PASS") + "@" : ""; | ||
connString += dotenvStore_1.default.get("BAILIFF_MONGO_HOST") ? dotenvStore_1.default.get("BAILIFF_MONGO_HOST") : ""; | ||
connString += dotenvStore_1.default.get("BAILIFF_MONGO_PORT") ? ":" + dotenvStore_1.default.get("BAILIFF_MONGO_PORT") : ""; | ||
return connString; | ||
}("mongodb://")); | ||
}; |
@@ -1,7 +0,19 @@ | ||
import * as _ from 'lodash'; | ||
import * as fs from "fs"; | ||
import path from 'path'; | ||
import appRoot from 'app-root-path'; | ||
import Errors from '../errors'; | ||
export default class CustomStore { | ||
"use strict"; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const _ = __importStar(require("lodash")); | ||
const fs = __importStar(require("fs")); | ||
const path_1 = __importDefault(require("path")); | ||
const app_root_path_1 = __importDefault(require("app-root-path")); | ||
const errors_1 = __importDefault(require("../errors")); | ||
class CustomStore { | ||
static add(configs, dirName) { | ||
@@ -24,9 +36,9 @@ if (typeof configs == "string") { | ||
static __absoluteValidPath(relativePath, dirName) { | ||
let absolutePath = path.join(appRoot.toString(), relativePath); | ||
let absolutePath = path_1.default.join(app_root_path_1.default.toString(), relativePath); | ||
if (fs.existsSync(absolutePath)) | ||
return absolutePath; | ||
absolutePath = path.join(dirName, relativePath); | ||
absolutePath = path_1.default.join(dirName, relativePath); | ||
if (fs.existsSync(absolutePath)) | ||
return absolutePath; | ||
console.error(Errors["002"]); | ||
console.error(errors_1.default["002"]); | ||
return false; | ||
@@ -39,3 +51,3 @@ } | ||
if (Array.isArray(parsedJson)) { | ||
console.error(Errors["003"]); | ||
console.error(errors_1.default["003"]); | ||
} | ||
@@ -47,3 +59,3 @@ else { | ||
catch (e) { | ||
console.error(Errors["004"]); | ||
console.error(errors_1.default["004"]); | ||
} | ||
@@ -53,2 +65,3 @@ return jsonData; | ||
} | ||
exports.default = CustomStore; | ||
CustomStore.__configStore = {}; |
@@ -1,3 +0,5 @@ | ||
import 'dotenv/config'; | ||
export default class DotenvStore { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
require("dotenv/config"); | ||
class DotenvStore { | ||
static get(name) { | ||
@@ -7,1 +9,2 @@ return process.env[name]; | ||
} | ||
exports.default = DotenvStore; |
{ | ||
"name": "bailiff", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "A config manager", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
51134
272