UNPKG

@adocasts.com/dto

Version:

Easily make and generate DTOs from Lucid Models

43 lines (42 loc) 1.65 kB
import { access, constants, readFile } from 'node:fs/promises'; import string from '@adonisjs/core/helpers/string'; export default class FileService { /** * Determines whether the provided file path exists and has allow-read permissions * @param filePath */ static async canRead(filePath) { try { await access(filePath, constants.R_OK); return true; } catch { return false; } } /** * Get's a files property declarations lines (declare | public) * @param filePath */ static async readDeclarations(filePath) { const contents = await readFile(filePath, 'utf8'); const fileLines = contents.split('\n'); const classStartIndex = fileLines.findIndex((line) => line.includes('export default class ')); const classEndIndex = fileLines.findLastIndex((line) => string.condenseWhitespace(line) === '}'); const classLines = fileLines .slice(classStartIndex + 1, classEndIndex) .map((line) => string.condenseWhitespace(line)); let isInBlock = false; const definitions = classLines.filter((line) => { const propertyMatch = line.match(/^(declare |public |get |[0-9A-z])+/); const isStatic = line.startsWith('static '); const isDefinition = propertyMatch && !isStatic && !isInBlock; if (line.endsWith('{')) isInBlock = true; if (line.startsWith('}') && isInBlock) isInBlock = false; return isDefinition; }); return { definitions, fileLines }; } }