From b9e671c11d77e2c47880f0cf60bde64c5d2aa449 Mon Sep 17 00:00:00 2001 From: Nicholas Cunningham Date: Wed, 18 Oct 2023 18:29:13 -0600 Subject: [PATCH] fix(react): Error when invalid path is provided to federate-module generator (#19720) --- .../packages/react/generators/federate-module.json | 2 +- .../generators/federate-module/federate-module.ts | 2 +- .../federate-module/federate-module.spec.ts | 12 ++++++++++++ .../generators/federate-module/federate-module.ts | 7 +++++++ .../react/src/generators/federate-module/schema.json | 2 +- 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/docs/generated/packages/react/generators/federate-module.json b/docs/generated/packages/react/generators/federate-module.json index 0fe80424ead41..bd916aeb29955 100644 --- a/docs/generated/packages/react/generators/federate-module.json +++ b/docs/generated/packages/react/generators/federate-module.json @@ -25,7 +25,7 @@ }, "path": { "type": "string", - "description": "The path to locate the federated module.", + "description": "The path to locate the federated module. This path should be relative to the workspace root and the file should exist.", "x-prompt": "What is the path to the module to be federated?" }, "remote": { diff --git a/packages/angular/src/generators/federate-module/federate-module.ts b/packages/angular/src/generators/federate-module/federate-module.ts index e8637789ed92b..b03f567c3b8d4 100644 --- a/packages/angular/src/generators/federate-module/federate-module.ts +++ b/packages/angular/src/generators/federate-module/federate-module.ts @@ -15,7 +15,7 @@ import { export async function federateModuleGenerator(tree: Tree, schema: Schema) { if (!tree.exists(schema.path)) { - throw new Error(stripIndents`The "path" provided does not exist. Please verify the path is correct and pointing to a file that exists in the workspace. + throw new Error(stripIndents`The "path" provided does not exist. Please verify the path is correct and pointing to a file that exists in the workspace. Path: ${schema.path}`); } diff --git a/packages/react/src/generators/federate-module/federate-module.spec.ts b/packages/react/src/generators/federate-module/federate-module.spec.ts index 12ce7b19b8747..2e2ccfc00c0de 100644 --- a/packages/react/src/generators/federate-module/federate-module.spec.ts +++ b/packages/react/src/generators/federate-module/federate-module.spec.ts @@ -17,6 +17,7 @@ describe('federate-module', () => { beforeAll(() => { tree = createTreeWithEmptyWorkspace(); + tree.write('my-remote/src/my-federated-module.ts', ''); // Ensure that the file exists }); describe('no remote', () => { it('should generate a remote and e2e', async () => { @@ -46,6 +47,17 @@ describe('federate-module', () => { tsconfig.compilerOptions.paths['my-remote/my-federated-module'] ).toEqual(['my-remote/src/my-federated-module.ts']); }); + + it('should error when invalid path is provided', async () => { + await federateModuleGenerator(tree, { + ...schema, + path: 'invalid/path', + }).catch((e) => { + expect(e.message).toContain( + 'The "path" provided does not exist. Please verify the path is correct and pointing to a file that exists in the workspace.' + ); + }); + }); }); describe('with remote', () => { diff --git a/packages/react/src/generators/federate-module/federate-module.ts b/packages/react/src/generators/federate-module/federate-module.ts index 627a0047274bd..e9fe3740c5db8 100644 --- a/packages/react/src/generators/federate-module/federate-module.ts +++ b/packages/react/src/generators/federate-module/federate-module.ts @@ -5,6 +5,7 @@ import { logger, readJson, runTasksInSerial, + stripIndents, } from '@nx/devkit'; import { Schema } from './schema'; @@ -14,6 +15,12 @@ import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/pr import { addTsConfigPath, getRootTsConfigPathInTree } from '@nx/js'; export async function federateModuleGenerator(tree: Tree, schema: Schema) { + // Check if the file exists + if (!tree.exists(schema.path)) { + throw new Error(stripIndents`The "path" provided does not exist. Please verify the path is correct and pointing to a file that exists in the workspace. + + Path: ${schema.path}`); + } const tasks: GeneratorCallback[] = []; // Check remote exists const remote = checkRemoteExists(tree, schema.remote); diff --git a/packages/react/src/generators/federate-module/schema.json b/packages/react/src/generators/federate-module/schema.json index ebeda7758cc60..e74868b47fb36 100644 --- a/packages/react/src/generators/federate-module/schema.json +++ b/packages/react/src/generators/federate-module/schema.json @@ -25,7 +25,7 @@ }, "path": { "type": "string", - "description": "The path to locate the federated module.", + "description": "The path to locate the federated module. This path should be relative to the workspace root and the file should exist.", "x-prompt": "What is the path to the module to be federated?" }, "remote": {