@@ -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 [ ] ;
@@ -50,6 +51,7 @@ export default async (_: any, options: Props) => {
5051
5152 const services = options . services ?? [ ] ;
5253 validateOptions ( options ) ;
54+
5355 for ( const serviceSpec of services ) {
5456 console . log ( chalk . green ( `Processing ${ serviceSpec . path } ` ) ) ;
5557
@@ -66,9 +68,11 @@ export default async (_: any, options: Props) => {
6668 const version = document . info . version ;
6769
6870 const service = buildService ( serviceSpec , document ) ;
69- let serviceMarkdown = service . markdown ;
70- let serviceSpecificationsFiles = [ ] ;
71- let serviceSpecifications = service . specifications ;
71+ let serviceCatalogPath = service . id ;
72+ let markdown = service . markdown ;
73+ let specFiles = [ ] ;
74+ let specifications = service . specifications ;
75+ let { sends, receives } = await processMessagesForOpenAPISpec ( serviceSpec . path , document ) ;
7276
7377 // Manage domain
7478 if ( options . domain ) {
@@ -104,64 +108,66 @@ export default async (_: any, options: Props) => {
104108 await addServiceToDomain ( domainId , { id : service . id , version : service . version } , domainVersion ) ;
105109 }
106110
107- // Process all messages for the OpenAPI spec
108- let { sends, receives } = await processMessagesForOpenAPISpec ( serviceSpec . path , document ) ;
109-
110111 // Check if service is already defined... if the versions do not match then create service.
111112 const latestServiceInCatalog = await getService ( service . id , 'latest' ) ;
113+ const existingVersionInCatalog = await getService ( service . id , version ) ;
114+
112115 console . log ( chalk . blue ( `Processing service: ${ document . info . title } (v${ version } )` ) ) ;
113116
117+ // Found a service, and versions do not match, we need to version the one already there
114118 if ( latestServiceInCatalog ) {
115- serviceMarkdown = latestServiceInCatalog . markdown ;
116- serviceSpecificationsFiles = await getSpecificationFilesForService ( service . id , 'latest' ) ;
117- sends = latestServiceInCatalog . sends || ( [ ] as any ) ;
118-
119- // persist any specifications that are already in the catalog
120- serviceSpecifications = {
121- ...serviceSpecifications ,
122- ...latestServiceInCatalog . specifications ,
123- } ;
124-
125- // Found a service, and versions do not match, we need to version the one already there
126- if ( latestServiceInCatalog . version !== version ) {
119+ if ( isHigherVersion ( version , latestServiceInCatalog . version ) ) {
127120 await versionService ( service . id ) ;
128121 console . log ( chalk . cyan ( ` - Versioned previous service (v${ latestServiceInCatalog . version } )` ) ) ;
122+ } else {
123+ serviceCatalogPath = serviceCatalogPath . concat ( `/versioned/${ version } ` ) ;
124+ console . log ( chalk . yellow ( ` - Service (v${ latestServiceInCatalog . version } ) already exists, skipped creation...` ) ) ;
129125 }
126+ }
127+
128+ if ( existingVersionInCatalog ) {
129+ markdown = existingVersionInCatalog . markdown ;
130+ specFiles = await getSpecificationFilesForService ( service . id , version ) ;
131+ sends = existingVersionInCatalog . sends || ( [ ] as any ) ;
132+ receives = [ ...( existingVersionInCatalog . receives ?? [ ] ) , ...receives ] ;
133+
134+ // persist any specifications that are already in the catalog
135+ specifications = {
136+ ...specifications ,
137+ ...existingVersionInCatalog . specifications ,
138+ } ;
130139
131140 // Match found, override it
132- if ( latestServiceInCatalog . version === version ) {
133- receives = latestServiceInCatalog . receives ? [ ...latestServiceInCatalog . receives , ...receives ] : receives ;
134- await rmServiceById ( service . id ) ;
135- }
141+ await rmServiceById ( service . id , version ) ;
136142 }
137143
138144 await writeService (
139145 {
140146 ...service ,
141- markdown : serviceMarkdown ,
142- specifications : serviceSpecifications ,
147+ markdown : markdown ,
148+ specifications : specifications ,
143149 sends,
144150 receives,
145151 } ,
146- { path : service . id }
152+ { path : serviceCatalogPath }
147153 ) ;
148154
149155 // What files need added to the service (speficiation files)
150- const specFiles = [
156+ const existingSpecFiles = [
151157 // add any previous spec files to the list
152- ...serviceSpecificationsFiles ,
158+ ...specFiles ,
153159 {
154160 content : openAPIFile ,
155161 fileName : service . schemaPath ,
156162 } ,
157163 ] ;
158164
159- for ( const specFile of specFiles ) {
165+ for ( const spec of existingSpecFiles ) {
160166 await addFileToService (
161167 service . id ,
162168 {
163- fileName : specFile . fileName ,
164- content : specFile . content ,
169+ fileName : spec . fileName ,
170+ content : spec . content ,
165171 } ,
166172 version
167173 ) ;
@@ -248,3 +254,6 @@ const processMessagesForOpenAPISpec = async (pathToSpec: string, document: OpenA
248254 }
249255 return { receives, sends : [ ] } ;
250256} ;
257+ function isHigherVersion ( sourceVersion : string , targetVersion : string ) {
258+ return semver . gt ( sourceVersion , targetVersion ) ;
259+ }
0 commit comments