Skip to content

Commit

Permalink
fix(nextjs): enhance page generator to work when --project is not sup…
Browse files Browse the repository at this point in the history
…plied
  • Loading branch information
ndcunningham committed Dec 18, 2023
1 parent aaf3e54 commit 6979133
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/next/src/generators/page/page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
24 changes: 22 additions & 2 deletions packages/next/src/generators/page/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/generators/page/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6979133

Please sign in to comment.