find-page-file.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.findPageFile = findPageFile;
  6. exports.isLayoutsLeafPage = isLayoutsLeafPage;
  7. var _fileExists = require("../../lib/file-exists");
  8. var _getPagePaths = require("../../shared/lib/page-path/get-page-paths");
  9. var _nonNullable = require("../../lib/non-nullable");
  10. var _path = require("path");
  11. var _fs = require("fs");
  12. var _log = require("../../build/output/log");
  13. var _chalk = _interopRequireDefault(require("../../lib/chalk"));
  14. function _interopRequireDefault(obj) {
  15. return obj && obj.__esModule ? obj : {
  16. default: obj
  17. };
  18. }
  19. async function isTrueCasePagePath(pagePath, pagesDir) {
  20. const pageSegments = (0, _path).normalize(pagePath).split(_path.sep).filter(Boolean);
  21. const segmentExistsPromises = pageSegments.map(async (segment, i)=>{
  22. const segmentParentDir = (0, _path).join(pagesDir, ...pageSegments.slice(0, i));
  23. const parentDirEntries = await _fs.promises.readdir(segmentParentDir);
  24. return parentDirEntries.includes(segment);
  25. });
  26. return (await Promise.all(segmentExistsPromises)).every(Boolean);
  27. }
  28. async function findPageFile(pagesDir, normalizedPagePath, pageExtensions, isAppDir) {
  29. const pagePaths = (0, _getPagePaths).getPagePaths(normalizedPagePath, pageExtensions, isAppDir);
  30. const [existingPath, ...others] = (await Promise.all(pagePaths.map(async (path)=>{
  31. const filePath = (0, _path).join(pagesDir, path);
  32. return await (0, _fileExists).fileExists(filePath) ? path : null;
  33. }))).filter(_nonNullable.nonNullable);
  34. if (!existingPath) {
  35. return null;
  36. }
  37. if (!await isTrueCasePagePath(existingPath, pagesDir)) {
  38. return null;
  39. }
  40. if (others.length > 0) {
  41. (0, _log).warn(`Duplicate page detected. ${_chalk.default.cyan((0, _path).join("pages", existingPath))} and ${_chalk.default.cyan((0, _path).join("pages", others[0]))} both resolve to ${_chalk.default.cyan(normalizedPagePath)}.`);
  42. }
  43. return existingPath;
  44. }
  45. function isLayoutsLeafPage(filePath) {
  46. return /[\\/]?page\.((server|client)\.?)?[jt]sx?$/.test(filePath);
  47. }
  48. //# sourceMappingURL=find-page-file.js.map