Skip to content

Commit 63b367a

Browse files
committed
feat(cli): add --firebase-out flag for custom firebase.json path
1 parent b2887a9 commit 63b367a

File tree

4 files changed

+80
-11
lines changed

4 files changed

+80
-11
lines changed

packages/flutterfire_cli/lib/src/commands/config.dart

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ class ConfigCommand extends FlutterFireCommand {
188188
'Where to write the `google-services.json` file to be written for android platform. Useful for different flavors',
189189
);
190190

191+
argParser.addOption(
192+
kFirebaseOutFlag,
193+
valueHelp: 'filePath',
194+
help: 'The output file path of the `firebase.json` file that will be generated or updated.',
195+
);
196+
191197
argParser.addFlag(
192198
kOverwriteFirebaseOptionsFlag,
193199
abbr: 'f',
@@ -345,6 +351,12 @@ class ConfigCommand extends FlutterFireCommand {
345351
return argResults!['out'] as String;
346352
}
347353

354+
String get firebaseJsonPath {
355+
final customPath = argResults![kFirebaseOutFlag] as String?;
356+
if (customPath != null) return customPath;
357+
return path.join(flutterApp!.package.path, 'firebase.json');
358+
}
359+
348360
bool get overwriteFirebaseOptions {
349361
if (argResults!['overwrite-firebase-options'] == null) {
350362
return false;
@@ -536,8 +548,6 @@ class ConfigCommand extends FlutterFireCommand {
536548
}
537549

538550
Future<bool> checkIfUserRequiresReconfigure() async {
539-
final firebaseJsonPath =
540-
path.join(flutterApp!.package.path, 'firebase.json');
541551
final file = File(firebaseJsonPath);
542552

543553
if (file.existsSync()) {
@@ -550,7 +560,7 @@ class ConfigCommand extends FlutterFireCommand {
550560
);
551561

552562
if (reuseFirebaseJsonValues) {
553-
final reconfigure = Reconfigure(flutterApp, token: testAccessToken);
563+
final reconfigure = Reconfigure(flutterApp, token: testAccessToken, firebaseJsonPath: firebaseJsonPath);
554564
reconfigure.logger = logger;
555565
await reconfigure.run();
556566
return true;
@@ -705,12 +715,11 @@ class ConfigCommand extends FlutterFireCommand {
705715
firebaseJsonWrites.add(firebaseJsonWrite);
706716
}
707717

708-
// 5. Writes for "firebase.json" file in root of project
718+
// 5. Writes for "firebase.json" file
709719
if (firebaseJsonWrites.isNotEmpty) {
710720
await writeToFirebaseJson(
711721
listOfWrites: firebaseJsonWrites,
712-
firebaseJsonPath:
713-
path.join(flutterApp!.package.path, 'firebase.json'),
722+
firebaseJsonPath: firebaseJsonPath,
714723
);
715724
}
716725

packages/flutterfire_cli/lib/src/commands/reconfigure.dart

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,26 @@ class ConfigFileWrite {
5656
}
5757

5858
class Reconfigure extends FlutterFireCommand {
59-
Reconfigure(FlutterApp? flutterApp, {String? token}) : super(flutterApp) {
59+
Reconfigure(FlutterApp? flutterApp, {String? token, String? firebaseJsonPath}) : super(flutterApp) {
6060
setupDefaultFirebaseCliOptions();
6161
_accessToken = token;
62+
_firebaseJsonPath = firebaseJsonPath;
6263
argParser.addOption(
6364
'ci-access-token',
6465
valueHelp: 'ciAccessToken',
6566
hide: true,
6667
help:
6768
'Set the access token for making Firebase API requests. Required for CI environment.',
6869
);
70+
argParser.addOption(
71+
kFirebaseOutFlag,
72+
valueHelp: 'filePath',
73+
help: 'The path to the `firebase.json` file.',
74+
);
6975
}
7076

77+
String? _firebaseJsonPath;
78+
7179
@override
7280
final String description =
7381
'Updates the configurations for all build variants included in the "firebase.json" added by running `flutterfire configure`.';
@@ -378,11 +386,14 @@ class Reconfigure extends FlutterFireCommand {
378386
Future<void> run() async {
379387
try {
380388
commandRequiresFlutterApp();
389+
final customPath = argResults != null ? argResults![kFirebaseOutFlag] as String? : null;
381390
final firebaseJson = File(
382-
path.join(
383-
flutterApp!.package.path,
384-
'firebase.json',
385-
),
391+
_firebaseJsonPath ??
392+
customPath ??
393+
path.join(
394+
flutterApp!.package.path,
395+
'firebase.json',
396+
),
386397
);
387398

388399
if (!firebaseJson.existsSync()) {

packages/flutterfire_cli/lib/src/common/utils.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ const String kMacosTargetFlag = 'macos-target';
7979
const String kIosOutFlag = 'ios-out';
8080
const String kMacosOutFlag = 'macos-out';
8181
const String kAndroidOutFlag = 'android-out';
82+
const String kFirebaseOutFlag = 'firebase-out';
8283
const String kOverwriteFirebaseOptionsFlag = 'overwrite-firebase-options';
8384
const String kTestAccessTokenFlag = 'test-access-token';
8485

packages/flutterfire_cli/test/configure_test.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,4 +1740,52 @@ void main() {
17401740
Duration(minutes: 3),
17411741
),
17421742
);
1743+
1744+
test('flutterfire configure: --firebase-out flag should dictate where firebase.json is written', () async {
1745+
// Install flutterfire_cli from local path
1746+
final installDevDependency = Process.runSync(
1747+
'flutter',
1748+
[
1749+
'pub',
1750+
'add',
1751+
'--dev',
1752+
'flutterfire_cli',
1753+
'--path=${Directory.current.path}',
1754+
],
1755+
workingDirectory: projectPath,
1756+
);
1757+
1758+
if (installDevDependency.exitCode != 0) {
1759+
fail(installDevDependency.stderr as String);
1760+
}
1761+
1762+
const customFirebaseJsonPath = 'custom/firebase.json';
1763+
final result = Process.runSync(
1764+
'dart',
1765+
[
1766+
'run',
1767+
'flutterfire_cli:flutterfire',
1768+
'configure',
1769+
'--yes',
1770+
'--project=$firebaseProjectId',
1771+
'--platforms=android',
1772+
'--firebase-out=$customFirebaseJsonPath',
1773+
],
1774+
workingDirectory: projectPath,
1775+
runInShell: true,
1776+
);
1777+
1778+
if (result.exitCode != 0) {
1779+
fail(result.stderr as String);
1780+
}
1781+
1782+
// check custom "firebase.json" was created and has correct content
1783+
final firebaseJsonFile = p.join(projectPath!, 'custom', 'firebase.json');
1784+
expect(File(firebaseJsonFile).existsSync(), true);
1785+
1786+
final firebaseJsonFileContent = await File(firebaseJsonFile).readAsString();
1787+
final decodedFirebaseJson = jsonDecode(firebaseJsonFileContent) as Map<String, dynamic>;
1788+
1789+
expect(decodedFirebaseJson[kFlutter], isNotNull);
1790+
});
17431791
}

0 commit comments

Comments
 (0)