From 4b6f15112a528b0ff9e3fb3f7284ee4d6046e0a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=92riks=20Lapi=C5=86=C5=A1?= Date: Mon, 22 Jan 2024 23:19:34 +0200 Subject: [PATCH] prettier lint --- src/runtime/route-rules.ts | 2 +- src/runtime/routes/openapi.ts | 4 +- src/runtime/virtual/server-handlers.d.ts | 7 +- src/scan.ts | 105 +++++++++++++---------- src/types/handler.ts | 2 +- 5 files changed, 67 insertions(+), 53 deletions(-) diff --git a/src/runtime/route-rules.ts b/src/runtime/route-rules.ts index 8848841bc8..41ebdca76d 100644 --- a/src/runtime/route-rules.ts +++ b/src/runtime/route-rules.ts @@ -72,5 +72,5 @@ export function getRouteRulesForPath(path: string): NitroRouteRules { } export function defineRouteMeta(meta: ServerRouteMeta) { - return meta + return meta; } diff --git a/src/runtime/routes/openapi.ts b/src/runtime/routes/openapi.ts index 562424e231..7ee194964b 100644 --- a/src/runtime/routes/openapi.ts +++ b/src/runtime/routes/openapi.ts @@ -58,8 +58,8 @@ function getPaths(): PathsObject { if (h.meta && h.meta.openAPI) { paths[route][method] = { ...paths[route][method], - ...h.meta.openAPI - } + ...h.meta.openAPI, + }; } } diff --git a/src/runtime/virtual/server-handlers.d.ts b/src/runtime/virtual/server-handlers.d.ts index 2e56b53529..9ef35a34fa 100644 --- a/src/runtime/virtual/server-handlers.d.ts +++ b/src/runtime/virtual/server-handlers.d.ts @@ -1,11 +1,10 @@ import type { H3EventHandler, LazyEventHandler, RouterMethod } from "h3"; import type { OperationObject } from "openapi-typescript"; - export type ServerRouteMeta = { - openAPI?: OperationObject, - [key: string] : any, -} + openAPI?: OperationObject; + [key: string]: any; +}; export type HandlerDefinition = { route: string; diff --git a/src/scan.ts b/src/scan.ts index a37ce986bf..4ced45d78b 100644 --- a/src/scan.ts +++ b/src/scan.ts @@ -1,17 +1,17 @@ import fs from "node:fs"; import { runInNewContext } from "node:vm"; import { relative, join } from "pathe"; -import acorn from 'acorn'; +import acorn from "acorn"; import { transform } from "esbuild"; import { CallExpression, Node } from "estree"; -import { walk } from 'estree-walker'; +import { walk } from "estree-walker"; import { globby } from "globby"; import { withBase, withLeadingSlash, withoutTrailingSlash } from "ufo"; import type { Nitro } from "./types"; import type { ServerRouteMeta } from "#internal/nitro/virtual/server-handlers"; export const GLOB_SCAN_PATTERN = "**/*.{js,mjs,cjs,ts,mts,cts,tsx,jsx}"; -type FileInfo = { path: string; fullPath: string}; +type FileInfo = { path: string; fullPath: string }; const httpMethodRegex = /\.(connect|delete|get|head|options|patch|post|put|trace)$/; @@ -54,34 +54,35 @@ export async function scanServerRoutes( prefix = "/" ) { const files = await scanFiles(nitro, dir); - return await Promise.all(files.map(async (file) => { - let route = file.path - .replace(/\.[A-Za-z]+$/, "") - .replace(/\[\.{3}]/g, "**") - .replace(/\[\.{3}(\w+)]/g, "**:$1") - .replace(/\[(\w+)]/g, ":$1"); - route = withLeadingSlash(withoutTrailingSlash(withBase(route, prefix))); - - const meta = await scanRouteMeta(file.fullPath) - - let method; - const methodMatch = route.match(httpMethodRegex); - if (methodMatch) { - route = route.slice(0, Math.max(0, methodMatch.index)); - method = methodMatch[1]; - } + return await Promise.all( + files.map(async (file) => { + let route = file.path + .replace(/\.[A-Za-z]+$/, "") + .replace(/\[\.{3}]/g, "**") + .replace(/\[\.{3}(\w+)]/g, "**:$1") + .replace(/\[(\w+)]/g, ":$1"); + route = withLeadingSlash(withoutTrailingSlash(withBase(route, prefix))); + + const meta = await scanRouteMeta(file.fullPath); + + let method; + const methodMatch = route.match(httpMethodRegex); + if (methodMatch) { + route = route.slice(0, Math.max(0, methodMatch.index)); + method = methodMatch[1]; + } - route = route.replace(/\/index$/, "") || "/"; + route = route.replace(/\/index$/, "") || "/"; - return { - handler: file.fullPath, - lazy: true, - middleware: false, - route, - method, - meta - }; - }) + return { + handler: file.fullPath, + lazy: true, + middleware: false, + route, + method, + meta, + }; + }) ); } @@ -135,32 +136,46 @@ async function scanDir( } export async function scanRouteMeta( - fullPath: string, + fullPath: string ): Promise { - const file = fs.readFileSync(fullPath, { encoding: 'utf8', flag: 'r' }); - - let routeMeta: ServerRouteMeta | null = null; + const file = fs.readFileSync(fullPath, { encoding: "utf8", flag: "r" }); - const js = await transform(file, { loader: 'ts' }); - const fileAST = acorn.parse(js.code, { ecmaVersion: "latest", sourceType: "module" }) as Node; + let routeMeta: ServerRouteMeta | null = null; + const js = await transform(file, { loader: "ts" }); + const fileAST = acorn.parse(js.code, { + ecmaVersion: "latest", + sourceType: "module", + }) as Node; walk(fileAST, { enter(_node) { - if (_node.type !== "CallExpression" || (_node as CallExpression).callee.type !== 'Identifier') { return } - const node = _node as CallExpression & { start: number, end: number}; - const name = 'name' in node.callee && node.callee.name + if ( + _node.type !== "CallExpression" || + (_node as CallExpression).callee.type !== "Identifier" + ) { + return; + } + const node = _node as CallExpression & { start: number; end: number }; + const name = "name" in node.callee && node.callee.name; if (name === "defineRouteMeta") { - const metaString = js.code.slice(node.start, node.end) + const metaString = js.code.slice(node.start, node.end); try { - routeMeta = JSON.parse(runInNewContext(metaString.replace('defineRouteMeta', 'JSON.stringify'), {})) + routeMeta = JSON.parse( + runInNewContext( + metaString.replace("defineRouteMeta", "JSON.stringify"), + {} + ) + ); } catch { - throw new Error("[nitro] Error parsing route meta. They should be JSON-serializable") + throw new Error( + "[nitro] Error parsing route meta. They should be JSON-serializable" + ); } } - } - }) + }, + }); - return routeMeta -} \ No newline at end of file + return routeMeta; +} diff --git a/src/types/handler.ts b/src/types/handler.ts index 255e60135d..436aa2e00b 100644 --- a/src/types/handler.ts +++ b/src/types/handler.ts @@ -30,7 +30,7 @@ export interface NitroEventHandler { * Router method matcher */ method?: string; - + /** * Route meta */