writeFile function

File writeFile(
  1. String path,
  2. String content, {
  3. bool overwrite = false,
  4. bool logger = true,
})

Implementation

File writeFile(
  String path,
  String content, {
  bool overwrite = false,
  bool logger = true,
}) {
  var file = File(Structure.replaceAsExpected(path: path));

  if (!file.existsSync() || overwrite) {
    try {
      content = sortImports(
        content,
        filePath: path,
      );
    } on Exception catch (_) {
      if (file.existsSync()) {
        'The ${file.path} is not a valid dart file.';
      }
      rethrow;
    }
    var separatorFileType = PubspecUtils.separatorFileType!;
    if (separatorFileType.isNotEmpty) {
      file = file.existsSync()
          ? file =
              file.renameSync(replacePathTypeSeparator(path, separatorFileType))
          : File(replacePathTypeSeparator(path, separatorFileType));
    }

    file.createSync(recursive: true);
    file.writeAsStringSync(content);

    if (logger) {
      LogService.success(
          'File: ${basename(file.path)} created successfully at path: ${file.path}');
    }
  }
  return file;
}