/
mfe-remote.ts
40 lines (33 loc) · 1.19 KB
/
mfe-remote.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import type { Tree } from '@nrwl/devkit';
import type { Schema } from './schema';
import { getProjects, readProjectConfiguration } from '@nrwl/devkit';
import applicationGenerator from '../application/application';
import { getMfeProjects } from '../../utils/get-mfe-projects';
function findNextAvailablePort(tree: Tree) {
const mfeProjects = getMfeProjects(tree);
const ports = new Set<number>([4200]);
for (const mfeProject of mfeProjects) {
const { targets } = readProjectConfiguration(tree, mfeProject);
const port = targets?.serve?.options?.port ?? 4200;
ports.add(port);
}
const nextAvailablePort = Math.max(...ports) + 1;
return nextAvailablePort;
}
export default async function mfeRemote(tree: Tree, options: Schema) {
const projects = getProjects(tree);
if (options.host && !projects.has(options.host)) {
throw new Error(
`The name of the application to be used as the host app does not exist. (${options.host})`
);
}
const installTask = await applicationGenerator(tree, {
...options,
mfe: true,
mfeType: 'remote',
routing: true,
host: options.host,
port: options.port ?? findNextAvailablePort(tree),
});
return installTask;
}