Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: implement writeFile and writeFileSync #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/fs.re
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,36 @@ let writeString = (~fd, ~string, ~offset, ~length) =>
let writeStringSync = (~fd, ~string, ~offset, ~length) =>
Unix.write_substring(fd, string, offset, length);

let writeFile = (~file, ~data, ~options) => ();

let writeFileSync = (~file, ~data, ~options) => ();
type writeFileOptions = {
encoding: option(string),
mode: option(asyncFilePerm),
flags: list(asyncOpenFlag)
};

let defaultWriteFileFlags = [Unix.O_CREAT, Unix.O_TRUNC];

let writeFile = (~options={encoding: None, mode: None, flags: []}, ~file, ~data) => {
let flags = List.length(options.flags) < 1 ? defaultWriteFileFlags : options.flags;
let mode = RenodeUtils.withDefault(options.mode, ~default=438);
Lwt.bind(
Lwt_unix.openfile(file, flags, mode),
fd => Lwt.bind(
write(fd, data, 0, Bytes.length(data)),
(_) => close(fd)
)
)
};

type writeFileSyncOptions = {
encoding: option(string),
mode: option(syncFilePerm),
flags: list(syncOpenFlag)
};

let writeFileSync = (~options={encoding: None, mode: None, flags: []}, ~file, ~data) => {
let flags = List.length(options.flags) < 1 ? defaultWriteFileFlags : options.flags;
let mode = RenodeUtils.withDefault(options.mode, ~default=438);
let fd = Unix.openfile(file, flags, mode);
let _ = writeSync(fd, data, 0, Bytes.length(data));
closeSync(fd);
};
38 changes: 17 additions & 21 deletions src/fs.rei
Original file line number Diff line number Diff line change
Expand Up @@ -363,24 +363,20 @@ let writeString:
let writeStringSync:
(~fd: syncFileDescr, ~string: string, ~offset: int, ~length: int) => int;

[@ocaml.deprecated
{|
Fs.writeFile has yet to be implemented.
Please open a Work-In-Progress pull request if you are interested in contributing.
We will help answer questions and push you in the right direction.

Repo URL: https://github.com/kennetpostigo/lwt-node
|}
]
let writeFile: (~file: 'a, ~data: 'b, ~options: 'c) => unit;

[@ocaml.deprecated
{|
Fs.writeFileSync has yet to be implemented.
Please open a Work-In-Progress pull request if you are interested in contributing.
We will help answer questions and push you in the right direction.

Repo URL: https://github.com/kennetpostigo/lwt-node
|}
]
let writeFileSync: (~file: 'a, ~data: 'b, ~options: 'c) => unit;
type writeFileOptions = {
encoding: option(string),
mode: option(asyncFilePerm),
flags: list(asyncOpenFlag)
};

let writeFile:
(~options: writeFileOptions=?, ~file: string, ~data: bytes) => Node.t(unit);

type writeFileSyncOptions = {
encoding: option(string),
mode: option(syncFilePerm),
flags: list(syncOpenFlag)
};

let writeFileSync:
(~options: writeFileSyncOptions=?, ~file: string, ~data: bytes) => unit;
5 changes: 5 additions & 0 deletions src/renodeUtils.re
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,9 @@ let iterPathUntil = (~condition, ~list, ~f) => {
f(arr[position^], position, ! (position^ > (-1)));
position := position^ - 1;
};
};

let withDefault = (opt, ~default) => switch opt {
| Some(v) => v
| None => default
};