From 31de6717db7f75f44ac38ba81cbdfae768128638 Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Tue, 19 Apr 2022 11:38:45 +0100 Subject: [PATCH] feat(angular): host generator should create remotes (#9848) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(angular): host generator should create remotes * fix(angular): remove log line Co-authored-by: Leosvel Pérez Espinosa Co-authored-by: Leosvel Pérez Espinosa --- .../angular/src/generators/host/host.spec.ts | 48 ++++++++++++++----- packages/angular/src/generators/host/host.ts | 28 +++++++++-- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/packages/angular/src/generators/host/host.spec.ts b/packages/angular/src/generators/host/host.spec.ts index 51cc23612886a..56756899795e5 100644 --- a/packages/angular/src/generators/host/host.spec.ts +++ b/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 () => { @@ -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'` + ); }); }); diff --git a/packages/angular/src/generators/host/host.ts b/packages/angular/src/generators/host/host.ts index 7254c5b366862..0eb7ef6231437 100644 --- a/packages/angular/src/generators/host/host.ts +++ b/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); } }); } @@ -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; }