writeChangelogToFile function

Future<ChangeSummary?> writeChangelogToFile({
  1. required List<Commit> commits,
  2. required File file,
  3. required String version,
  4. DateTime? now,
})

Writes to a changelog file based on commits.

It first checks if commits have "releaseable" commits using hasReleasableCommits. If it doesn't, it will return null. If it does, it will write to the file with given version string for the changes within within commits and now for the date. This will then return with a ChangeSummary.

If now isn't provided, it will default to DateTime.now().

Implementation

Future<ChangeSummary?> writeChangelogToFile({
  required List<Commit> commits,
  required File file,
  required String version,
  DateTime? now,
}) async {
  final summary = await changelogSummary(
    commits: commits,
    version: version,
    now: now,
  );
  if (summary is ChangeSummary) {
    String oldContents = '';
    if (await file.exists()) {
      oldContents = (await file.readAsString()).trim();
    }
    await file.writeAsString(oldContents.isEmpty
        ? summary.toMarkdown()
        : '${summary.toMarkdown()}\n$oldContents\n');
    return summary;
  }
  return null;
}