galeforce-tmp-sea
Version:
A customizable, promise-based, and command-oriented TypeScript library for the Riot Games API.
80 lines • 3.79 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeWithDefaultConfig = exports.getConfig = exports.validate = void 0;
const yaml_1 = __importDefault(require("yaml"));
const fs_1 = __importDefault(require("fs"));
const process_1 = __importDefault(require("process"));
const ajv_1 = __importDefault(require("ajv"));
const TJS = __importStar(require("typescript-json-schema"));
const debug_1 = __importDefault(require("debug"));
const chalk_1 = __importDefault(require("chalk"));
const path_1 = __importDefault(require("path"));
const lodash_1 = __importDefault(require("lodash"));
const default_1 = __importDefault(require("./default"));
const initDebug = (0, debug_1.default)('galeforce:init');
initDebug(`${chalk_1.default.bold('reading config interface and converting to JSON schema')}`);
const configInterfacePath = path_1.default.join(__dirname, '..', 'interfaces', 'config.d.ts');
const program = TJS.getProgramFromFiles([configInterfacePath], {
strictNullChecks: true,
});
// Generate JSON schema from the ConfigInterface interface
const ConfigSchema = TJS.generateSchema(program, 'ConfigInterface', { required: true });
const ajv = new ajv_1.default();
exports.validate = ajv.compile(ConfigSchema); // Function validating a provided configuration object against valid field types
/**
*
* @param {string} filename The filename of the config file.
*
* @return {ConfigInterface} The corresponding config object.
*/
function getConfig(filename) {
const configObject = lodash_1.default.cloneDeepWith(yaml_1.default.parse(fs_1.default.readFileSync(filename, 'utf8')), (value) => {
if (typeof value === 'string') {
try {
return lodash_1.default.template(value)(process_1.default.env); // Substitute template string-like values with values in process.env
}
catch (e) {
if (e instanceof Error) {
// Throw if a required template string value is not present.
throw new Error(`[galeforce]: process.env.${e.message.split(' ')[0]} is required in config file but is undefined.`);
}
else {
throw new Error('[galeforce]: Config failed to process.');
}
}
}
});
return configObject;
}
exports.getConfig = getConfig;
function mergeWithDefaultConfig(config) {
return lodash_1.default.merge({}, default_1.default, config); // Merge the provided config object with the default object.
}
exports.mergeWithDefaultConfig = mergeWithDefaultConfig;
//# sourceMappingURL=index.js.map