Skip to content

Commit

Permalink
wip: implement writeFile and writeFileSync
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Molina <[email protected]>
  • Loading branch information
erizocosmico committed Jan 8, 2018
1 parent 2102528 commit aa21361
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
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);
};
30 changes: 12 additions & 18 deletions src/fs.rei
Original file line number Diff line number Diff line change
Expand Up @@ -363,24 +363,18 @@ 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.
type writeFileOptions = {
encoding: option(string),
mode: option(asyncFilePerm),
flags: list(asyncOpenFlag)
};

Repo URL: https://github.com/kennetpostigo/reason-node
|}
]
let writeFile: (~file: 'a, ~data: 'b, ~options: 'c) => unit;
let writeFile: (~options: writeFileOptions=?, ~file: string, ~data: bytes) => Node.t(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.
type writeFileSyncOptions = {
encoding: option(string),
mode: option(syncFilePerm),
flags: list(syncOpenFlag)
};

Repo URL: https://github.com/kennetpostigo/reason-node
|}
]
let writeFileSync: (~file: 'a, ~data: 'b, ~options: 'c) => unit;
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
};

0 comments on commit aa21361

Please sign in to comment.