From a020f1d93382896f546c843d04b4fc0f549887e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Wed, 20 Sep 2023 15:07:47 +0200 Subject: [PATCH] feat(misc): do not prompt for project name and root format when they produce the same result (#19221) --- .../project-name-and-root-utils.spec.ts | 27 +++++++++++++++++++ .../generators/project-name-and-root-utils.ts | 4 +++ 2 files changed, 31 insertions(+) diff --git a/packages/devkit/src/generators/project-name-and-root-utils.spec.ts b/packages/devkit/src/generators/project-name-and-root-utils.spec.ts index 793d76b00159e..fe31b01319092 100644 --- a/packages/devkit/src/generators/project-name-and-root-utils.spec.ts +++ b/packages/devkit/src/generators/project-name-and-root-utils.spec.ts @@ -394,6 +394,33 @@ describe('determineProjectNameAndRootOptions', () => { // restore original interactive mode restoreOriginalInteractiveMode(); }); + + it('should not prompt when the resulting name and root are the same for both formats', async () => { + // simulate interactive mode + ensureInteractiveMode(); + const promptSpy = jest.spyOn(enquirer, 'prompt'); + + const result = await determineProjectNameAndRootOptions(tree, { + name: 'libName', + projectType: 'library', + callingGenerator: '', + }); + + expect(promptSpy).not.toHaveBeenCalled(); + expect(result).toEqual({ + projectName: 'lib-name', + names: { + projectSimpleName: 'lib-name', + projectFileName: 'lib-name', + }, + importPath: '@proj/lib-name', + projectRoot: 'lib-name', + projectNameAndRootFormat: 'as-provided', + }); + + // restore original interactive mode + restoreOriginalInteractiveMode(); + }); }); describe('with layout', () => { diff --git a/packages/devkit/src/generators/project-name-and-root-utils.ts b/packages/devkit/src/generators/project-name-and-root-utils.ts index a177f57dbe59e..d9639be3efe56 100644 --- a/packages/devkit/src/generators/project-name-and-root-utils.ts +++ b/packages/devkit/src/generators/project-name-and-root-utils.ts @@ -155,6 +155,10 @@ async function determineFormat( Root: ${formats['derived'].projectRoot}`; const derivedSelectedValue = `${formats['derived'].projectName} @ ${formats['derived'].projectRoot}`; + if (asProvidedSelectedValue === derivedSelectedValue) { + return 'as-provided'; + } + const result = await prompt<{ format: ProjectNameAndRootFormat }>({ type: 'select', name: 'format',