Search code examples
javascriptshellspidermonkey

Opposite of `read`/`snarf` in Spidermonkey js shell, function to write a string to a file?


Spidermonkey's js shell has a nice feature called read/snarf that can read a file from disk. Is there an equivalent way to write files? Nothing like this appears available in the documentation but perhaps someone knows of some undocumented way to do this.

I'm looking for a way to get a large string out of an interactive js session. In the browser/Firebug, I'd be able to use copy to copy a string to clipboard, or add it to the DOM to copy. In node, I'd use fs to write a file.


Solution

  • You can use redirect and putstr functions exposed by the shell like this:

    const previous = redirect('path/to/your/file')
    putstr('stuff to be written')
    redirect(previous) // restore the redirection to stdout
    

    Or you can use os.file.writeTypedArrayToFile to write typed arrays.