Files
OmniRoute/scripts/check/lib/apiRoutes.mjs
backryun d1d75fdbf4 ci(quality): cut PR gate wall time without dropping protection (#6716)
Collapse duplicate CI spend while keeping each gate's existence reason:

- quality.yml: TIA __RUN_ALL__ defers full unit to fast-unit 4-shard (#6781);
  path filters via classify-pr-changes; docs-gates split; draft skip
- ci.yml: wire docs/i18n/code path filters; ESLint JSON artifact for quality-gate;
  drop advisory typecheck:noimplicit; float actions/cache@v6
- TIA parity: memory/usage/combo/serial; **/*.test.mjs any depth; electron/bin
  no longer force unit __RUN_ALL__
- check:complexity-ratchets: one ESLint walk, ruleId-isolated baselines + cache
- check:api-docs-refs + lib/apiRoutes: shared API route inventory
- husky pre-push: intentionally light (gates live in pre-commit); CLAUDE.md +
  QUALITY_GATES.md docs synced
- collect-metrics / lint:json: path.resolve cache path; Windows-safe eslint bin
- env-doc allowlist for ESLINT_RESULTS_JSON / COMPLEXITY_ESLINT_REPORT
- release-green --full-ci expects check:api-docs-refs (not docs-symbols alone)

Tests: select-impacted, classify-pr-changes, api-routes lib, complexity-rule-count,
validate-release-green.

Reconciled after #6781 (fast-unit 2→4 shards) per maintainer request on #6716.

Co-authored-by: Diego Rodrigues de Sa e Souza <8016841+diegosouzapw@users.noreply.github.com>
2026-07-11 08:03:11 -03:00

83 lines
2.5 KiB
JavaScript

/**
* Shared filesystem inventory of Next.js App Router API routes.
*
* Existence reason: openapi-routes (spec→route), docs-symbols (prose→route),
* and openapi-coverage (route→spec %) all need the same walk of src/app/api.
* One collector keeps path normalization consistent and avoids triple walks
* when a combined gate runs them together.
*/
import fs from "node:fs";
import path from "node:path";
/**
* @param {string} [root] repo root
* @returns {string} absolute path to src/app/api
*/
export function apiRoot(root = process.cwd()) {
return path.join(root, "src", "app", "api");
}
/**
* Convert a directory under src/app/api (the folder that contains route.ts)
* to an OpenAPI-style /api/... path.
* Dynamic segments: [id] → {id}, [...slug] → {slug}.
*
* @param {string} routeDir absolute directory containing route.ts
* @param {string} apiRootAbs absolute src/app/api
* @returns {string}
*/
export function toApiUrlPath(routeDir, apiRootAbs) {
const rel = path.relative(apiRootAbs, routeDir).replace(/\\/g, "/");
if (!rel || rel === ".") return "/api";
const normalized = rel
.replace(/\[\.\.\.([^\]]+)\]/g, "{$1}")
.replace(/\[([^\]]+)\]/g, "{$1}");
return `/api/${normalized}`;
}
/**
* Walk src/app/api for route.ts(x) → OpenAPI-style URL paths.
* @param {string} [root]
* @returns {string[]}
*/
export function collectApiRouteUrlPaths(root = process.cwd()) {
const API = apiRoot(root);
if (!fs.existsSync(API)) return [];
const out = [];
function walk(dir) {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
walk(full);
} else if (entry.isFile() && /^route\.tsx?$/.test(entry.name)) {
out.push(toApiUrlPath(path.dirname(full), API));
}
}
}
walk(API);
return out;
}
/**
* Walk src/app/api → relative repo paths to route.ts (docs-symbols resolver).
* @param {string} [root]
* @returns {Set<string>}
*/
export function collectApiRouteFiles(root = process.cwd()) {
const API = apiRoot(root);
const out = new Set();
if (!fs.existsSync(API)) return out;
function walk(dir) {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
walk(full);
} else if (entry.isFile() && /^route\.tsx?$/.test(entry.name)) {
out.add(path.relative(root, full).replace(/\\/g, "/"));
}
}
}
walk(API);
return out;
}