Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: env suffix for plugins/middlewares/tasks #2922

Draft
wants to merge 4 commits into
base: v2
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions src/core/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import type { Nitro } from "nitropack/types";
import { join, relative } from "pathe";
import { withBase, withLeadingSlash, withoutTrailingSlash } from "ufo";

export const GLOB_SCAN_PATTERN = "**/*.{js,mjs,cjs,ts,mts,cts,tsx,jsx}";
type FileInfo = { path: string; fullPath: string };

const suffixRegex =
/(\.(?<method>connect|delete|get|head|options|patch|post|put|trace))?(\.(?<env>dev|prod|prerender))?$/;

// prettier-ignore
type MatchedMethodSuffix = "connect" | "delete" | "get" | "head" | "options" | "patch" | "post" | "put" | "trace";
type MatchedEnvSuffix = "dev" | "prod" | "prerender";
type FileInfo = {
path: string;
fullPath: string;
method?: MatchedMethodSuffix;
env?: MatchedEnvSuffix;
};

export const GLOB_SCAN_PATTERN = "**/*.{js,mjs,cjs,ts,mts,cts,tsx,jsx}";
const suffixRegex =
/(\.(?<method>connect|delete|get|head|options|patch|post|put|trace))?(\.(?<env>dev|prod|prerender))?$/;

export async function scanAndSyncOptions(nitro: Nitro) {
// Scan plugins
Expand Down Expand Up @@ -82,6 +86,7 @@ export async function scanMiddleware(nitro: Nitro) {
return {
middleware: true,
handler: file.fullPath,
env: file.env,
};
});
}
Expand All @@ -100,25 +105,15 @@ export async function scanServerRoutes(
.replace(/\[\.{3}(\w+)]/g, "**:$1")
.replace(/\[([^/\]]+)]/g, ":$1");
route = withLeadingSlash(withoutTrailingSlash(withBase(route, prefix)));

const suffixMatch = route.match(suffixRegex);
let method: MatchedMethodSuffix | undefined;
let env: MatchedEnvSuffix | undefined;
if (suffixMatch?.index && suffixMatch?.index >= 0) {
route = route.slice(0, suffixMatch.index);
method = suffixMatch.groups?.method as MatchedMethodSuffix | undefined;
env = suffixMatch.groups?.env as MatchedEnvSuffix | undefined;
}

route = route.replace(/\/index$/, "") || "/";

return {
handler: file.fullPath,
lazy: true,
middleware: false,
route,
method,
env,
method: file.method,
env: file.env,
};
});
}
Expand All @@ -135,7 +130,7 @@ export async function scanTasks(nitro: Nitro) {
.replace(/\/index$/, "")
.replace(/\.[A-Za-z]+$/, "")
.replace(/\//g, ":");
return { name, handler: f.fullPath };
return { name, handler: f.fullPath, env: f.env };
});
}

Expand Down Expand Up @@ -164,9 +159,21 @@ async function scanDir(
});
return fileNames
.map((fullPath) => {
const path = relative(join(dir, name), fullPath);
const suffixMatch = path.match(suffixRegex);
sandros94 marked this conversation as resolved.
Show resolved Hide resolved
let method: MatchedMethodSuffix | undefined;
let env: MatchedEnvSuffix | undefined;

if (suffixMatch?.index && suffixMatch?.index >= 0) {
method = suffixMatch.groups?.method as MatchedMethodSuffix | undefined;
env = suffixMatch.groups?.env as MatchedEnvSuffix | undefined;
}

return {
fullPath,
path: relative(join(dir, name), fullPath),
path,
method,
env,
};
})
.sort((a, b) => a.path.localeCompare(b.path));
Expand Down
Loading