mainEdit method

void mainEdit()

Implementation

void mainEdit() {
  final file = File("${Directory.current.path}/lib/main.dart");
  bool isSSLCLIProject = true;
  String setupProjectSnippet = '';
  List<String> lines = file.readAsLinesSync();

  int initServicesIndex = lines.indexOf("initServices() async {");
  if (initServicesIndex == -1) {
    isSSLCLIProject = false;
    initServicesIndex = lines.indexOf("void main() {");
  }

  String codeSnippet1 = 'AppUrlExtention.setUrl(UrlLink.isDev);';
  final codeSnippet2 = '''
const mode = String.fromEnvironment('mode', defaultValue: 'DEV');
AppUrlExtention.setUrl(
  mode == "DEV" ? UrlLink.isDev : UrlLink.isLive,
);
''';
  final codeSnippet3 = '''
const mode = String.fromEnvironment('mode', defaultValue: 'DEV');
if(mode == "LIVE"){
  // set your production based url
} else if (mode == "DEV") {
   // set your development based url
}
''';

  setupProjectSnippet = isSSLCLIProject ? codeSnippet2 : codeSnippet3;

  int index1 = lines.indexWhere((line) => line.trim() == codeSnippet1.trim());
  int index2 = lines.join('\n').contains(setupProjectSnippet.trim()) ? 1 : -1;

  if (index1 != -1) {
    lines[index1] = setupProjectSnippet;
    file.writeAsStringSync(lines.join('\n'));
  } else {
    if (index2 == -1) {
      final int index = lines
          .indexWhere((line) => line.trim() == 'final flavorCode = \'\'\'');

      if (index != -1) {
        lines.replaceRange(
            index + 1, index + 7, setupProjectSnippet.split('\n'));
      } else {
        lines.insert(initServicesIndex + 1, setupProjectSnippet);
      }

      file.writeAsStringSync(lines.join('\n'));
    }
  }
}