From 69791330eb19abb10034cb1d151ceebcea40e66c Mon Sep 17 00:00:00 2001 From: Nicholas Cunningham Date: Thu, 14 Dec 2023 14:22:57 -0700 Subject: [PATCH] fix(nextjs): enhance page generator to work when --project is not supplied --- .../next/src/generators/page/page.spec.ts | 3 ++- packages/next/src/generators/page/page.ts | 24 +++++++++++++++++-- packages/next/src/generators/page/schema.d.ts | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/next/src/generators/page/page.spec.ts b/packages/next/src/generators/page/page.spec.ts index 30e2a693d59ac..4eb29d5b2a465 100644 --- a/packages/next/src/generators/page/page.spec.ts +++ b/packages/next/src/generators/page/page.spec.ts @@ -64,8 +64,9 @@ describe('component', () => { it('should generate component in app directory', async () => { await pageGenerator(tree, { name: 'about', - project: appRouterProjectName, + directory: `${appRouterProjectName}/app/about`, style: 'css', + nameAndDirectoryFormat: 'as-provided', }); expect( diff --git a/packages/next/src/generators/page/page.ts b/packages/next/src/generators/page/page.ts index 8b815e93555a3..8e407c92c7397 100644 --- a/packages/next/src/generators/page/page.ts +++ b/packages/next/src/generators/page/page.ts @@ -52,19 +52,39 @@ export async function pageGeneratorInternal(host: Tree, schema: Schema) { async function normalizeOptions(host: Tree, options: Schema) { let isAppRouter: boolean; let derivedDirectory: string; + let routerDirectory: string; if (options.project) { // Legacy behavior, detect app vs page router from specified project. // TODO(v18): remove this logic const project = readProjectConfiguration(host, options.project); // app/ is a reserved folder in nextjs so it is safe to check it's existence - isAppRouter = host.exists(`${project.root}/app`); + isAppRouter = + host.exists(`${project.root}/app`) || + host.exists(`${project.root}/src/app`); - const routerDirectory = isAppRouter ? 'app' : 'pages'; + routerDirectory = isAppRouter ? 'app' : 'pages'; derivedDirectory = options.directory ? `${routerDirectory}/${options.directory}` : `${routerDirectory}`; } else { + // Get the project name first so we can determine the router directory + const { project: determinedProjectName } = + await determineArtifactNameAndDirectoryOptions(host, { + artifactType: 'page', + callingGenerator: '@nx/next:page', + name: options.name, + directory: options.directory, + }); + + const project = readProjectConfiguration(host, determinedProjectName); + + // app/ is a reserved folder in nextjs so it is safe to check it's existence + isAppRouter = + host.exists(`${project.root}/app`) || + host.exists(`${project.root}/src/app`); + + routerDirectory = isAppRouter ? 'app' : 'pages'; // New behavior, use directory as is without detecting whether we're using app or pages router. derivedDirectory = options.directory; } diff --git a/packages/next/src/generators/page/schema.d.ts b/packages/next/src/generators/page/schema.d.ts index d2402956119ef..687c6c30f7148 100644 --- a/packages/next/src/generators/page/schema.d.ts +++ b/packages/next/src/generators/page/schema.d.ts @@ -5,7 +5,7 @@ export interface Schema { /** * @deprecated Provide the `directory` option instead and use the `as-provided` format. The project will be determined from the directory provided. It will be removed in Nx v18. */ - project: string; + project?: string; style: SupportedStyles; directory?: string; fileName?: string;