writeFile function
Create or edit the contents of a file
Implementation
File writeFile(String path, String content,
{bool overwrite = false,
bool skipFormatter = false,
bool logger = true,
bool skipRename = false,
bool useRelativeImport = false}) {
var newFile = File(Structure.replaceAsExpected(path: path));
if (!newFile.existsSync() || overwrite) {
if (!skipFormatter) {
if (path.endsWith('.dart')) {
try {
content = sortImports(
content,
renameImport: !skipRename,
filePath: path,
useRelative: useRelativeImport,
);
} on Exception catch (_) {
if (newFile.existsSync()) {
LogService.info(
LocaleKeys.error_invalid_dart.trArgs([newFile.path]));
}
rethrow;
}
}
}
if (!skipRename && newFile.path != 'pubspec.yaml') {
var separatorFileType = PubspecUtils.separatorFileType!;
if (separatorFileType.isNotEmpty) {
newFile = newFile.existsSync()
? newFile = newFile
.renameSync(replacePathTypeSeparator(path, separatorFileType))
: File(replacePathTypeSeparator(path, separatorFileType));
}
}
newFile.createSync(recursive: true);
newFile.writeAsStringSync(content);
if (logger) {
LogService.success(
LocaleKeys.sucess_file_created.trArgs(
[basename(newFile.path), newFile.path],
),
);
}
}
return newFile;
}