addToRouter static method

dynamic addToRouter({
  1. String routerName = "router",
  2. required String classImport,
  3. required String createTemplate(
    1. String originalFile
    ),
})

Attempts to replace a file. Provide a routerName to select which file to replace. Then you can use the callback originalFile to get the file and manipulate it.

Implementation

static addToRouter(
    {String routerName = "router",
    required String classImport,
    required String Function(String originalFile) createTemplate}) async {
  // add it to the decoder config
  String filePath = "lib/routes/$routerName.dart";
  String originalFile = await loadAsset(filePath);

  // create new file
  String fileCreated = createTemplate(originalFile);
  if (fileCreated == "") {
    return;
  }

  // Add import
  fileCreated = "$classImport\n$fileCreated";

  // save new file
  final File file = File(filePath);
  RegExp regEx = RegExp(r'^([\s]+)?}\);([\n\s\S]+)?$');

  await file.writeAsString(fileCreated.replaceAll(regEx, '});'));
}