@@ -11,6 +11,7 @@ import { Domain, Service } from './types';
1111import { getMessageTypeUtils } from './utils/catalog-shorthand' ;
1212import { OpenAPI } from 'openapi-types' ;
1313import checkLicense from './utils/checkLicense' ;
14+ import * as semver from 'semver' ;
1415
1516type Props = {
1617 services : Service [ ] ;
@@ -53,9 +54,11 @@ export default async (_: any, options: Props) => {
5354 const version = document . info . version ;
5455
5556 const service = buildService ( serviceSpec , document ) ;
56- let serviceMarkdown = service . markdown ;
57- let serviceSpecificationsFiles = [ ] ;
58- let serviceSpecifications = service . specifications ;
57+ let serviceCatalogPath = service . id ;
58+ let markdown = service . markdown ;
59+ let specFiles = [ ] ;
60+ let specifications = service . specifications ;
61+ let { sends, receives } = await processMessagesForOpenAPISpec ( serviceSpec . path , document ) ;
5962
6063 // Manage domain
6164 if ( options . domain ) {
@@ -91,64 +94,66 @@ export default async (_: any, options: Props) => {
9194 await addServiceToDomain ( domainId , { id : service . id , version : service . version } , domainVersion ) ;
9295 }
9396
94- // Process all messages for the OpenAPI spec
95- let { sends, receives } = await processMessagesForOpenAPISpec ( serviceSpec . path , document ) ;
96-
9797 // Check if service is already defined... if the versions do not match then create service.
9898 const latestServiceInCatalog = await getService ( service . id , 'latest' ) ;
99+ const existingVersionInCatalog = await getService ( service . id , version ) ;
100+
99101 console . log ( chalk . blue ( `Processing service: ${ document . info . title } (v${ version } )` ) ) ;
100102
103+ // Found a service, and versions do not match, we need to version the one already there
101104 if ( latestServiceInCatalog ) {
102- serviceMarkdown = latestServiceInCatalog . markdown ;
103- serviceSpecificationsFiles = await getSpecificationFilesForService ( service . id , 'latest' ) ;
104- sends = latestServiceInCatalog . sends || ( [ ] as any ) ;
105-
106- // persist any specifications that are already in the catalog
107- serviceSpecifications = {
108- ...serviceSpecifications ,
109- ...latestServiceInCatalog . specifications ,
110- } ;
111-
112- // Found a service, and versions do not match, we need to version the one already there
113- if ( latestServiceInCatalog . version !== version ) {
105+ if ( isHigherVersion ( version , latestServiceInCatalog . version ) ) {
114106 await versionService ( service . id ) ;
115107 console . log ( chalk . cyan ( ` - Versioned previous service (v${ latestServiceInCatalog . version } )` ) ) ;
108+ } else {
109+ serviceCatalogPath = serviceCatalogPath . concat ( `/versioned/${ version } ` ) ;
110+ console . log ( chalk . yellow ( ` - Service (v${ latestServiceInCatalog . version } ) already exists, skipped creation...` ) ) ;
116111 }
112+ }
113+
114+ if ( existingVersionInCatalog ) {
115+ markdown = existingVersionInCatalog . markdown ;
116+ specFiles = await getSpecificationFilesForService ( service . id , version ) ;
117+ sends = existingVersionInCatalog . sends || ( [ ] as any ) ;
118+ receives = [ ...( existingVersionInCatalog . receives ?? [ ] ) , ...receives ] ;
119+
120+ // persist any specifications that are already in the catalog
121+ specifications = {
122+ ...specifications ,
123+ ...existingVersionInCatalog . specifications ,
124+ } ;
117125
118126 // Match found, override it
119- if ( latestServiceInCatalog . version === version ) {
120- receives = latestServiceInCatalog . receives ? [ ...latestServiceInCatalog . receives , ...receives ] : receives ;
121- await rmServiceById ( service . id ) ;
122- }
127+ await rmServiceById ( service . id , version ) ;
123128 }
124129
125130 await writeService (
126131 {
127132 ...service ,
128- markdown : serviceMarkdown ,
129- specifications : serviceSpecifications ,
133+ markdown : markdown ,
134+ specifications : specifications ,
130135 sends,
131136 receives,
132137 } ,
133- { path : service . id }
138+ { path : serviceCatalogPath }
134139 ) ;
135140
136141 // What files need added to the service (speficiation files)
137- const specFiles = [
142+ const existingSpecFiles = [
138143 // add any previous spec files to the list
139- ...serviceSpecificationsFiles ,
144+ ...specFiles ,
140145 {
141146 content : openAPIFile ,
142147 fileName : service . schemaPath ,
143148 } ,
144149 ] ;
145150
146- for ( const specFile of specFiles ) {
151+ for ( const spec of existingSpecFiles ) {
147152 await addFileToService (
148153 service . id ,
149154 {
150- fileName : specFile . fileName ,
151- content : specFile . content ,
155+ fileName : spec . fileName ,
156+ content : spec . content ,
152157 } ,
153158 version
154159 ) ;
@@ -235,3 +240,6 @@ const processMessagesForOpenAPISpec = async (pathToSpec: string, document: OpenA
235240 }
236241 return { receives, sends : [ ] } ;
237242} ;
243+ function isHigherVersion ( sourceVersion : string , targetVersion : string ) {
244+ return semver . gt ( sourceVersion , targetVersion ) ;
245+ }
0 commit comments