checkForUpdate method

Future<void> checkForUpdate()

Implementation

Future<void> checkForUpdate() async {
  // don't wait on this, stop after 1 second
  final exiter = Completer<({(bool, String)? result, bool exit})>();

  Timer? timer;

  timer = Timer(const Duration(seconds: 1), () {
    exiter.complete((result: null, exit: true));
  });

  updateCommand.needsUpdate().then((value) {
    exiter.complete((result: value, exit: false));
  }).ignore();

  final (:result, :exit) = await exiter.future;
  timer.cancel();

  if (exit) {
    logger.detail('Skipping version check, timeout reached');
    return;
  }

  final (needsUpdate, latestVersion) = result!;

  if (needsUpdate) {
    const changelog =
        'https://github.com/mrgnhnt96/sip/blob/main/packages/sip/CHANGELOG.md';

    final package = cyan.wrap('sip_cli');
    final currentVersion = red.wrap(packageVersion);
    final updateToVersion = green.wrap(latestVersion);
    final updateCommand = yellow.wrap('sip update');
    final changelogLink = darkGray.wrap('Changelog: $changelog');

    final message = '''
 ┌─────────────────────────────────────────────────────────────────────────────────┐
 │ New update for $package is available!                                            │
 │ You are using $currentVersion, the latest is $updateToVersion.                                       │
 │ Run `$updateCommand` to update to the latest version.                               │
 │ $changelogLink │
 └─────────────────────────────────────────────────────────────────────────────────┘
''';

    logger.write(message);
  }
}