makePage static method

dynamic makePage(
  1. String className,
  2. String value, {
  3. String folderPath = pagesFolder,
  4. bool forceCreate = false,
  5. bool addToRoute = true,
  6. bool isInitialPage = false,
  7. bool isAuthPage = false,
  8. String? creationPath,
})

Creates a new Page.

Implementation

static makePage(String className, String value,
    {String folderPath = pagesFolder,
    bool forceCreate = false,
    bool addToRoute = true,
    bool isInitialPage = false,
    bool isAuthPage = false,
    String? creationPath}) async {
  String name = className.snakeCase.replaceAll(RegExp(r'(_?page)'), "");

  await createDirectoriesFromCreationPath(creationPath, folderPath);

  String filePath = createPathForDartFile(
    folderPath: folderPath,
    className: name,
    prefix: 'page',
    creationPath: creationPath,
  );

  await _makeDirectory(folderPath);
  await _checkIfFileExists(filePath, shouldForceCreate: forceCreate);
  await _createNewFile(filePath, value, onSuccess: () {
    TaycanConsole.writeInGreen('[Page] ${name.snakeCase}_page created 🎊');
  });

  if (addToRoute == false) return;

  String classImport =
      "import '/ui/${creationPath != null ? '$creationPath/' : ''}${name.snakeCase}_page.dart';";

  /// Add to router
  await addToRouter(
      classImport: classImport,
      createTemplate: (file) {
        String strAuthPage = "";
        if (isAuthPage) {
          strAuthPage =
              "  static const String ${name.snakeCase} = '/${name.snakeCase}';\n";
        }
        String strInitialPage = "";
        if (isInitialPage) {
          strInitialPage =
              "  static const String initialPage = ${name.snakeCase};\n";
        }
        String routeName =
            'router.add(${name.pascalCase}Page.path$strAuthPage$strInitialPage);';
        if (file.contains(routeName)) {
          return "";
        }

        RegExp reg = RegExp(r'([\s]+)?}\);([\n\s\S]+)?$');
        return file.replaceAll(reg, '  $routeName\n});');
      });
}