checkForUpdate function

dynamic checkForUpdate()

Implementation

checkForUpdate() async {
  print(info('Info: Checking for update'));
  bool isInternet = await checkInternet();
  if (!isInternet) {
    print(chalk.yellow('Warning: Update check failed'));
    return;
  }
  var versionInPubDev =
      await getLatestVersionFromPackage('whitecodel_auto_link');
  var versionInstalled = await getCurrentVersion();

  if (versionInstalled == null) {
    exit(2);
  }

  final v1 = Version.parse(versionInPubDev!);
  final v2 = Version.parse(versionInstalled);
  final needsUpdate = v1.compareTo(v2);
  // needs update.

  if (needsUpdate == 1) {
    print(info('Info: Update available for whitecodel_auto_link'));
    print(info(
        'Info: Run ${chalk.green('flutter pub global activate whitecodel_auto_link')} to update'));

    // ask for update
    var shouldUpdate = Confirm(
      prompt: 'Do you want to update whitecodel_auto_link?',
    ).interact();

    if (shouldUpdate) {
      var process = await Process.start(
          'flutter', ['pub', 'global', 'activate', 'whitecodel_auto_link']);
      process.stdout.transform(utf8.decoder).listen((data) {
        print(data);
      });

      process.stderr.transform(utf8.decoder).listen((data) {
        print(error('Error: $data'));
      });

      var exitCode = await process.exitCode;
      if (exitCode != 0) {
        throw error('Error: Command failed with exit code $exitCode');
      }
    }
  } else {
    print(info('Info: whitecodel_auto_link is up to date'));
  }
}