UNPKG

@stoplight/spectral

Version:

A flexible object linter with out of the box support for OpenAPI v2 and v3.

57 lines 2.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const fs_extra_1 = require("fs-extra"); const lodash_1 = require("lodash"); const path = require("path"); const reader_1 = require("../fs/reader"); const config_1 = require("../types/config"); const DEFAULT_CONFIG_FILE = 'spectral.yml'; const loadConfig = (filePath) => tslib_1.__awaiter(this, void 0, void 0, function* () { switch (path.extname(filePath)) { case '.json': case '.yaml': case '.yml': const { data, diagnostics } = yield reader_1.readParsable(filePath, 'utf8'); if (!lodash_1.isEmpty(diagnostics)) { throw new Error('Parsing exception'); } return data; default: throw new Error('Unknown file'); } }); exports.load = (filePath, command) => tslib_1.__awaiter(this, void 0, void 0, function* () { const config = yield loadConfig(filePath); exports.validate(config, command); exports.normalize(config, command); return lodash_1.merge(exports.createEmptyConfig(), config); }); exports.validate = (config, command) => { if (!config) throw new Error('Missing config'); if (!config[command]) throw new Error(`Missing ${command} command in config file`); }; exports.normalize = (config, command) => { switch (command) { case config_1.ConfigCommand.LINT: if (config.lint.ruleset) { const { ruleset } = config.lint; config.lint.ruleset = Array.isArray(ruleset) ? ruleset : [ruleset]; } break; } }; exports.createEmptyConfig = () => ({ lint: { encoding: 'utf8', format: config_1.ConfigFormat.STYLISH, verbose: false, }, }); exports.getDefaultConfigFile = (directory) => { const filePath = path.join(directory, DEFAULT_CONFIG_FILE); return fs_extra_1.existsSync(filePath) ? filePath : null; }; //# sourceMappingURL=configLoader.js.map