find-pages-dir.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.findPagesDir = findPagesDir;
  6. exports.existsSync = void 0;
  7. var _fs = _interopRequireDefault(require("fs"));
  8. var _path = _interopRequireDefault(require("path"));
  9. function _interopRequireDefault(obj) {
  10. return obj && obj.__esModule ? obj : {
  11. default: obj
  12. };
  13. }
  14. const existsSync = (f)=>{
  15. try {
  16. _fs.default.accessSync(f, _fs.default.constants.F_OK);
  17. return true;
  18. } catch (_) {
  19. return false;
  20. }
  21. };
  22. exports.existsSync = existsSync;
  23. function findDir(dir, name) {
  24. // prioritize ./${name} over ./src/${name}
  25. let curDir = _path.default.join(dir, name);
  26. if (existsSync(curDir)) return curDir;
  27. curDir = _path.default.join(dir, "src", name);
  28. if (existsSync(curDir)) return curDir;
  29. return null;
  30. }
  31. function findPagesDir(dir, appDirEnabled) {
  32. const pagesDir = findDir(dir, "pages") || undefined;
  33. let appDir;
  34. if (appDirEnabled) {
  35. appDir = findDir(dir, "app") || undefined;
  36. if (appDirEnabled == null && pagesDir == null) {
  37. throw new Error("> Couldn't find any `pages` or `app` directory. Please create one under the project root");
  38. }
  39. }
  40. if (!appDirEnabled) {
  41. if (pagesDir == null) {
  42. throw new Error("> Couldn't find a `pages` directory. Please create one under the project root");
  43. }
  44. }
  45. return {
  46. pages: pagesDir,
  47. appDir
  48. };
  49. }
  50. //# sourceMappingURL=find-pages-dir.js.map