Skip to content

Commit

Permalink
feat(angular): automatically set next remote port (#9422)
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Mar 21, 2022
1 parent 99d871a commit 47697c6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/angular/src/generators/mfe-remote/mfe-remote.spec.ts
@@ -1,6 +1,7 @@
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import mfeRemote from './mfe-remote';
import applicationGenerator from '../application/application';
import { readProjectConfiguration } from '@nrwl/devkit';

describe('MFE Remote App Generator', () => {
it('should generate a remote mfe app with no host', async () => {
Expand Down Expand Up @@ -58,4 +59,22 @@ describe('MFE Remote App Generator', () => {
);
}
});

it('should generate a remote mfe app and automatically find the next port available', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace(2);
await mfeRemote(tree, {
name: 'existing',
port: 4201,
});

// ACT
await mfeRemote(tree, {
name: 'test',
});

// ASSERT
const project = readProjectConfiguration(tree, 'test');
expect(project.targets.serve.options.port).toEqual(4202);
});
});
20 changes: 18 additions & 2 deletions packages/angular/src/generators/mfe-remote/mfe-remote.ts
@@ -1,7 +1,23 @@
import type { Tree } from '@nrwl/devkit';
import type { Schema } from './schema';
import { getProjects } from '@nrwl/devkit';
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>();
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);
Expand All @@ -17,7 +33,7 @@ export default async function mfeRemote(tree: Tree, options: Schema) {
mfeType: 'remote',
routing: true,
host: options.host,
port: options.port ?? 4200,
port: options.port ?? findNextAvailablePort(tree),
});

return installTask;
Expand Down

0 comments on commit 47697c6

Please sign in to comment.