checkUpdate function

Future<void> checkUpdate([
  1. bool fromCommand = true
])

Command to run from fts upgrade or automatically after all executions. Checks if there's a new version available on pub.dev and allows the user to install them.

Implementation

Future<void> checkUpdate([bool fromCommand = true]) async {
  // if (CliConfig.isDev) {
  //   trace('\nCan\'t update when running on dev enviroment');
  //   return;
  // }
  if (fromCommand) {
    trace('\nChecking for updates...');
  }

  try {
    final latest = await _checkLatestVersion();
    if (latest == null) {
      if (fromCommand) {
        error('Cannot fetch the latest published version');
      }
      return;
    }
    final current = currentVersion();
    if (current == null) {
      if (fromCommand) {
        error('There was an error reading the current version');
      }
      return;
    }
    final compare = compareSemver(current, latest);
    if (compare >= 0) {
      if (fromCommand) {
        trace(cyan('You are using the latest version of fts.'));
      }
      return;
    }
    final c = orange(current);
    final l = green(latest);
    trace(
      yellow(
        '___________________________________________________\n\n',
      ),
    );
    trace(cyan('Update available ', bold: true));
    trace('\n$c -> $l\n');
    // blue();
    trace(
      'Changelog: ${blue("https://pub.dev/packages/${CliConfig.packageName}/changelog")}',
    );
    trace(
      yellow(
        '\n___________________________________________________\n\n',
      ),
    );
    final result = confirm(
      yellow('Would you like update to the last version?'),
      defaultValue: true,
    );
    if (result) {
      return upgrade();
    }
  } on Exception {
    return;
  }
}