deployStorage method

Future<bool> deployStorage({
  1. bool allowNotInitialized = false,
})

Deploy Storage rules

Implementation

Future<bool> deployStorage({bool allowNotInitialized = false}) async {
  info('Deploying Storage rules...');
  final String? projectId = _requireFirebaseProjectId();
  if (projectId == null) {
    return false;
  }

  final List<String> args = <String>[
    'deploy',
    '--only',
    'storage',
    '--project',
    projectId,
  ];

  final ProcessResult firstAttempt = await _runner.run(
    'firebase',
    args,
    workingDirectory: config.outputDir,
    environment: _authEnvironment,
  );

  if (firstAttempt.success) {
    return true;
  }

  final String firstOutput = '${firstAttempt.stdout}\n${firstAttempt.stderr}'
      .trim();
  if (_isStorageNotInitialized(firstOutput)) {
    // Try to auto-create the default bucket via FirebaseInitializer before
    // falling back to the console hand-off.
    info(
      'Firebase Storage default bucket appears uninitialized; attempting to create it automatically...',
    );
    final FirebaseInitializer initializer = FirebaseInitializer(
      projectId,
      runner: _runner,
    );
    final StorageInitResult init = await initializer.ensureStorageBucket();
    if (init.success) {
      // Retry deploy now that the bucket exists.
      final ProcessResult retry = await _runner.run(
        'firebase',
        args,
        workingDirectory: config.outputDir,
        environment: _authEnvironment,
      );
      if (retry.success) {
        return true;
      }
      final String retryOutput = '${retry.stdout}\n${retry.stderr}'.trim();
      if (!_isStorageNotInitialized(retryOutput)) {
        // Fall through to retry-with-prompt path below.
        return _retryStorageDeploy(args);
      }
    } else if (init.message.isNotEmpty) {
      warn(init.message);
    }

    warn(
      'Firebase Storage is not initialized for project $projectId. '
      'Open ${FirebaseInitializer.getStartedUrl(projectId)} and click "Get Started".',
    );
    return allowNotInitialized;
  }

  return _retryStorageDeploy(args);
}