Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions packages/flutterfire_cli/lib/src/firebase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ Future<bool> exists() async {
return _existsCache = process.exitCode == 0;
}

/// Check to verify npx is installed.
bool? _npxExistsCache;
Future<bool> npxExists() async {
if (_npxExistsCache != null) {
return _npxExistsCache!;
}
final process = await Process.run(
'npx',
['--version'],
runInShell: true,
);
return _npxExistsCache = process.exitCode == 0;
}

/// Tries to read the default Firebase project id from the
/// .firbaserc file at the root of the dart project if it exists.
Future<String?> getDefaultFirebaseProjectId() async {
Expand Down Expand Up @@ -72,25 +86,37 @@ Future<Map<String, dynamic>> runFirebaseCommand(
String? account,
String? serviceAccount,
}) async {
final cliExists = await exists();
if (!cliExists) {
throw FirebaseCommandException(
'--version',
logMissingFirebaseCli,
);
}
final workingDirectoryPath = Directory.current.path;
final execArgs = [
var command = 'firebase';
var execArgs = [
...commandAndArgs,
'--json',
if (project != null) '--project=$project',
if (account != null) '--account=$account',
];

final cliExists = await exists();
if (!cliExists) {
// If the CLI isn't installed, we can attempt to run it via npx which will
// use the newest Firebase CLI version
final npxCliExists = await npxExists();
if (!npxCliExists) {
throw FirebaseCommandException(
'--version',
logMissingFirebaseCli,
);
}
command = 'npx'
execArgs = [
'firebase-tools@latest'
...execArgs,
];
}

ProcessResult process;
try {
process = await Process.run(
'firebase',
command,
execArgs,
workingDirectory: workingDirectoryPath,
environment: {
Expand Down
Loading