UNPKG

lambdaorm-base

Version:
69 lines 3.2 kB
"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 }); exports.FileSchemaService = void 0; const domain_1 = require("../domain"); const path_1 = __importDefault(require("path")); const yaml = require('js-yaml'); class FileSchemaService { // eslint-disable-next-line no-useless-constructor constructor(schemaFileHelper, helper) { this.schemaFileHelper = schemaFileHelper; this.helper = helper; } read(source) { return __awaiter(this, void 0, void 0, function* () { const configPath = yield this.schemaFileHelper.getConfigPath(source); if (!configPath) { return null; } const content = yield this.readConfig(configPath); if (content === undefined || content === null) { throw new domain_1.SchemaError(`Schema file: ${configPath} empty`); } else if (path_1.default.extname(configPath) === '.yaml' || path_1.default.extname(configPath) === '.yml') { return { schema: yaml.load(content), path: configPath }; } else if (path_1.default.extname(configPath) === '.json') { return { schema: JSON.parse(content), path: configPath }; } else { throw new domain_1.SchemaError(`Schema file: ${configPath} not supported`); } }); } write(schema, fullPath) { return __awaiter(this, void 0, void 0, function* () { if (path_1.default.extname(fullPath) === '.yaml' || path_1.default.extname(fullPath) === '.yml') { yield this.helper.fs.write(fullPath, yaml.dump(schema)); } else if (path_1.default.extname(fullPath) === '.json') { yield this.helper.fs.write(fullPath, JSON.stringify(schema, null, 2)); } else { throw new domain_1.SchemaError(`Schema file: ${fullPath} not supported`); } }); } readConfig(path) { return __awaiter(this, void 0, void 0, function* () { if (path.startsWith('http')) { return yield this.helper.http.get(path); } return yield this.helper.fs.read(path); }); } } exports.FileSchemaService = FileSchemaService; //# sourceMappingURL=fileSchemaService.js.map