Skip to content

Commit 92f7bd1

Browse files
authored
Merge pull request #1 from TheSpyder/upstream_patches
Upstream patches
2 parents 8ba03d7 + 86073c9 commit 92f7bd1

File tree

3 files changed

+43
-39
lines changed

3 files changed

+43
-39
lines changed

examples/StreamTextToFile.re

Lines changed: 0 additions & 27 deletions
This file was deleted.

examples/StreamTextToFile.res

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Normally `open NodeJs` would be used to shorten the module accessors
2+
// this is not done in the example to make it clear where things come from
3+
4+
let data = "Sample text to write to a file!"->NodeJs.Buffer.fromString
5+
let process = NodeJs.Process.process
6+
7+
let outputPath = NodeJs.Path.relative(~from=NodeJs.Process.cwd(process), ~to_="example__output.txt")
8+
9+
let writeStream = NodeJs.Fs.createWriteStream(outputPath)
10+
11+
let logErrorIfExists = maybeError =>
12+
switch Js.Nullable.toOption(maybeError) {
13+
| Some(err) => Js.log2("An error occurred", err)
14+
| None => ()
15+
}
16+
17+
let () =
18+
writeStream
19+
->NodeJs.Stream.writeWith(
20+
data,
21+
~callback=maybeError => {
22+
logErrorIfExists(maybeError)
23+
Js.log("Finished")
24+
},
25+
(),
26+
)
27+
->ignore

src/Fs.res

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,14 @@ type fd = private int
196196

197197
type writeFileOptions
198198
@obj
199-
external writeFileOptions: (~mode: int=?, ~flag: string=?, unit) => writeFileOptions = ""
199+
external writeFileOptions: (~mode: int=?, ~flag: string=?, ~encoding: string=?, unit) => writeFileOptions = ""
200200

201201
type appendFileOptions
202202
@obj
203-
external appendFileOptions: (~mode: int=?, ~flag: Flag.t=?, unit) => appendFileOptions = ""
203+
external appendFileOptions: (~mode: int=?, ~flag: Flag.t=?, ~encoding: string=?, unit) => appendFileOptions = ""
204204

205205
type readFileOptions
206-
@obj external readFileOptions: (~flag: Flag.t=?, unit) => readFileOptions = ""
206+
@obj external readFileOptions: (~flag: Flag.t=?, ~encoding: string=?, unit) => readFileOptions = ""
207207

208208
@ocaml.doc("
209209
* `readdirSync(path)`
@@ -246,14 +246,10 @@ external openSyncWith: (string, ~flag: Flag.t=?, ~mode: int=?) => fd = "openSync
246246
external readFileSync: (string, ~options: readFileOptions=?, unit) => Buffer.t = "readFileSync"
247247
@module("fs") external existsSync: string => bool = "existsSync"
248248

249-
type writeFileSyncOptions
250-
@obj
251-
external writeFileSyncOptions: (~mode: int=?, ~flag: Flag.t=?, unit) => writeFileSyncOptions = ""
252-
253249
@val @module("fs")
254250
external writeFileSync: (string, Buffer.t) => unit = "writeFileSync"
255251
@val @module("fs")
256-
external writeFileSyncWith: (string, Buffer.t, writeFileSyncOptions) => unit = "writeFileSync"
252+
external writeFileSyncWith: (string, Buffer.t, writeFileOptions) => unit = "writeFileSync"
257253

258254
module FileHandle = {
259255
type t
@@ -307,7 +303,7 @@ module FileHandle = {
307303

308304
type writeFileOptions
309305
@obj
310-
external writeFileOptions: (~mode: int=?, ~flag: Flag.t=?, unit) => writeFileOptions = ""
306+
external writeFileOptions: (~mode: int=?, ~flag: Flag.t=?, ~encoding: string=?, unit) => writeFileOptions = ""
311307

312308
@send
313309
external writeFile: (t, Buffer.t) => Js.Promise.t<unit> = "writeFile"
@@ -373,11 +369,17 @@ external mkdir: (string, mkdirOptions) => Js.Promise.t<unit> = "mkdir"
373369
@module("fs") @scope("promises")
374370
external mkdirWith: (string, mkdirOptions) => Js.Promise.t<unit> = "mkdir"
375371

372+
@module("fs")
373+
external mkdirSync: string => unit = "mkdirSync"
374+
375+
@module("fs")
376+
external mkdirSyncWith: (string, mkdirOptions) => unit = "mkdirSync"
377+
376378
type mkdtempOptions
377-
@obj external mdktempOptions: unit => mkdtempOptions = ""
379+
@obj external mdktempOptions: (~encoding: string=?, unit) => mkdtempOptions = ""
378380

379381
@module("fs") @scope("promises")
380-
external mkdtemp: (string, mkdtempOptions) => Js.Promise.t<string> = "mkddtemp"
382+
external mkdtemp: (string) => Js.Promise.t<string> = "mkddtemp"
381383

382384
@module("fs") @scope("promises")
383385
external mkdtempWith: (string, mkdtempOptions) => Js.Promise.t<string> = "mkddtemp"
@@ -448,6 +450,7 @@ type createReadStreamOptions
448450
@obj
449451
external createReadStreamOptions: (
450452
~flags: string=?,
453+
~encoding: string=?,
451454
~fd: fd=?,
452455
~mode: int=?,
453456
~autoClose: bool=?,
@@ -468,14 +471,15 @@ type createWriteStreamOptions
468471
@obj
469472
external createWriteStreamOptions: (
470473
~flags: string=?,
474+
~encoding: string=?,
471475
~fd: fd=?,
472476
~mode: int=?,
473477
~autoClose: bool=?,
474478
~emitClose: bool=?,
475479
~start: int=?,
476480
~fs: {..}=?,
477481
unit,
478-
) => createReadStreamOptions = ""
482+
) => createWriteStreamOptions = ""
479483
@module("fs")
480484
external createWriteStream: string => WriteStream.t = "createWriteStream"
481485

0 commit comments

Comments
 (0)