Sandpack/nodebox-runtime

Problem writing a file

robtweed opened this issue · 5 comments

As an alternative to fetching an NPM package of files, I was trying to fetch a file from Github and save it locally:

async function importGithubFile(path) {

  // eg /index.html

  let url = 'https://raw.githubusercontent.com/robtweed/golgi-sbadmin/master/example';
  url = url + path;
  let res = await fetch(url);
  let text = await res.text();
  //console.log(text);
  await emulator.fs.writeFile('./server' + path, text);
  console.log(3333333);
}

console.log(8888888);
await importGithubFile('/index.html');
console.log(9999999);

Unfortunately this doesn't complete. If I un-comment the console.log(text) line, I can see that it has successfully fetched the contents of the file from Github, but the emulator.fs.writeFile() method appears to be failing.

I'm not seeing an error as such in the browser's console, but I don't see it showing the 33333 or 99999 in the console and it doesn't get any further in my logic.

Any ideas?

Interesting, might be that you need to create the parent directories, but really strange that the error does not come through to the client, this is definitely a bug we should tackle. Will log it internally as a high prio issue.

Also double checked and your dependency is synced with our CDN so nodebox should be able to automatically download it now :D

Some updated thoughts/observations:

It would be helpful if the nodebox.fs API included mkdir(). This is because nodebox.fs.writeFile fails if the path directory structure doesn't yet exist. (it works just fine if the path you're writing to already exists)

However, I've found that you can defer the creation of files so they're written within your index.js file - ie within the running Node.js environment once it's started. The Nodebox virtual file system can then be successfully manipulated using the standard Node.js fs API, so you can use fs.mkdir() to add new directories to the virtual file system and then fs.writeFile() to that extended directory structure.

Nevertheless, it would be nice to be able to fully pre-prepare the virtual file system outside the index.js file using nodebox.fs API

Another thought is to be able to customise the /nodebox/ directory structure via the nodebox.fs.init() method?

Hope this helps
Rob

Another thought is to be able to customise the /nodebox/ directory structure via the nodebox.fs.init() method?

You can already prepare the whole fs using fs.init: https://github.com/codesandbox/nodebox-runtime/blob/main/packages/nodebox/api.md#nodeboxfsinitfiles

Or would you like to have a custom homedir?

I see - so I can define a file, eg '/frontend/dummy.txt' in the fs.init() method which does indeed create the /nodebox/frontend directory for me.

That helps, but I still think an fs.mkdir() method and/or the ability to use fs.writeFile() so it automatically creates any missing directories in the path would be helpful in the nodebox.fs API

We just released a new version 0.1.2 which includes mkdir, readdir, stat and a recursive option for writeFile