From 657892c873178961209bf77c1120e032f77221d6 Mon Sep 17 00:00:00 2001 From: Salakar Date: Wed, 30 Mar 2022 13:04:13 +0100 Subject: [PATCH] feat(configure): add `--yes` flag to automatically accept default options on prompts (closes #48) --- .../lib/src/commands/config.dart | 19 +++++++++++++++++-- .../src/firebase/firebase_app_id_file.dart | 6 +++++- .../firebase/firebase_configuration_file.dart | 6 +++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/packages/flutterfire_cli/lib/src/commands/config.dart b/packages/flutterfire_cli/lib/src/commands/config.dart index 0cfaf8a6..428f4642 100644 --- a/packages/flutterfire_cli/lib/src/commands/config.dart +++ b/packages/flutterfire_cli/lib/src/commands/config.dart @@ -43,6 +43,13 @@ class ConfigCommand extends FlutterFireCommand { help: 'The output file path of the Dart file that will be generated with ' 'your Firebase configuration options.', ); + argParser.addFlag( + 'yes', + abbr: 'y', + negatable: false, + help: + 'Skip the Y/n confirmation prompts and accept default options (such as detected platforms).', + ); argParser.addOption( 'ios-bundle-id', valueHelp: 'bundleIdentifier', @@ -86,6 +93,10 @@ class ConfigCommand extends FlutterFireCommand { 'command will fetch Firebase configuration for you and generate a ' 'Dart file with prefilled FirebaseOptions you can use.'; + bool get yes { + return argResults!['yes'] as bool || false; + } + String? get androidApplicationId { final value = argResults!['android-app-id'] as String?; // TODO validate appId is valid if provided @@ -151,7 +162,7 @@ class ConfigCommand extends FlutterFireCommand { var selectedProjectId = projectId; selectedProjectId ??= await firebase.getDefaultFirebaseProjectId(); - if (isCI && selectedProjectId == null) { + if ((isCI || yes) && selectedProjectId == null) { throw FirebaseProjectRequiredException(); } @@ -215,7 +226,7 @@ class ConfigCommand extends FlutterFireCommand { kMacos: flutterApp!.macos, kWeb: flutterApp!.web, }; - if (isCI) { + if (isCI || yes) { return selectedPlatforms; } final answers = promptMultiSelect( @@ -294,6 +305,7 @@ class ConfigCommand extends FlutterFireCommand { iosOptions: iosOptions, macosOptions: macosOptions, webOptions: webOptions, + force: isCI || yes, ); futures.add(configFile.write()); @@ -302,6 +314,7 @@ class ConfigCommand extends FlutterFireCommand { iosAppIDOutputFilePrefix, appId: iosOptions.appId, firebaseProjectId: iosOptions.projectId, + force: isCI || yes, ); futures.add(appIDFile.write()); } @@ -311,6 +324,7 @@ class ConfigCommand extends FlutterFireCommand { macosAppIDOutputFilePrefix, appId: macosOptions.appId, firebaseProjectId: macosOptions.projectId, + force: isCI || yes, ); futures.add(appIDFile.write()); } @@ -320,6 +334,7 @@ class ConfigCommand extends FlutterFireCommand { androidAppIDOutputFilePrefix, appId: androidOptions.appId, firebaseProjectId: androidOptions.projectId, + force: isCI || yes, ); futures.add(appIDFile.write()); } diff --git a/packages/flutterfire_cli/lib/src/firebase/firebase_app_id_file.dart b/packages/flutterfire_cli/lib/src/firebase/firebase_app_id_file.dart index 81a11891..aef93403 100644 --- a/packages/flutterfire_cli/lib/src/firebase/firebase_app_id_file.dart +++ b/packages/flutterfire_cli/lib/src/firebase/firebase_app_id_file.dart @@ -34,12 +34,16 @@ class FirebaseAppIDFile { required this.appId, required this.firebaseProjectId, this.fileName = _defaultAppIdFileName, + this.force = false, }); final StringBuffer _stringBuffer = StringBuffer(); final String outputDirectoryPath; + /// Whether to skip prompts and force write output file. + final bool force; + final String fileName; final String appId; @@ -53,7 +57,7 @@ class FirebaseAppIDFile { final appIDFilePath = joinAll([outputDirectoryPath, fileName]); final outputFile = File(joinAll([Directory.current.path, appIDFilePath])); - if (outputFile.existsSync() && !isCI) { + if (outputFile.existsSync() && !force) { final existingFileContents = await outputFile.readAsString(); final existingFileContentsAsJson = json.decode(existingFileContents) as Map; diff --git a/packages/flutterfire_cli/lib/src/firebase/firebase_configuration_file.dart b/packages/flutterfire_cli/lib/src/firebase/firebase_configuration_file.dart index 51df4736..6ca1ddc0 100644 --- a/packages/flutterfire_cli/lib/src/firebase/firebase_configuration_file.dart +++ b/packages/flutterfire_cli/lib/src/firebase/firebase_configuration_file.dart @@ -31,12 +31,16 @@ class FirebaseConfigurationFile { this.iosOptions, this.macosOptions, this.webOptions, + this.force = false, }); final StringBuffer _stringBuffer = StringBuffer(); final String outputFilePath; + /// Whether to skip prompts and force write output file. + final bool force; + FirebaseOptions? webOptions; FirebaseOptions? macosOptions; @@ -53,7 +57,7 @@ class FirebaseConfigurationFile { _writeClass(); final newFileContents = _stringBuffer.toString(); - if (outputFile.existsSync() && !isCI) { + if (outputFile.existsSync() && !force) { final existingFileContents = await outputFile.readAsString(); // Only prompt overwrite if contents have changed. if (existingFileContents != newFileContents) {