addRoute function

void addRoute(
  1. String nameRoute,
  2. String bindingDir,
  3. String viewDir
)

This command will create the route to the new page

Implementation

void addRoute(String nameRoute, String bindingDir, String viewDir) {
  var routesFile = findFileByName('app_routes.dart');
  // var content = '';

  if (routesFile.path.isEmpty) {
    RouteSample().create();
    routesFile = File(RouteSample().path);
    // content = routesFile.readAsStringSync();
  }

  var pathSplit = viewDir.split('/');

  ///remove file
  pathSplit.removeLast();

  ///remove view folder
  if (PubspecUtils.extraFolder ?? true) {
    pathSplit.removeLast();
  }

  // remove the first 2 folders
  pathSplit.removeRange(0, 2);
  // pathSplit.removeWhere((element) => element == 'app' || element == 'modules');

  for (var i = 0; i < pathSplit.length; i++) {
    pathSplit[i] =
        pathSplit[i].snakeCase.snakeCase.toLowerCase().replaceAll('_', '-');
  }
  var route = pathSplit.join('/');

  var declareRoute = 'static const ${nameRoute.camelCase} =';
  var line = "$declareRoute '/$route';";
  if (supportChildrenRoutes) {
    line = '$declareRoute ${_pathsToRoute(pathSplit)};';
    var linePath = "$declareRoute '/${pathSplit.last}';";
    final result = routesFile.appendClassContent('_Paths', linePath);
    if (!result) {
      LogService.info(
        'The route $route already exists in app_routes.dart, skip adding',
        false,
        false,
      );
    }
  }
  final result = routesFile.appendClassContent('Routes', line);
  if (!result) {
    LogService.info(
      'The route $route already exists in app_routes.dart, skip adding',
      false,
      false,
    );
  }

  addAppPage(nameRoute, bindingDir, viewDir);

  LogService.success(
    Translation(LocaleKeys.sucess_route_created).trArgs([nameRoute]),
  );
}