Skip to content

Commit

Permalink
Move check for Astro's pages dir injectPages (#20)
Browse files Browse the repository at this point in the history
* Fix comment

* Move Astro 'page' dir check into `injectPages` function

- Refactor `stringToDir` function to access `srcDir` inside main function
- Tweak error messages
- Remove extra if case that got copy/pasted

* lint
  • Loading branch information
BryceRussell authored Mar 1, 2024
1 parent b7b35f1 commit 1adc442
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-planets-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-pages": patch
---

Defer error when pointing to Astro's 'pages' dir to `injectPages` function
43 changes: 20 additions & 23 deletions package/utils/add-page-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ import type { IntegrationOption } from "../types";
function stringToDir(
option: IntegrationOption,
key: "dir" | "cwd",
base: string,
path?: string,
base?: string,
): string {
const { log, config, logger } = option;

const srcDir = config.srcDir.toString();
const { log, logger } = option;

// Check if path is string
if (key === "dir") {
if (!path || typeof path !== "string")
throw new AstroError(`[astro-pages]: '${key}' is invalid!`, path);
throw new AstroError(`'${key}' path is invalid!`, path);
}

if (!path) path = srcDir;
if (!path) path = base;

// Check if path is a file URL
if (path.startsWith("file:/")) {
Expand All @@ -30,7 +28,7 @@ function stringToDir(

// Check if path is relative
if (!isAbsolute(path)) {
path = resolve(base || srcDir, path);
path = resolve(base, path);
}

// Check if path is a file
Expand All @@ -40,16 +38,9 @@ function stringToDir(
path = dirname(path);
}

// Check if path is pointing to Astro's page directory
if (path === resolve(srcDir, "pages")) {
throw new AstroError(
`[astro-pages]: '${key}' cannot point to Astro's 'pages' directory!`,
);
}

// Check if path exists
if (!existsSync(path)) {
throw new AstroError(`[astro-pages]: '${key}' does not exist!`, path);
throw new AstroError(`'${key}' path does not exist!`, path);
}

return path;
Expand All @@ -62,18 +53,16 @@ export default function addPageDir(options: IntegrationOption) {
glob,
pattern: transformer,
log,
config,
logger,
injectRoute,
} = options;

cwd = stringToDir(options, "cwd", cwd);
const srcDir = fileURLToPath(config.srcDir.toString());

dir = stringToDir(options, "dir", dir, cwd);
cwd = stringToDir(options, "cwd", srcDir, cwd);

// Handle glob default including empty array case
if (!glob || (Array.isArray(glob) && !glob.length)) {
glob = "**.{astro,ts,js}";
}
dir = stringToDir(options, "dir", cwd, dir);

// Handle glob default including empty array case
if (!glob || (Array.isArray(glob) && !glob.length)) {
Expand All @@ -98,7 +87,7 @@ export default function addPageDir(options: IntegrationOption) {

for (const entrypoint of entrypoints) {
const pattern =
entrypoint // Transoform absolute filepath into a route pattern:
entrypoint // Transform absolute filepath into a route pattern:
.slice(dir.length, -extname(entrypoint).length) // Get path between page dir and file extension
.replace(/(\\|\/)index$/, "") || // Remove 'index' from end of path
"/"; // Default to root when replace is falsy
Expand All @@ -107,7 +96,15 @@ export default function addPageDir(options: IntegrationOption) {
}

function injectPages() {
if (log) logger.info("Adding page directory: " + dir);
// Check if directory is pointing to Astro's page directory
if (dir === resolve(srcDir, "pages")) {
throw new AstroError(
`Failed to inject pages! Directory cannot point to Astro's 'pages' directory`,
dir,
);
}

if (log) logger.info("Adding pages: " + dir);

for (let [pattern, entrypoint] of Object.entries(pages)) {
// Transform pattern if available
Expand Down

0 comments on commit 1adc442

Please sign in to comment.