import { findGraphChanges } from "./amfGraphDifferencer" ; import { parseRamlFile } from "../common/amfParser" ; import { ramlToolLogger } from "../common/logger" ; import { ApiChanges } from "./changes/apiChanges" ; import { applyRules } from "./rulesProcessor" ; import amf from "amf-client-js" ; import path from "path" ; import { FlattenedGraph } from "./amfGraphTypes" ; export class ApiDifferencer { static readonly DEFAULT_RULES_BASE_PATH = "resources/diff/rules/defaultRules.json" ; static readonly DEFAULT_RULES_PATH = path . join ( __dirname , "../../" , ApiDifferencer . DEFAULT_RULES_BASE_PATH ) ; static readonly DEFAULT_RULES_PACKAGE_PATH = path . join ( "@commerce-apps/raml-toolkit" , path . dirname ( ApiDifferencer . DEFAULT_RULES_BASE_PATH ) , path . basename ( ApiDifferencer . DEFAULT_RULES_BASE_PATH , ".json" ) ) ; constructor ( public baseApiSpec , public newApiSpec ) { } returns async findChanges ( ) { const [ leftGraph , rightGraph ] = await Promise . all ( [ this . generateGraph ( this . baseApiSpec ) , this . generateGraph ( this . newApiSpec ) , ] ) ; ramlToolLogger . info ( `Finding differences between flattened JSON-LD of ${ this . baseApiSpec } and ${ this . newApiSpec }` ) ; const apiChanges = new ApiChanges ( this . baseApiSpec , this . newApiSpec ) ; apiChanges . nodeChanges = findGraphChanges ( leftGraph , rightGraph ) ; return apiChanges ; } param rulesPath returns async findAndCategorizeChanges ( rulesPath = ApiDifferencer . DEFAULT_RULES_PATH ) { const apiChanges = await this . findChanges ( ) ; ramlToolLogger . info ( "Applying rules on the differences" ) ; apiChanges . nodeChanges = await applyRules ( apiChanges . nodeChanges , rulesPath ) ; return apiChanges ; } param ramlFilePath returns private async generateGraph ( ramlFilePath ) { const parsedRaml = await parseRamlFile ( ramlFilePath ) ; const model = new amf . Raml10Resolver ( ) . resolve ( parsedRaml ) ; const renderOptions = new amf . render . RenderOptions ( ) . withoutSourceMaps . withCompactUris . withFlattenedJsonLd ; let graphStr = await amf . AMF . amfGraphGenerator ( ) . generateString ( model , renderOptions ) ; /**
         * Types referenced from another RAML contain the filepath in their ID. Replace with empty string so that paths are not compared
         */ graphStr = graphStr . split ( path . dirname ( ramlFilePath ) ) . join ( "" ) ; return JSON . parse ( graphStr ) ; } }