Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(angular): automatically set next remote port #9422

Merged
merged 1 commit into from Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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