Repository: tatethurston/nextjs-routes
Pull request #29 (2022-07-08 19:34:03)

fix src/pages usage on windows


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ff740f5..019e87f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,7 @@
 
 - re-export types from `next/link` and `next/router`
 - remove prettier as a peer dependency
+- enable src/pages for windows users
 
 ## 0.0.16
 
diff --git a/src/cli.ts b/src/cli.ts
index c45e14e..392b8ec 100644
--- a/src/cli.ts
+++ b/src/cli.ts
@@ -4,14 +4,9 @@ import { writeFileSync } from "fs";
 import { join } from "path";
 import { getPagesDirectory, findFiles, generate, nextRoutes } from "./utils.js";
 
-const NEXTJS_PAGES_DIRECTORY = join(".", getPagesDirectory());
+const pagesDirectory = getPagesDirectory() as string;
+const files = findFiles(join(".", pagesDirectory));
+const routes = nextRoutes(files, pagesDirectory);
+const generated = generate(routes);
 
-function main(): void {
-  const files = findFiles(NEXTJS_PAGES_DIRECTORY);
-  const routes = nextRoutes(files);
-  const generated = generate(routes);
-
-  writeFileSync("nextjs-routes.d.ts", generated);
-}
-
-main();
+writeFileSync("nextjs-routes.d.ts", generated);
diff --git a/src/utils.ts b/src/utils.ts
index a281905..a465c64 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -4,11 +4,13 @@ import { join, parse } from "path";
 const NEXTJS_NON_ROUTABLE_PREFIX = "_";
 const DYNAMIC_SEGMENT_RE = /\[(.*?)\]/g;
 
-export function getPagesDirectory(): string {
-  if (existsSync("pages")) {
-    return "pages";
+export function getPagesDirectory(): string | undefined {
+  const dirs = ["pages", join("src", "pages")];
+  for (const dir of dirs) {
+    if (existsSync(dir)) {
+      return dir;
+    }
   }
-  return "src/pages";
 }
 
 // istanbul ignore next
@@ -36,13 +38,11 @@ function convertWindowsPathToUnix(file: string): string {
   return file.replace(/\\/g, "/");
 }
 
-export function nextRoutes(files: string[]): Route[] {
-  const pagesDirectory = getPagesDirectory();
+export function nextRoutes(files: string[], pagesDirectory: string): Route[] {
   const filenames = files
+    .map((file) => file.replace(pagesDirectory, ""))
+    .map((file) => file.replace(parse(file).ext, ""))
     .map(convertWindowsPathToUnix)
-    .map((file) =>
-      file.replace(pagesDirectory, "").replace(parse(file).ext, "")
-    )
     .filter((file) => !parse(file).name.startsWith(NEXTJS_NON_ROUTABLE_PREFIX));
 
   return filenames.map((filename) => {


Test patch:
diff --git a/src/test.ts b/src/test.ts
index b05915f..9e3d52a 100644
--- a/src/test.ts
+++ b/src/test.ts
@@ -2,8 +2,8 @@ import { nextRoutes, generate } from "./utils.js";
 
 describe("nextRoutes", () => {
   it("transforms windows paths", () => {
-    const pages = ["\\[foo]\\bar\\index.ts"];
-    const { pathname } = nextRoutes(pages)[0];
+    const pages = ["src\\pages\\[foo]\\bar\\index.ts"];
+    const { pathname } = nextRoutes(pages, "src\\pages")[0];
     expect(pathname).toEqual("/[foo]/bar");
   });
 });
@@ -11,29 +11,29 @@ describe("nextRoutes", () => {
 describe("route generation", () => {
   it("typescript", () => {
     const pages = [
-      "/404.ts",
-      "/[foo].ts",
-      "/[foo]/[bar]/[baz].ts",
-      "/[foo]/bar/[baz].ts",
-      "/[foo]/bar/[baz]/foo/[bar].ts",
-      "/[foo]/baz.ts",
-      "/_app.ts",
-      "/_debug.ts",
-      "/_debug/health-check.ts",
-      "/_document.ts",
-      "/_error.ts",
-      "/_error/index.ts",
-      "/api/[[...segments]].ts",
-      "/api/[...segments].ts",
-      "/api/bar.ts",
-      "/foo/[slug].ts",
-      "/index.ts",
-      "/not-found.ts",
-      "/settings/bars/[bar].ts",
-      "/settings/bars/[bar]/baz.ts",
-      "/settings/foo.ts",
-      "/settings/index.ts",
+      "pages/404.ts",
+      "pages/[foo].ts",
+      "pages/[foo]/[bar]/[baz].ts",
+      "pages/[foo]/bar/[baz].ts",
+      "pages/[foo]/bar/[baz]/foo/[bar].ts",
+      "pages/[foo]/baz.ts",
+      "pages/_app.ts",
+      "pages/_debug.ts",
+      "pages/_debug/health-check.ts",
+      "pages/_document.ts",
+      "pages/_error.ts",
+      "pages/_error/index.ts",
+      "pages/api/[[...segments]].ts",
+      "pages/api/[...segments].ts",
+      "pages/api/bar.ts",
+      "pages/foo/[slug].ts",
+      "pages/index.ts",
+      "pages/not-found.ts",
+      "pages/settings/bars/[bar].ts",
+      "pages/settings/bars/[bar]/baz.ts",
+      "pages/settings/foo.ts",
+      "pages/settings/index.ts",
     ];
-    expect(generate(nextRoutes(pages))).toMatchSnapshot();
+    expect(generate(nextRoutes(pages, "pages"))).toMatchSnapshot();
   });
 });