UNPKG

open-collaboration-server

Version:

Open Collaboration Server implementation, part of the Open Collaboration Tools project

126 lines 4.59 kB
// ****************************************************************************** // Copyright 2024 TypeFox GmbH // This program and the accompanying materials are made available under the // terms of the MIT License, which is available in the project root. // ****************************************************************************** var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; import * as fs from 'node:fs'; import * as path from 'path'; import { inject, injectable, optional, postConstruct } from 'inversify'; import { parse as parseYaml } from 'yaml'; export const Configuration = Symbol('Configuration'); export const ConfigurationFile = Symbol('ConfigurationFile'); let DefaultConfiguration = class DefaultConfiguration { configurationFile; configuration = {}; initialize() { if (this.configurationFile) { const ext = path.extname(this.configurationFile).toLowerCase(); const configContent = fs.readFileSync(this.configurationFile, 'utf8'); let config; if (ext === '.yml' || ext === '.yaml') { config = parseYaml(configContent); } else { config = JSON.parse(configContent); } if (typeof config === 'object' && config !== null) { this.configuration = config; } } } getValue(key, type) { let value = this.getFromEnv(key); if (!value) { value = this.getFromConfig(key); } return this.convertToType(value, type); } convertToType(value, type) { if (value === undefined) { return undefined; } if (type === 'string' || type === undefined) { return String(value); } else if (type === 'number') { if (typeof value === 'string') { const parsed = parseInt(value, 10); if (!isNaN(parsed)) { return parsed; } } else if (typeof value === 'number') { return value; } } else if (type === 'boolean') { if (typeof value === 'string') { const lower = value.toLowerCase(); switch (lower) { case 'true': return true; case 'false': return false; } } else if (typeof value === 'boolean') { return value; } } return undefined; } getFromEnv(key) { return process.env[toEnvKey(key)]; } getFromConfig(key) { return this.getFromConfigObject(this.configuration, toObjKey(key)); } getFromConfigObject(object, key) { let value = object[key]; if (isConfigValue(value)) { return value; } const segments = key.split('.'); for (let i = segments.length - 1; i >= 1; i--) { const key = segments.slice(0, i).join('.'); value = object[key]; if (typeof value === 'object' && value !== null) { return this.getFromConfigObject(value, segments.slice(i).join('.')); } } return undefined; } }; __decorate([ inject(ConfigurationFile), optional() ], DefaultConfiguration.prototype, "configurationFile", void 0); __decorate([ postConstruct() ], DefaultConfiguration.prototype, "initialize", null); DefaultConfiguration = __decorate([ injectable() ], DefaultConfiguration); export { DefaultConfiguration }; /** * Converts a kebab-case key to a SNAKE_CASE key. */ function toEnvKey(key) { return key.replace(/-/g, '_').toUpperCase(); } /** * Converts a kebab-case key to a dot.separated.key. */ function toObjKey(key) { return key.replace(/-/g, '.'); } function isConfigValue(value) { return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean'; } //# sourceMappingURL=configuration.js.map