listTemplates static method

Future<List<TemplateInfo>> listTemplates(
  1. String directoryPath
)

Lists the templates available in a directory

Implementation

static Future<List<TemplateInfo>> listTemplates(String directoryPath) async {
  try {
    final directory = Directory(directoryPath);
    if (!await directory.exists()) return [];

    final templates = <TemplateInfo>[];

    await for (final entity in directory.list()) {
      if (entity is File && entity.path.endsWith('.rpt')) {
        final metadata = await getMetadata(entity.path);
        if (metadata != null) {
          templates.add(
            TemplateInfo(
              filePath: entity.path,
              name: metadata['name'] ?? entity.path.split('/').last,
              description: metadata['description'] ?? '',
              createdAt: metadata['createdAt'] != null
                  ? DateTime.parse(metadata['createdAt'])
                  : null,
              updatedAt: metadata['updatedAt'] != null
                  ? DateTime.parse(metadata['updatedAt'])
                  : null,
            ),
          );
        }
      }
    }

    return templates;
  } catch (e) {
    throw Exception('Error scanning the template directory: $e');
  }
}