addToRouter static method
dynamic
addToRouter({})
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, '});'));
}