Skip to content

Commit

Permalink
Pass aspect basename as relative base
Browse files Browse the repository at this point in the history
  • Loading branch information
villasv committed Oct 8, 2024
1 parent b0d0db8 commit 2e9f0fc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion projects/null-cms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Placing the `Index` component on a `page.tsx` under `app/some/route`:
import { Index } from "null-cms/components";

export default async function Page() {
return <Index />;
return <Index base=${__dirname} />;
}
```

Expand Down
11 changes: 8 additions & 3 deletions projects/null-cms/components/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import path from "path";
import { listPages } from "@/projects/null-cms/pages";

export interface IndexProps {
/**
* The base folder name used to list all sub pages in the aspect root page.
*/
base?: string;
base: string;
}

function sanitizeBasedir(base: string): string {
Expand All @@ -15,9 +16,13 @@ function sanitizeBasedir(base: string): string {
}

// dirname is evaluated as a parameter at invocation time by the caller module
export async function Index({ base = (() => __dirname)() }: IndexProps) {
export async function Index({ base }: IndexProps) {
const sanitizedBase = sanitizeBasedir(base);
const subPages = await listPages(sanitizedBase, ".", 1);
const rootPath = path.dirname(sanitizedBase);
const relativeBase = path.basename(sanitizedBase);
const subPages = await listPages(rootPath, relativeBase);
console.log(sanitizedBase);
console.log(subPages.map((sp) => sp.relativePath));
return (
<div>
<ol>
Expand Down
15 changes: 11 additions & 4 deletions projects/null-cms/pages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,25 @@ export interface Page {
// TODO: read from next config based on extensions
const NEXT_PAGE_FILES = ["page.tsx", "page.mdx"];

/**
*
* @param basePath the base directory, will not compose the relative path
* @param relativePath
* @param levelSkips will not output anything until recurred this amount of times
* @returns
*/
export async function listPages(
basePath: string,
relativePath: string,
skipLevel: number,
relativePath: string = ".",
levelSkips: number = 1,
): Promise<Page[]> {
const children = await fs.readdir(path.join(basePath, relativePath));
const pages = await Promise.all(
children.map(async (fileName) => {
const childPath = path.join(basePath, relativePath, fileName);
return (await fs.stat(childPath)).isDirectory()
? listPages(basePath, path.join(relativePath, fileName), skipLevel - 1)
: NEXT_PAGE_FILES.includes(fileName) && skipLevel <= 0
? listPages(basePath, path.join(relativePath, fileName), levelSkips - 1)
: NEXT_PAGE_FILES.includes(fileName) && levelSkips <= 0
? [await loadPage({ basePath, relativePath, fileName })]
: [];
}),
Expand Down

0 comments on commit 2e9f0fc

Please sign in to comment.