@@ -56,10 +56,10 @@ export async function convertE2EToExample(config: ConvertE2EToExampleConfig) {
5656 const meshConfigTsFile = path . join ( e2eDir , 'mesh.config.ts' ) ;
5757 const composes = await exists ( meshConfigTsFile ) ;
5858 if ( composes ) {
59- console . group ( `"mesh.config.ts" found, transforming...` ) ;
59+ console . group ( `"mesh.config.ts" found, transforming service ports ...` ) ;
6060 using _ = defer ( ( ) => console . groupEnd ( ) ) ;
6161
62- const result = transformMeshConfig (
62+ const result = transformServicePorts (
6363 await fs . readFile ( meshConfigTsFile , 'utf8' ) ,
6464 ) ;
6565 portForService = result . portForService ;
@@ -75,11 +75,11 @@ export async function convertE2EToExample(config: ConvertE2EToExampleConfig) {
7575 relativeServiceFile ;
7676
7777 console . group (
78- `service file "${ relativeServiceFile } " found, transforming...` ,
78+ `service file "${ relativeServiceFile } " found, transforming service ports ...` ,
7979 ) ;
8080 using _ = defer ( ( ) => console . groupEnd ( ) ) ;
8181
82- const result = transformService (
82+ const result = transformServicePorts (
8383 await fs . readFile ( serviceFile , 'utf8' ) ,
8484 portForService ,
8585 ) ;
@@ -266,18 +266,36 @@ export async function convertE2EToExample(config: ConvertE2EToExampleConfig) {
266266 console . log ( 'Ok' ) ;
267267}
268268
269- interface PortForService {
269+ export interface PortForService {
270270 [ service : string ] : number /* port */ ;
271271}
272272
273273/**
274+ * Finds and replaces all service ports in the given source file.
275+ *
276+ * If no {@link portForService} argument is provided, then ports will be auto-assigned
277+ * starting from `4001` and the map of used ports will be returned. Otherwise, the ports
278+ * from {@link portForService} will be used.
279+ *
274280 * @param source - Source code of the `mesh.config.ts` file.
281+ * @param portForService - Map of service names to ports.
275282 */
276- function transformMeshConfig ( source : string ) {
283+ export function transformServicePorts ( source : string ) : {
284+ source : string ;
285+ portForService : PortForService ;
286+ } ;
287+ export function transformServicePorts (
288+ source : string ,
289+ portForService : PortForService ,
290+ ) : { source : string } ;
291+ export function transformServicePorts (
292+ source : string ,
293+ portForService ?: PortForService ,
294+ ) : { source : string ; portForService ?: PortForService } {
277295 const root = j ( source ) ;
278296
279297 const startingServicePort = 4001 ;
280- const portForService : PortForService = { } ;
298+ const autoPortForService : PortForService = { } ;
281299
282300 root
283301 // import '@internal/testing'
@@ -344,101 +362,19 @@ function transformMeshConfig(source: string) {
344362 }
345363
346364 const serviceName = arg0 . value ! . toString ( ) ;
347- const port = startingServicePort + i ;
348- portForService [ serviceName ] = port ;
349-
350- console . log (
351- `Replacing "${ variableName } .getServicePort('${ serviceName } ')" with "${ port } " at ${ loc ( path , true ) } ` ,
352- ) ;
353-
354- j ( path ) . replaceWith ( j . literal ( port ) ) ; // replace opts.portForService('foo') with port literal
355- } ) ;
356- } )
357- . remove ( ) ; // remove all const opts = Opts()
358- } ) ;
359- } )
360- . remove ( ) ; // remove all import '@internal/testing'
361-
362- return { source : root . toSource ( ) , portForService } ;
363- }
364-
365- /**
366- * @param source - Source code of the `mesh.config.ts` file.
367- * @param portForService - Map of service names to ports.
368- */
369- function transformService ( source : string , portForService : PortForService ) {
370- const root = j ( source ) ;
371-
372- root
373- // import '@internal/testing'
374- . find ( j . ImportDeclaration , {
375- source : {
376- value : '@internal/testing' ,
377- } ,
378- } )
379- . forEach ( ( path ) => {
380- console . group (
381- `Processing "@internal/testing" import at ${ loc ( path ) } , will remove` ,
382- ) ;
383- using _ = defer ( ( ) => console . groupEnd ( ) ) ;
384-
385- path . node . specifiers
386- // import { Opts } from '@internal/testing'
387- ?. filter ( ( s ) => 'imported' in s && s . imported . name === 'Opts' )
388- . forEach ( ( s , i ) => {
389- console . group (
390- `Processing imported "Opts" #${ i + 1 } (as "${ s . local ! . name } ")` ,
391- ) ;
392- using _ = defer ( ( ) => console . groupEnd ( ) ) ;
393-
394- root
395- // const opts = Opts()
396- . find ( j . VariableDeclarator , {
397- init : {
398- callee : {
399- name : s . local ! . name ,
400- } ,
401- } ,
402- } )
403- . forEach ( ( path ) => {
404- if ( path . node . id . type !== 'Identifier' ) {
405- throw new Error (
406- `Expected "Opts()" to declare a node of type "Identifier", but got "${ path . node . id . type } "` ,
407- ) ;
408- }
409-
410- const variableName = path . node . id . name ;
411- console . group (
412- `Variable "${ variableName } " declared with "Opts()" at ${ loc ( path ) } ` ,
413- ) ;
414- using _ = defer ( ( ) => console . groupEnd ( ) ) ;
415-
416- root
417- // opts.getServicePort()
418- . find ( j . CallExpression , {
419- callee : {
420- object : {
421- name : variableName ,
422- } ,
423- property : {
424- name : 'getServicePort' ,
425- } ,
426- } ,
427- } )
428- . forEach ( ( path ) => {
429- const arg0 = path . node . arguments [ 0 ] ;
430- if ( arg0 ?. type !== 'Literal' ) {
431- throw new Error (
432- 'TODO: get variable value when literal is not used in "opts.getServicePort" argument' ,
433- ) ;
434- }
435365
436- const serviceName = arg0 . value ! . toString ( ) ;
437- const port = portForService [ serviceName ] ;
438- if ( ! port ) {
439- throw new Error (
440- `Port for service "${ serviceName } " not found` ,
441- ) ;
366+ let port : number ;
367+ if ( portForService ) {
368+ const foundPort = portForService [ serviceName ] ;
369+ if ( ! foundPort ) {
370+ throw new Error (
371+ `Port for service "${ serviceName } " not found` ,
372+ ) ;
373+ }
374+ port = foundPort ;
375+ } else {
376+ port = startingServicePort + i ;
377+ autoPortForService [ serviceName ] = port ;
442378 }
443379
444380 console . log (
@@ -453,5 +389,8 @@ function transformService(source: string, portForService: PortForService) {
453389 } )
454390 . remove ( ) ; // remove all import '@internal/testing'
455391
392+ if ( ! portForService ) {
393+ return { source : root . toSource ( ) , portForService : autoPortForService } ;
394+ }
456395 return { source : root . toSource ( ) } ;
457396}
0 commit comments