getTemplatesByDirectory method

Future<TemplateDatas> getTemplatesByDirectory({
  1. required Directory? template_directory,
  2. Duration? durationTimeOut,
})

Implementation

Future<general_lib_scheme.TemplateDatas> getTemplatesByDirectory({
  required Directory? template_directory,
  Duration? durationTimeOut,
}) async {
  durationTimeOut ??= const Duration(minutes: 1);
  DateTime dateTimeTimeOut = DateTime.now().add(durationTimeOut);
  template_directory ??= Directory(join(base_directory.path, "template"));
  List<FileSystemEntity> dir_template = [];
  bool is_complete = false;

  var str = template_directory.list().listen(
    (FileSystemEntity fileSystemEntity) {
      if (fileSystemEntity.statSync().type ==
          FileSystemEntityType.directory) {
        dir_template.add(fileSystemEntity);
      }
    },
    onDone: () {
      is_complete = true;
    },
    cancelOnError: true,
  );
  List<general_lib_scheme.TemplateData> template_datas = [];
  while (true) {
    await Future.delayed(const Duration(milliseconds: 1));
    if (dateTimeTimeOut.isExpired()) {
      await str.cancel();
      return general_lib_scheme.TemplateDatas({
        "@type": "error",
        "message": "time_out",
        "description": "Time out",
      });
    }
    if (is_complete) {
      await str.cancel();
      break;
    }
  }
  for (var i = 0; i < dir_template.length; i++) {
    template_datas.add(general_lib_scheme.TemplateData.create(
      special_type: "templateData",
      name: basename(dir_template[i].path),
      path: dir_template[i].path,
      data: [],
    ));
  }
  return general_lib_scheme.TemplateDatas.create(
    special_type: "templateDatas",
    count: dir_template.length,
    templates: template_datas,
  );
}