Skip to content

Commit

Permalink
feat: add Directory.emptyOrCreate helper in utils (fs and io)
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Oct 24, 2024
1 parent 532a5c1 commit bd27f6b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
15 changes: 13 additions & 2 deletions fs/lib/utils/io/read_write.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'dart:typed_data';

import 'package:fs_shim/fs_io.dart' as fs;
import 'package:fs_shim/utils/read_write.dart' as fs;
import 'package:fs_shim/utils/src/utils_impl.dart' as fs;

var _useCrLf = Platform.isWindows;
var _eol = _useCrLf ? '\r\n' : '\n';
Expand Down Expand Up @@ -71,6 +70,18 @@ Future<List<String>> readLines(File file, {Encoding encoding = utf8}) =>
fs.readLines(fs.wrapIoFile(file), encoding: encoding);

/// Ensure the directory is created and empty.
/// @Deprecated
Future emptyOrCreateDirectory(Directory dir) {
return fs.emptyOrCreateDirectory(fs.wrapIoDirectory(dir));
return dir.emptyOrCreate();
}

/// Empty or create helper
extension DirectoryEmptyOrCreateExt on Directory {
fs.Directory get _fsDir => fs.wrapIoDirectory(this);

/// Ensure the directory is created and empty.
Future<void> emptyOrCreate() async {
await _fsDir.delete(recursive: true);
await _fsDir.create(recursive: true);
}
}
15 changes: 15 additions & 0 deletions fs/lib/utils/read_write.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,18 @@ Future<List<String>> readLines(File file, {Encoding encoding = utf8}) async {
var text = await readString(file, encoding: encoding);
return LineSplitter.split(text).toList();
}

/// Empty or create helper
extension DirectoryEmptyOrCreateExt on Directory {
/// Ensure the directory is created and empty.
Future<void> emptyOrCreate() async {
if (await exists()) {
try {
await delete(recursive: true);
} catch (_) {
// ignore
}
}
await create(recursive: true);
}
}
5 changes: 0 additions & 5 deletions fs/lib/utils/src/utils_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,6 @@ Future<int> copyFileMeta(File src, File dst) async {
return 0;
}

Future emptyOrCreateDirectory(Directory dir) async {
await dir.delete(recursive: true);
await dir.create(recursive: true);
}

abstract class EntityNode {
EntityNode? get parent; // can be null
FileSystem get fs; // cannot be null
Expand Down
13 changes: 13 additions & 0 deletions fs_test/lib/utils_read_write_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,18 @@ void defineTests(FileSystemTestContext ctx) {
await writeString(file, 'test2');
expect(await readString(file), 'test2');
});

test('Directory.emptyOrCreate', () async {
final top = await ctx.prepare();
var dir = fs.directory(fs.path.join(top.path, 'dir'));
expect(await dir.exists(), isFalse);
await dir.emptyOrCreate();
expect(await dir.exists(), isTrue);
// test 2 level depth
dir = fs.directory(fs.path.join(top.path, 'sub', 'dir'));
expect(await dir.exists(), isFalse);
await dir.emptyOrCreate();
expect(await dir.exists(), isTrue);
});
});
}

0 comments on commit bd27f6b

Please sign in to comment.