import path from "path" ; import fs from "fs-extra" ; import { client , model , Core , MessageStyles , ProfileName , ProfileNames , } from "amf-client-js" ; import { parseRamlFile } from "../common/amfParser" ; import { AMF } from "amf-client-js" ; import _ from "lodash" ; export const PROFILE_PATH = path . join ( __dirname , "../../resources/lint/profiles" ) ; type AmfError = Record < string , any > ; export async function mergeValidationReports ( ramlValidationReport , customValidationReport ) { return new client . validate . ValidationReport ( ramlValidationReport . conforms && customValidationReport . conforms , ramlValidationReport . model , customValidationReport . profile , _ . unionBy ( ramlValidationReport . results , customValidationReport . results , "source" ) ) ; } param amfModel param profileFile export async function validateCustom ( amfModel , profileFile ) { let profileName ; try { profileName = await Core . loadValidationProfile ( profileFile ) ; } catch ( err ) { if ( err instanceof Error ) { throw err ; } const message = `${ err . message ?? err }` ; const cleaned = new Error ( message . replace ( /^amf\S+? / , "" ) ) as Error & { amfError : AmfError ; } ; cleaned . amfError = err ; throw cleaned ; } const ramlValidationReport = await Core . validate ( amfModel , ProfileNames . RAML , MessageStyles . RAML ) ; const customValidationReport = await Core . validate ( amfModel , profileName , MessageStyles . RAML ) ; return mergeValidationReports ( ramlValidationReport , customValidationReport ) ; } param model param profile export async function validateModel ( model , profile ) { if ( ! fs . existsSync ( profile ) ) { console . log ( `Using built in profile: ${ profile }` ) ; profile = path . join ( PROFILE_PATH , `${ profile }.raml` ) ; } const results = await validateCustom ( model , `file://${ profile }` ) ; return results ; } param results param warnings export async function printResults ( results , warnings = false ) { if ( results && ! warnings && results . conforms ) { console . log ( `Model: ${ results . model }
Profile: ${ results . profile }
Conforms? ${ results . conforms }
Number of results: 0
Number of hidden warnings: ${ results . results . length }
    ` ) ; } else if ( results ) { console . log ( results . toString ( ) ) ; } } param filename param profile export async function validateFile ( filename , profile ) { await AMF . init ( ) ; const model = await parseRamlFile ( filename ) ; if ( ! model ) throw new Error ( "Error validating file" ) ; return await validateModel ( model , profile ) ; }