downloadTemplate method

Future<void> downloadTemplate(
  1. String repoName,
  2. String path,
  3. Directory baseDirectory
)

Implementation

Future<void> downloadTemplate(String repoName, String path, Directory baseDirectory) async {
  final repoApiContentsPath = "https://api.github.com/repos/$repoName/contents/$path";
  final httpResponse = await http.get(Uri.parse(repoApiContentsPath));

  if (httpResponse.statusCode >= 300 || httpResponse.statusCode < 200) {
    throw Exception("Failed to clone the docs base.");
  }

  final jsonBody = jsonDecode(httpResponse.body) as List<dynamic>;
  for (final rawFileData in jsonBody) {
    final fileJson = rawFileData as Map<String, dynamic>;
    if ((fileJson["download_url"] as String?) == null) {
      print("No download path for ${fileJson["name"]}, ignoring file.");
      return;
    }
    final fileData = await http.read(Uri.parse(fileJson["download_url"] as String));
    final filePath = baseDirectory.path + (fileJson["path"] as String).replaceAll(path, "");
    final file = File(filePath);
    await file.create(recursive: true);
    await file.writeAsString(fileData);
  }
}