listFilesRecursive function
List<FileMetadata>
listFilesRecursive(
- String folderPath, {
- List<
FileMetadata> ? fileList, - String relativePath = '',
Implementation
List<FileMetadata> listFilesRecursive(String folderPath,
{List<FileMetadata>? fileList, String relativePath = ''}) {
fileList ??= [];
final gitignorePath = File('$folderPath/.gitignore');
final gitignorePatterns =
gitignorePath.existsSync() ? gitignorePath.readAsLinesSync() : [];
gitignorePatterns.add('.git'); // Always ignore .git folder.
final files = Directory(folderPath).listSync();
for (var file in files) {
final fileName = file.uri.pathSegments.last;
final fullPath = file.path;
final relativeFilePath = path.join(relativePath, fileName);
// Skip file if it matches .gitignore patterns
if (gitignorePatterns
.any((pattern) => RegExp(pattern).hasMatch(relativeFilePath))) {
continue;
}
// var newList = List.from(fileList);
if (FileSystemEntity.isDirectorySync(fullPath)) {
listFilesRecursive(fullPath,
fileList: fileList, relativePath: '$relativeFilePath/');
} else {
fileList.add(FileMetadata(
content: [],
fileName: fileName,
path: relativePath,
index: fullPath,
contentType: null));
}
// fileList = newList;
}
return fileList..sort((a, b) => a.fileName.compareTo(b.fileName));
}