generateLib method

String generateLib({
  1. required String template,
  2. required String fileName,
  3. List<String> dirNames = const [],
  4. bool rewrite = false,
  5. bool flutter = true,
  6. bool shouldExport = true,
})

Implementation

String generateLib({
  required String template,
  required String fileName,
  List<String> dirNames = const [],

  /// String? dirName,
  bool rewrite = false,

  ///
  bool flutter = true,

  /// should export current file to its core lib
  bool shouldExport = true,
}) {
  var contents = template
      .replaceAll(RegExp(r"className"), className)
      .replaceAll(RegExp(r"objectName"), className.camelCase)
      .replaceAll(RegExp(r"projectName"), projectName);

  /// if user input directory name,
  /// then create the new sub directory if not exists

  /// TODO: old
  /// if (dirName != null) {
  ///   DirectoryCreator(path.join(libraryPath, parentDir, dirName)).run();
  /// }

  DirectoryCreator(path.joinAll(
      [libraryPath, if (parentDir != null) parentDir!, ...dirNames])).run();

  /// current file parent path
  final _parentLibPath = path.joinAll([
    libraryPath,
    if (parentDir != null) ...[parentDir!, "${parentDir!}.dart"]
  ]);

  /// printRed("_parentLibPath $_parentLibPath");

  /// current new file path
  final _filePath = path.joinAll([
    libraryPath,
    if (parentDir != null) parentDir!,
    ...dirNames,
    "$fileName.dart"
  ]);

  FileCreator(_filePath, contents: contents, rewrite: rewrite);

  /// write content
  FileCreator(_filePath, contents: contents, rewrite: rewrite).run();

  /// printCyan(content);

  /// add to its parent lib if none exist
  if (_parentLibPath != libraryPath) {
    var content = io.File(_parentLibPath).readAsStringSync();
    var exist = content.contains(RegExp("$fileName.dart"));
    if (!exist && shouldExport) {
      content +=
          "\nexport '${dirNames.isNotEmpty ? (dirNames.join("/") + "/") : ""}$fileName.dart';";

      /// force to rewrite
      FileCreator(_parentLibPath, contents: content, rewrite: true).run();
    }
  }

  return _filePath;
}