UNPKG

next-affected

Version:

CLI tool to list Next.js pages affected by changes

50 lines (49 loc) 2.07 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isPage = isPage; exports.getRouteFromPage = getRouteFromPage; exports.normalizePath = normalizePath; exports.shouldExcludeModule = shouldExcludeModule; const path_1 = __importDefault(require("path")); function isPage(modulePath, projectDir, config) { const normalizedPath = normalizePath(modulePath); const pagesDirs = config.pagesDirectories.map((dir) => normalizePath(path_1.default.join(projectDir, dir))); return pagesDirs.some((dir) => normalizedPath.startsWith(dir)); } function getRouteFromPage(pagePath, projectDir, config) { const normalizedPagePath = normalizePath(pagePath); const pagesDirs = config.pagesDirectories.map((dir) => normalizePath(path_1.default.join(projectDir, dir))); let route = normalizedPagePath; for (const dir of pagesDirs) { if (normalizedPagePath.startsWith(dir)) { route = normalizedPagePath.slice(dir.length); break; } } route = route.replace(/\.(js|jsx|ts|tsx)$/, ""); return route || "/"; } function normalizePath(filePath) { const absolutePath = path_1.default.resolve(filePath); return absolutePath?.replace(/\\/g, "/"); } function shouldExcludeModule(modulePath, config, projectDir) { // Check if the module has an excluded extension if (config.excludedExtensions.some((ext) => modulePath.endsWith(ext))) { return true; } const normalizedModulePath = normalizePath(modulePath); // Check if the module is within any of the excluded paths if (config.excludedPaths) { for (const excludedPath of config.excludedPaths) { const normalizedExcludedPath = normalizePath(path_1.default.resolve(projectDir, excludedPath)); if (normalizedModulePath.startsWith(normalizedExcludedPath)) { return true; } } } return false; }