processMemoryFile method

Future<List<MemoryFileInfo>> processMemoryFile(
  1. String filePath,
  2. MemoryType type,
  3. Set<String> processedPaths,
  4. bool includeExternal, {
  5. int depth = 0,
  6. String? parent,
})

Recursively processes a memory file and all its @include references.

Implementation

Future<List<MemoryFileInfo>> processMemoryFile(
  String filePath,
  MemoryType type,
  Set<String> processedPaths,
  bool includeExternal, {
  int depth = 0,
  String? parent,
}) async {
  final normalizedPath = filePath.toLowerCase();
  if (processedPaths.contains(normalizedPath) || depth >= _maxIncludeDepth) {
    return [];
  }

  if (_isNeomageMdExcluded(filePath, type)) return [];

  processedPaths.add(normalizedPath);

  final readResult = await _safelyReadMemoryFileAsync(
    filePath,
    type,
    includeBasePath: filePath,
  );

  final memoryFile = readResult.info;
  if (memoryFile == null || memoryFile.content.trim().isEmpty) return [];

  final withParent = parent != null
      ? memoryFile.copyWith(parent: parent)
      : memoryFile;

  final result = <MemoryFileInfo>[withParent];

  for (final resolvedIncludePath in readResult.includePaths) {
    final isExternal = !_pathInWorkingPath(
      resolvedIncludePath,
      _getOriginalCwd(),
    );
    if (isExternal && !includeExternal) continue;

    final includedFiles = await processMemoryFile(
      resolvedIncludePath,
      type,
      processedPaths,
      includeExternal,
      depth: depth + 1,
      parent: filePath,
    );
    result.addAll(includedFiles);
  }

  return result;
}