Skip to content

Commit cf45fbd

Browse files
committed
transform service ports and begin with testing
1 parent 2b68e39 commit cf45fbd

File tree

4 files changed

+88
-102
lines changed

4 files changed

+88
-102
lines changed

internal/examples/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"devDependencies": {
1010
"@types/jscodeshift": "^0.12.0",
1111
"@whatwg-node/disposablestack": "^0.0.5",
12+
"dedent": "^1.5.3",
1213
"glob": "^11.0.0",
1314
"jscodeshift": "^17.1.1",
1415
"tsx": "^4.19.2"

internal/examples/src/convert.ts

Lines changed: 40 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import dedent from 'dedent';
2+
import { expect, it } from 'vitest';
3+
import { PortForService, transformServicePorts } from '../src/convert';
4+
5+
it.each([
6+
{
7+
name: 'declaring opts variable',
8+
source: dedent`
9+
import { Opts } from '@internal/testing';
10+
const opts = Opts();
11+
const port = opts.getServicePort('foo');
12+
`,
13+
auto: {
14+
result: {
15+
source: dedent`
16+
const port = 4001;
17+
`,
18+
portForService: {
19+
foo: 4001,
20+
} satisfies PortForService,
21+
},
22+
},
23+
manual: {
24+
portForService: {
25+
foo: 5001,
26+
} satisfies PortForService,
27+
result: {
28+
source: dedent`
29+
const port = 5001;
30+
`,
31+
},
32+
},
33+
},
34+
])('should transform service ports $name', ({ source, auto, manual }) => {
35+
// auto
36+
const actualAutoResult = transformServicePorts(source);
37+
expect(actualAutoResult).toEqual(auto.result);
38+
39+
// manual
40+
const actualManualResult = transformServicePorts(
41+
source,
42+
manual.portForService,
43+
);
44+
expect(actualManualResult).toEqual(manual.result);
45+
});

yarn.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4538,6 +4538,7 @@ __metadata:
45384538
dependencies:
45394539
"@types/jscodeshift": "npm:^0.12.0"
45404540
"@whatwg-node/disposablestack": "npm:^0.0.5"
4541+
dedent: "npm:^1.5.3"
45414542
glob: "npm:^11.0.0"
45424543
jscodeshift: "npm:^17.1.1"
45434544
tsx: "npm:^4.19.2"
@@ -9134,7 +9135,7 @@ __metadata:
91349135
languageName: node
91359136
linkType: hard
91369137

9137-
"dedent@npm:^1.0.0":
9138+
"dedent@npm:^1.0.0, dedent@npm:^1.5.3":
91389139
version: 1.5.3
91399140
resolution: "dedent@npm:1.5.3"
91409141
peerDependencies:

0 commit comments

Comments
 (0)