wch / sassy

A new Sass package for R
5 stars 0 forks source link

Replace JS utilities/shims with R calls #2

Open andrjohns opened 4 months ago

andrjohns commented 4 months ago

I've updated QuickJSR to add functionality for calling R functions from JS code, which could help make some of the utility functions used here a bit more robust.

For example, the current fileExists function:

export function fileExists(filePath) {
  try {
    const stat = os.stat(filePath);
    if (stat === null) {
      return false;
    }
    return (stat[0].mode & os.S_IFMT) === os.S_IFREG;
  } catch (err) {
    if (err.errno === os.ENOENT) {
      return false;
    }
    throw err;
  }
}

Could instead just be:

export function fileExists(filePath) {
  return R.package("base")["file.exists"](filePath);
}
wch commented 3 months ago

Thanks - it's great to be able to call R functions from JS, but for the specific cases here I actually like using the quickjs APIs for accessing the filesystem.

Just wondering: have you taken a look at the WebR APIs for calling R from JavaScript? It might be worth looking at. It would be great if the APIs were similar, but I can also understand if the WebR APIs are more complex than what you'd want to implement for QuickJSR.