remove method

  1. @override
Future<Result<void, FileError>> remove(
  1. String path, {
  2. bool recursive = false,
  3. bool force = false,
})
override

Removes a file or directory. With force, a missing path is not an error.

Implementation

@override
Future<Result<void, FileError>> remove(
  String path, {
  bool recursive = false,
  bool force = false,
}) async {
  final resolved = _resolve(path);
  try {
    final type = await FileSystemEntity.type(resolved, followLinks: false);
    if (type == FileSystemEntityType.notFound) {
      if (force) return const Ok(null);
      return Err(
        FileError(
          FileErrorCode.notFound,
          'No such file or directory',
          path: resolved,
        ),
      );
    }
    if (type == FileSystemEntityType.directory) {
      await Directory(resolved).delete(recursive: recursive);
    } else {
      await File(resolved).delete();
    }
    return const Ok(null);
  } on Object catch (error) {
    if (error is FileSystemException && !recursive) {
      return Err(
        FileError(
          FileErrorCode.invalid,
          error.message,
          path: resolved,
          cause: error,
        ),
      );
    }
    return Err(_toFileError(error, resolved));
  }
}