Skip to content

Commit

Permalink
update functions for 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bcpeinhardt committed Jun 1, 2024
1 parent 866c588 commit 67587b5
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 117 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
## Unreleased

## v2.0.0 - 1 June 2024
- Fix mapping of fileinfo attr name in JS
- Add underlying string as context to `Unknown` error
- `verify_is_directory`, `verify_is_file`, `verify_is_symlink` become
`is_directory`, `is_file`, `is_symlink respectively`. Deprecated `is_file` and `is_directory` removed.
- Fix mapping of `FileInfo` attr names in JS
- Add underlying `String` as context to `Unknown` error
- improve performance of `get_files`

## v1.7.0 - 5 April 2024
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ You *should* use simplifile if
If you think you need a different solutions, these projects may be helpful:
[File streams (erlang target)](https://github.com/richard-viney/file_streams)

## Upgrading to 2.0

Please consult the changelog, but basically:
The deprecated `is_file`, `is_directory` functions have been removed.
The `verify_is_file`, `verify_is_directory`, and `verify_is_symlink` have had their verify_ prefixes removed (now `is_file`, `is_directory`, `is_symlink` respectively).
The `Unknown` variant of `FileError` now has an inner `String`.

## Example
```gleam
let filepath = "./test/hello.txt"
Expand Down
52 changes: 11 additions & 41 deletions src/simplifile.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -333,25 +333,14 @@ pub fn append_bits(
|> cast_error
}

/// Checks if the provided filepath is a directory
/// ## Example
/// ```gleam
/// let assert True = is_directory("./test")
/// ```
///
@deprecated("Use `verify_is_directory` instead")
pub fn is_directory(filepath: String) -> Bool {
do_is_directory(filepath)
}

/// Checks if the provided filepath exists and is a directory.
/// Returns an error if it lacks permissions to read the directory.
///
/// ## Example
/// ```gleam
/// let assert Ok(True) = verify_is_directory("./test")
/// let assert Ok(True) = is_directory("./test")
/// ```
pub fn verify_is_directory(filepath: String) -> Result(Bool, FileError) {
pub fn is_directory(filepath: String) -> Result(Bool, FileError) {
do_verify_is_directory(filepath)
|> cast_error
}
Expand Down Expand Up @@ -403,22 +392,15 @@ pub fn read_directory(at path: String) -> Result(List(String), FileError) {
|> cast_error
}

/// Returns `True` if there is a file at the given path, false otherwise.
///
@deprecated("Use `verify_is_file` instead")
pub fn is_file(filepath: String) -> Bool {
do_is_file(filepath)
}

/// Checks if the file at the provided filepath exists and is a file.
/// Returns an Error if it lacks permissions to read the file.
///
/// ## Example
/// ```gleam
/// let assert Ok(True) = verify_is_file("./test.txt")
/// let assert Ok(True) = is_file("./test.txt")
/// ```
///
pub fn verify_is_file(filepath: String) -> Result(Bool, FileError) {
pub fn is_file(filepath: String) -> Result(Bool, FileError) {
do_verify_is_file(filepath)
|> cast_error
}
Expand All @@ -436,10 +418,10 @@ fn do_verify_is_file(filepath: String) -> Result(Bool, FileError)
///
/// ## Example
/// ```gleam
/// let assert Ok(True) = verify_is_symlink("./symlink")
/// let assert Ok(True) = is_symlink("./symlink")
/// ```
///
pub fn verify_is_symlink(filepath: String) -> Result(Bool, FileError) {
pub fn is_symlink(filepath: String) -> Result(Bool, FileError) {
do_verify_is_symlink(filepath)
|> cast_error
}
Expand All @@ -458,9 +440,9 @@ fn do_verify_is_symlink(filepath: String) -> Result(Bool, FileError)
pub fn create_file(at filepath: String) -> Result(Nil, FileError) {
case
filepath
|> verify_is_file,
|> is_file,
filepath
|> verify_is_directory
|> is_directory
{
Ok(True), _ | _, Ok(True) -> Error(Eexist)
_, _ -> write_bits(<<>>, to: filepath)
Expand Down Expand Up @@ -517,7 +499,7 @@ fn do_copy_directory(src: String, dest: String) -> Result(Nil, FileError) {
let src_path = filepath.join(src, segment)
let dest_path = filepath.join(dest, segment)

case verify_is_file(src_path), verify_is_directory(src_path) {
case is_file(src_path), is_directory(src_path) {
Ok(True), Ok(False) -> {
// For a file, create the file in the new directory
use content <- result.try(read_bits(src_path))
Expand Down Expand Up @@ -567,11 +549,11 @@ pub fn get_files(in directory: String) -> Result(List(String), FileError) {
use acc, content <- list.try_fold(over: contents, from: [])
let path = filepath.join(directory, content)

case verify_is_file(path) {
case is_file(path) {
Error(e) -> Error(e)
Ok(True) -> Ok([path, ..acc])
Ok(False) ->
case verify_is_directory(path) {
case is_directory(path) {
Error(e) -> Error(e)
Ok(False) -> Ok(acc)
Ok(True) -> {
Expand Down Expand Up @@ -728,10 +710,6 @@ fn do_write_bits(content: BitArray, to filepath: String) -> Result(Nil, String)
@external(javascript, "./simplifile_js.mjs", "appendBits")
fn do_append_bits(content: BitArray, to filepath: String) -> Result(Nil, String)

@target(javascript)
@external(javascript, "./simplifile_js.mjs", "isDirectory")
fn do_is_directory(filepath: String) -> Bool

@target(javascript)
@external(javascript, "./simplifile_js.mjs", "makeDirectory")
fn do_make_directory(filepath: String) -> Result(Nil, String)
Expand Down Expand Up @@ -867,10 +845,6 @@ fn cast_error(input: Result(a, FileError)) -> Result(a, FileError) {
input
}

@target(erlang)
@external(erlang, "filelib", "is_dir")
fn do_is_directory(path: String) -> Bool

@target(erlang)
@external(erlang, "simplifile_erl", "make_directory")
fn do_make_directory(directory: String) -> Result(Nil, FileError)
Expand All @@ -883,10 +857,6 @@ fn do_make_symlink(target: String, symlink: String) -> Result(Nil, FileError)
@external(erlang, "simplifile_erl", "list_directory")
fn do_read_directory(directory: String) -> Result(List(String), FileError)

@external(erlang, "simplifile_erl", "is_file")
@external(javascript, "./simplifile_js.mjs", "isFile")
fn do_is_file(filepath: String) -> Bool

@target(erlang)
@external(erlang, "simplifile_erl", "create_dir_all")
fn do_create_dir_all(dirpath: String) -> Result(Nil, FileError)
Expand Down
8 changes: 1 addition & 7 deletions src/simplifile_erl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

%% API
-export([read_file/1, append_file/2, write_file/2, delete_file/1, delete_directory/1,
recursive_delete/1, list_directory/1, make_directory/1, make_symlink/2,
is_file/1, create_dir_all/1, rename_file/2, set_permissions/2,
recursive_delete/1, list_directory/1, make_directory/1, make_symlink/2, create_dir_all/1, rename_file/2, set_permissions/2,
is_valid_directory/1, is_valid_file/1, is_valid_symlink/1, file_info/1]).

-include_lib("kernel/include/file.hrl").
Expand Down Expand Up @@ -117,10 +116,6 @@ delete_directory(Dir) ->
recursive_delete(Dir) ->
posix_result(file:del_dir_r(Dir)).

%% Checks whether a given file exists and is a file (as opposed to a directory)
is_file(Filename) ->
not (file:read_file_info(Filename) == {error, enoent}) and not filelib:is_dir(Filename).

%% Creates the entire path for a given directory to exist
create_dir_all(Filename) ->
posix_result(filelib:ensure_dir(Filename)).
Expand Down Expand Up @@ -213,4 +208,3 @@ file_info_result(Result) ->

file_info(Filename) ->
file_info_result(file:read_file_info(Filename, [{time, posix}])).

25 changes: 0 additions & 25 deletions src/simplifile_js.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import fs from "node:fs"
import path from "node:path"
import process from "node:process"
import { BitArray, Ok, Error as GError, toList} from "./gleam.mjs";
import { verify_is_directory } from "./simplifile.mjs";

/**
* Read the contents of a file as a BitArray
Expand Down Expand Up @@ -41,18 +40,6 @@ export function appendBits(contents, filepath) {
return gleamResult(() => fs.appendFileSync(path.normalize(filepath), contents.buffer))
}

/**
* Check whether a file exists at the given path
*
* @deprecated Use `isValidFile` instead
* @param {string} filepath
* @returns {boolean}
*/
export function isFile(filepath) {
let fp = path.normalize(filepath)
return fs.existsSync(fp) && fs.statSync(fp).isFile();
}

/**
* Check whether a file exists at the given path
*
Expand Down Expand Up @@ -89,18 +76,6 @@ export function isValidSymlink(filepath) {
}
}

/**
* Check whether a directory exists at the given path
*
* @deprecated Use `isValidDirectory` instead
* @param {string} filepath
* @returns {boolean}
*/
export function isDirectory(filepath) {
let fp = path.normalize(filepath)
return fs.existsSync(fp) && fs.statSync(fp).isDirectory();
}

/**
* Check whether a directory exists at the given path
*
Expand Down
Loading

0 comments on commit 67587b5

Please sign in to comment.