Skip to content

Commit

Permalink
Read next.config.js from cli (#204)
Browse files Browse the repository at this point in the history
Fixes #136 and #182
  • Loading branch information
tatethurston authored Oct 9, 2024
1 parent 01b9efd commit 8231dbd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.2.4

- CLI invocation now reads next.config.js or next.config.mjs.

## 2.2.3

- Bug fix: `usePathname` and `useParams` were incorrectly resolving to `any` return types.
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs-routes/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nextjs-routes",
"version": "2.2.3",
"version": "2.2.4-rc.1",
"description": "Type safe routing for Next.js",
"license": "MIT",
"author": "Tate <[email protected]>",
Expand Down
54 changes: 44 additions & 10 deletions packages/nextjs-routes/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,64 @@
#!/usr/bin/env node

import type { NextConfig } from "next";
import { writeNextJSRoutes } from "./core.js";
import { getAppDirectory, getPagesDirectory, isNotUndefined } from "./utils.js";
import { existsSync } from "node:fs";
import { join } from "node:path";
import { cwd } from "node:process";

const logger: Pick<Console, "error" | "info"> = {
error: (str: string) => console.error("[nextjs-routes] " + str),
info: (str: string) => console.info("[nextjs-routes] " + str),
};

function cli(): void {
async function loadNextConfig(dir: string): Promise<NextConfig | undefined> {
const jsPath = join(dir, "next.config.js");
const mjsPath = join(dir, "next.config.mjs");

let path = "";
if (existsSync(jsPath)) {
path = jsPath;
} else if (existsSync(mjsPath)) {
path = mjsPath;
}

if (!path) {
return;
}

logger.info(`Found ${jsPath}`);
const mod = (await import(path)).default;
if (typeof mod == "function") {
return await mod("phase-production-server", {});
}
return mod;
}

async function cli(): Promise<void> {
const dir = cwd();
const config = await loadNextConfig(dir);
if (!config) {
logger.error(
`Could not find a next.config.js or next.config.mjs. Expected to find either in ${dir}.`,
);
process.exit(1);
}

const dirs = [
getPagesDirectory(process.cwd()),
getAppDirectory(process.cwd()),
].filter(isNotUndefined);
if (dirs.length === 0) {
logger.error(`Could not find a Next.js pages directory. Expected to find either 'pages' (1), 'src/pages' (2), or 'app' (3) in your project root.
logger.error(
`Could not find a pages or app directory. Expected to find eitherin ${dir}.`,
);

1. https://nextjs.org/docs/basic-features/pages
2. https://nextjs.org/docs/advanced-features/src-directory
3. https://beta.nextjs.org/docs/routing/fundamentals#the-app-directory
`);
process.exit(1);
} else {
writeNextJSRoutes({});
logger.info("Generated route types.");
}

writeNextJSRoutes(config);
logger.info("Generated route types.");
}

cli();
await cli();

0 comments on commit 8231dbd

Please sign in to comment.