File-handling from Pervasives: open_out, output_string, etc., for Javascript


#1

I was hoping to use some of the pervasives functions like open_out and output_string, but (like my experience with read_line), I’ve discovered they’re unimplemented when I’m using Bucklescript/Javascript. For read_line, a couple of folks pointed me to nice bindings of something that was sort-of-like read_line, and enough to get my program going.

Is there something similar for some of the file-handling (e.g., reading/writing) stuff, or am I going to have to learn some actual Javascript to get this done? :frowning:


#2

Are you targeting NodeJS to run as a scripting platform specifically? Or are you OK with targeting Reason Native? If the former, you usually need to know JavaScript and some of the interop layer between it and BuckleScript (the Reason-JavaScript compiler). If the latter, you’ll need to know about the native toolchain (esy, the Reason Native libraries). I believe the Reason Native Fs library will allow you to do file reading/writing etc.

Let us know more details and we can give more explicit pointers.


#3

Unfortunately, I’m already committed to using the Javascript version of things. I’m using ReasonML in a course, and while file-output isn’t necessary for the students, we (the staff) would like to use it in a few places, as well as doing things like finding all the files in some directory, etc. Perhaps, for the next go-around, I’ll shift to Reason Native, although I’ve already gotten really used to bsb, and like working with it; perhaps esy is equally easy to deal with, though.

So…I really AM looking for a Javascript version of things.

I’m also wondering why there was a choice made to NOT implement all of the Pervasives module. I grant that some of the functions in there aren’t written the way we might want to write them today, but if you don’t like that, you don’t have to use them, right? It just seems odd to leave them unimplemented.


#4

No problem, in that case check out this module that’s shipped with BuckleScript: https://bucklescript.github.io/bucklescript/api/Node.Fs.html

It has bindings to the basic synchronous file read/write APIs (readFileSync, writeFileSync). If you need async I/O you can write bindings, it’s not too difficult. Here’s an example: https://dev.to/yawaramin/comment/ge32

[EDIT: corrected the example link]


#5

Thanks very much – just what was needed.