Skip to content

Instantly share code, notes, and snippets.

@idkjs
Last active October 15, 2019 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idkjs/c48dda9f1dba9ae17080403e3bda973f to your computer and use it in GitHub Desktop.
Save idkjs/c48dda9f1dba9ae17080403e3bda973f to your computer and use it in GitHub Desktop.
module Encoding = {
type norm =
| HEX
| UTF8
| ASCII
| LATIN1
| BASE64;
let getNorm = (e: norm) =>
switch (e) {
| UTF8 => "utf8"
| HEX => "hex"
| ASCII => "ascii"
| LATIN1 => "latin1"
| BASE64 => "base64"
};
};
let dirname: option(string) = [%bs.node __dirname];
[@bs.module "path"] external resolve: (string, string) => 'path = "resolve";
[@bs.module "fs"]
external fsReadFile: ('test, string, 'callback) => 'nodeCallback = "readFile";
let readFile = (path: string, norm: Encoding.norm, callback) =>
fsReadFile(path, Encoding.getNorm(norm), callback);
// let dirname = Some("./src/test.txt");
switch (dirname) {
| None => print_endline("no __dirname")
| Some(path) =>
readFile(resolve(path, "test.txt"), UTF8, (err, result) =>
err ? print_endline("Error") : print_endline(result)
)
};
This is text from text.txt.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment