Skip to content

Commit

Permalink
feat(angular): host generator should create remotes (#9848)
Browse files Browse the repository at this point in the history
* feat(angular): host generator should create remotes

* fix(angular): remove log line

Co-authored-by: Leosvel Pérez Espinosa <leosvel.perez.espinosa@gmail.com>

Co-authored-by: Leosvel Pérez Espinosa <leosvel.perez.espinosa@gmail.com>
  • Loading branch information
Coly010 and leosvelperez committed Apr 19, 2022
1 parent c560c80 commit 31de671
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
48 changes: 36 additions & 12 deletions packages/angular/src/generators/host/host.spec.ts
@@ -1,6 +1,7 @@
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import host from './host';
import applicationGenerator from '../application/application';
import remote from '../remote/remote';

describe('Host App Generator', () => {
it('should generate a host app with no remotes', async () => {
Expand Down Expand Up @@ -41,21 +42,44 @@ describe('Host App Generator', () => {
expect(tree.read('apps/test/webpack.config.js', 'utf-8')).toMatchSnapshot();
});

it('should error when a host app is attempted to be generated with an incorrect remote', async () => {
it('should generate a host and any remotes that dont exist', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace(2);

// ACT
try {
await host(tree, {
name: 'test',
remotes: ['remote'],
});
} catch (error) {
// ASSERT
expect(error.message).toEqual(
'Could not find specified remote application (remote)'
);
}

await host(tree, {
name: 'hostApp',
remotes: ['remote1', 'remote2'],
});

// ASSERT
expect(tree.exists('apps/remote1/project.json')).toBeTruthy();
expect(tree.exists('apps/remote2/project.json')).toBeTruthy();
expect(tree.read('apps/host-app/mfe.config.js', 'utf-8')).toContain(
`'remote1','remote2'`
);
});

it('should generate a host, integrate existing remotes and generate any remotes that dont exist', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace(2);
await remote(tree, {
name: 'remote1',
});

// ACT
await host(tree, {
name: 'hostApp',
remotes: ['remote1', 'remote2', 'remote3'],
});

// ASSERT
expect(tree.exists('apps/remote1/project.json')).toBeTruthy();
expect(tree.exists('apps/remote2/project.json')).toBeTruthy();
expect(tree.exists('apps/remote3/project.json')).toBeTruthy();
expect(tree.read('apps/host-app/mfe.config.js', 'utf-8')).toContain(
`'remote1','remote2','remote3'`
);
});
});
28 changes: 23 additions & 5 deletions packages/angular/src/generators/host/host.ts
@@ -1,18 +1,22 @@
import type { Tree } from '@nrwl/devkit';
import { formatFiles, names, Tree } from '@nrwl/devkit';
import type { Schema } from './schema';

import { getProjects } from '@nrwl/devkit';
import applicationGenerator from '../application/application';
import remoteGenerator from '../remote/remote';

export default async function host(tree: Tree, options: Schema) {
const projects = getProjects(tree);

const remotesToGenerate: string[] = [];
const remotesToIntegrate: string[] = [];

if (options.remotes && options.remotes.length > 0) {
options.remotes.forEach((remote) => {
if (!projects.has(remote)) {
throw new Error(
`Could not find specified remote application (${remote})`
);
remotesToGenerate.push(remote);
} else {
remotesToIntegrate.push(remote);
}
});
}
Expand All @@ -22,10 +26,24 @@ export default async function host(tree: Tree, options: Schema) {
mfe: true,
mfeType: 'host',
routing: true,
remotes: options.remotes ?? [],
remotes: remotesToIntegrate ?? [],
port: 4200,
federationType: options.dynamic ? 'dynamic' : 'static',
skipFormat: true,
});

for (const remote of remotesToGenerate) {
await remoteGenerator(tree, {
...options,
name: remote,
host: names(options.name).fileName,
skipFormat: true,
});
}

if (!options.skipFormat) {
await formatFiles(tree);
}

return installTask;
}

0 comments on commit 31de671

Please sign in to comment.