Skip to content

Commit

Permalink
feat: prompt to update CLI if version is older than latest published …
Browse files Browse the repository at this point in the history
…version
  • Loading branch information
Salakar committed Mar 30, 2022
1 parent 991d5a4 commit a88ade1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
25 changes: 24 additions & 1 deletion packages/flutterfire_cli/bin/flutterfire.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ignore_for_file: avoid_print
/*
* Copyright (c) 2020-present Invertase Limited & Contributors
*
Expand All @@ -22,13 +23,35 @@ import 'package:flutterfire_cli/src/common/exception.dart';
import 'package:flutterfire_cli/src/common/utils.dart' as utils;
import 'package:flutterfire_cli/src/flutter_app.dart';
import 'package:flutterfire_cli/version.g.dart';
import 'package:pub_updater/pub_updater.dart';

Future<void> main(List<String> arguments) async {
if (arguments.contains('--version') || arguments.contains('-v')) {
// ignore: avoid_print
print(cliVersion);
// No version checks on CIs.
if (utils.isCI) return;

// Check for updates.
final pubUpdater = PubUpdater();
const packageName = 'flutterfire_cli';
final isUpToDate = await pubUpdater.isUpToDate(
packageName: packageName,
currentVersion: cliVersion,
);
if (!isUpToDate) {
final latestVersion = await pubUpdater.getLatestVersion(packageName);
final shouldUpdate = utils.promptBool(
'There is a new version of $packageName available ($latestVersion). Would you like to update?',
);
if (shouldUpdate) {
await pubUpdater.update(packageName: packageName);
}
print('$packageName has been updated to version $latestVersion.');
}

return;
}

try {
final flutterApp = await FlutterApp.load(Directory.current);
await FlutterFireCommandRunner(flutterApp).run(arguments);
Expand Down
10 changes: 10 additions & 0 deletions packages/flutterfire_cli/lib/src/common/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ String listAsPaddedTable(List<List<String>> table, {int paddingSize = 1}) {
return output.join('\n');
}

bool promptBool(
String prompt, {
bool defaultValue = true,
}) {
return interact.Confirm(
prompt: prompt,
defaultValue: defaultValue,
).interact();
}

int promptSelect(
String prompt,
List<String> choices, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import 'dart:convert';
import 'dart:io';

import 'package:ansi_styles/ansi_styles.dart';
import 'package:interact/interact.dart' as interact;
import 'package:path/path.dart';

import '../common/exception.dart';
Expand Down Expand Up @@ -65,11 +64,9 @@ class FirebaseAppIDFile {
// Only prompt overwrite if values are different.
if (existingAppId != appId ||
existingFirebaseProjectId != firebaseProjectId) {
final shouldOverwrite = interact.Confirm(
prompt:
'Generated FirebaseAppID file ${AnsiStyles.cyan(appIDFilePath)} already exists (for app id "$existingAppId" on Firebase Project "$existingFirebaseProjectId"), do you want to override it?',
defaultValue: true,
).interact();
final shouldOverwrite = promptBool(
'Generated FirebaseAppID file ${AnsiStyles.cyan(appIDFilePath)} already exists (for app id "$existingAppId" on Firebase Project "$existingFirebaseProjectId"), do you want to override it?',
);
if (!shouldOverwrite) {
throw FirebaseAppIDAlreadyExistsException(appIDFilePath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import 'dart:io';

import 'package:ansi_styles/ansi_styles.dart';
import 'package:interact/interact.dart' as interact;
import 'package:path/path.dart';

import '../common/exception.dart';
Expand Down Expand Up @@ -58,11 +57,9 @@ class FirebaseConfigurationFile {
final existingFileContents = await outputFile.readAsString();
// Only prompt overwrite if contents have changed.
if (existingFileContents != newFileContents) {
final shouldOverwrite = interact.Confirm(
prompt:
'Generated FirebaseOptions file ${AnsiStyles.cyan(outputFilePath)} already exists, do you want to override it?',
defaultValue: true,
).interact();
final shouldOverwrite = promptBool(
'Generated FirebaseOptions file ${AnsiStyles.cyan(outputFilePath)} already exists, do you want to override it?',
);
if (!shouldOverwrite) {
throw FirebaseOptionsAlreadyExistsException(outputFilePath);
}
Expand Down
1 change: 1 addition & 0 deletions packages/flutterfire_cli/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies:
meta: ^1.7.0
path: ^1.8.0
platform: ^3.0.2
pub_updater: ^0.2.2
pubspec: ^2.0.1
xml: ^5.3.1

Expand Down

0 comments on commit a88ade1

Please sign in to comment.