doRun method

  1. @override
Future<int> doRun()
override

Implementation

@override
Future<int> doRun() async {
  final args = argResults!;
  final line = args.rest.join(' ').trim();

  if (line.isEmpty) {
    return error(1,
        message: 'Nothing to add - the entry is not provided. '
            'Example: alex changelog add "Some new feature"');
  }

  const fs = IOFileSystem();
  if (!await Spec.exists(fs)) {
    return error(1,
        message: 'You should run command from project root directory.');
  }

  final changelog = Changelog(fs);
  if (!await changelog.exists) {
    return error(1, message: 'CHANGELOG.md is not found.');
  }

  final section = args.getString(_argSection) ?? _sectionAdded;
  final issueId = args.getInt(_argIssue);

  final sectionAdded = await changelog.ensureNextReleaseSection();

  switch (section) {
    case _sectionFixed:
      await changelog.addFixedEntry(line, issueId, config.issueUrl);
      break;
    case _sectionPreRelease:
      await changelog.addPreReleaseEntry(line, issueId, config.issueUrl);
      break;
    case _sectionAdded:
    default:
      await changelog.addAddedEntry(line, issueId, config.issueUrl);
      break;
  }

  await changelog.save();

  final summary = 'added in $section'
      '${sectionAdded ? ', a section for the next release was created' : ''}';

  if (isJsonFormat) {
    return jsonResult(
      exitCode: 0,
      summary: summary,
      data: <String, dynamic>{
        'entry': line,
        'section': section,
        if (issueId != null) 'issue': issueId,
        'nextReleaseSectionCreated': sectionAdded,
      },
    );
  }

  if (sectionAdded) {
    printInfo('Section for the changes that are not released yet '
        'was created at the top of CHANGELOG.md.');
  }

  return success(message: '📝 Entry added in the $section section.');
}