removeFirebaseFromContent static method

String removeFirebaseFromContent(
  1. String content
)

Implementation

static String removeFirebaseFromContent(String content) {
  var cleaned = content;

  // 1. Remove Firebase init (multi-line)
  final firebaseInitRegex = RegExp(
    r'^\s*await Firebase\.initializeApp\([\s\S]*?\);[\t ]*\n?',
    multiLine: true,
  );
  cleaned = cleaned.replaceAll(firebaseInitRegex, '');

  // 2. Remove Firebase imports and options imports (handles single/double quotes, aliases, and indentation)
  cleaned = cleaned.replaceAll(
    RegExp(
      r'''^\s*import\s+['"]package:firebase_core/firebase_core\.dart['"];[\t ]*\n?''',
      multiLine: true,
    ),
    '',
  );
  cleaned = cleaned.replaceAll(
    RegExp(
      r'''^\s*import\s+['"].*?firebase_options.*?\.dart['"](?:\s+as\s+\w+)?;[\t ]*\n?''',
      multiLine: true,
    ),
    '',
  );

  // 3. Fix main() signature if it was made async for Firebase but no longer needs to be
  if (!cleaned.contains('await ')) {
    cleaned = cleaned.replaceFirst(
      RegExp(r'void main\s*\(\s*\) async\s*\{'),
      'void main() {',
    );
  }

  // 4. Cleanup multiple newlines
  cleaned = cleaned.replaceAll(RegExp(r'\n{3,}'), '\n\n');

  return cleaned.trim() + '\n';
}