UNPKG

@accordproject/concerto-linter

Version:

Concerto Linter using Spectral rulesets

92 lines 4.28 kB
"use strict"; /* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 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.lintModel = lintModel; const spectral_core_1 = require("@stoplight/spectral-core"); const spectral_parsers_1 = require("@stoplight/spectral-parsers"); const config_loader_1 = require("./config-loader"); const getRuleset_1 = require("@stoplight/spectral-cli/dist/services/linter/utils/getRuleset"); const concerto_linter_default_ruleset_1 = __importDefault(require("@accordproject/concerto-linter-default-ruleset")); const concerto_core_1 = require("@accordproject/concerto-core"); /** * Converts Concerto model to JSON AST representation * @param {string | object} model - Concerto model as string or parsed object * @returns {string} JSON string of the AST * @throws {Error} For invalid model inputs */ function convertToJsonAST(model) { try { if (typeof model === 'string') { const manager = new concerto_core_1.ModelManager(); manager.addCTOModel(model); return JSON.stringify(manager.getAst()); } return JSON.stringify(model); } catch (error) { throw new Error(`Model conversion failed: ${error instanceof Error ? error.message : error}`); } } /** * Loads Spectral ruleset based on configuration options * @param {string} [rulesetOption] - Custom ruleset path or 'default' * @returns {Promise<Ruleset | RulesetDefinition>} Loaded ruleset */ function loadRuleset(rulesetOption) { return __awaiter(this, void 0, void 0, function* () { try { const rulesetPath = yield (0, config_loader_1.resolveRulesetPath)(rulesetOption); return rulesetPath ? yield (0, getRuleset_1.getRuleset)(rulesetPath) : concerto_linter_default_ruleset_1.default; } catch (error) { throw new Error(`Ruleset loading failed: ${error instanceof Error ? error.message : error}`); } }); } /** * Lints Concerto models using Spectral and Concerto rules * @param {string | object} model - Model to lint (string CTO or parsed AST) * @param {string} [rulesetOption] - Path to custom ruleset or 'default' * @returns {Promise<IRuleResult[]>} Linting results * @throws {Error} For critical processing failures */ function lintModel(model, rulesetOption) { return __awaiter(this, void 0, void 0, function* () { try { const jsonAST = convertToJsonAST(model); const ruleset = yield loadRuleset(rulesetOption); const spectral = new spectral_core_1.Spectral(); spectral.setRuleset(ruleset); const document = new spectral_core_1.Document(jsonAST, spectral_parsers_1.Json); return yield spectral.run(document); } catch (error) { throw new Error(`Linting process failed: ${error instanceof Error ? error.message : error}`); } }); } //# sourceMappingURL=index.js.map