Skip to content

Commit

Permalink
feat(configure): add --yes flag to automatically accept default opt…
Browse files Browse the repository at this point in the history
…ions on prompts (closes #48)
  • Loading branch information
Salakar committed Mar 30, 2022
1 parent d992143 commit 657892c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
19 changes: 17 additions & 2 deletions packages/flutterfire_cli/lib/src/commands/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -215,7 +226,7 @@ class ConfigCommand extends FlutterFireCommand {
kMacos: flutterApp!.macos,
kWeb: flutterApp!.web,
};
if (isCI) {
if (isCI || yes) {
return selectedPlatforms;
}
final answers = promptMultiSelect(
Expand Down Expand Up @@ -294,6 +305,7 @@ class ConfigCommand extends FlutterFireCommand {
iosOptions: iosOptions,
macosOptions: macosOptions,
webOptions: webOptions,
force: isCI || yes,
);
futures.add(configFile.write());

Expand All @@ -302,6 +314,7 @@ class ConfigCommand extends FlutterFireCommand {
iosAppIDOutputFilePrefix,
appId: iosOptions.appId,
firebaseProjectId: iosOptions.projectId,
force: isCI || yes,
);
futures.add(appIDFile.write());
}
Expand All @@ -311,6 +324,7 @@ class ConfigCommand extends FlutterFireCommand {
macosAppIDOutputFilePrefix,
appId: macosOptions.appId,
firebaseProjectId: macosOptions.projectId,
force: isCI || yes,
);
futures.add(appIDFile.write());
}
Expand All @@ -320,6 +334,7 @@ class ConfigCommand extends FlutterFireCommand {
androidAppIDOutputFilePrefix,
appId: androidOptions.appId,
firebaseProjectId: androidOptions.projectId,
force: isCI || yes,
);
futures.add(appIDFile.write());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit 657892c

Please sign in to comment.