UNPKG

next-ts-api

Version:

A powerful TypeScript-first API client generator for Next.js applications with full type safety and automatic api type generation.

41 lines (40 loc) 1.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAppDirectory = getAppDirectory; exports.findFiles = findFiles; exports.isNotUndefined = isNotUndefined; const fs_1 = require("fs"); const path_1 = require("path"); const logger_1 = require("./logger"); function findDir(cwd, dir) { const paths = [(0, path_1.join)(cwd, dir), (0, path_1.join)(cwd, "src", dir)]; for (const path of paths) { if ((0, fs_1.existsSync)(path)) { return path; } } return undefined; } function getAppDirectory() { let appDir = findDir(process.cwd(), "app"); if (!appDir) { logger_1.logger.error(`Could not find a Next.js app directory`); appDir = (0, path_1.join)(process.cwd(), "app"); } return appDir; } function findFiles(entry) { return (0, fs_1.readdirSync)(entry).flatMap((file) => { const filepath = (0, path_1.join)(entry, file); if (filepath.includes("node_modules")) { return []; } if ((0, fs_1.statSync)(filepath).isDirectory()) { return findFiles(filepath); } return filepath; }); } function isNotUndefined(value) { return value !== undefined; }