streamich / memfs

JavaScript file system utilities
http://streamich.github.io/memfs/
Apache License 2.0
1.72k stars 130 forks source link

toJson for links #359

Open amitgilad3 opened 5 years ago

amitgilad3 commented 5 years ago

when i create a volume with a link and then convert that volume into a json the link is turned into an actual file so when i load that json into a new volume it is no longer a link.

example

const { fs, Volume } = require('memfs');
const vol = new Volume;

vol.mkdirSync('/tmp/');
vol.writeFileSync('/tmp/bar','bar!!!')
vol.linkSync( '/tmp/bar', '/app');

console.log(vol.toJSON()) //{/tmp/bar: "bar!!!", /app: "bar!!!"}
vol.writeFileSync('/tmp/bar', 'kutner')
console.log(vol.toJSON()) //{/tmp/bar: "kutner", /app: "kutner"}

const vol2 = Volume.fromJSON(vol.toJSON());
console.log(vol2.toJSON()) //{/tmp/bar: "kutner", /app: "kutner"}
vol2.writeFileSync('/tmp/bar', 'amit')
console.log(vol2.toJSON())//{/tmp/bar: "amit", /app: "kutner"}

any ideas?

pizzafroide commented 5 years ago

Well, that's part of a larger issue that I want to address for a while (but had no time for this the last few months). The problem is in fact that toJSON function only exports the files content, and fromJSON is only able to 'mount' regular files and directories. I agree that toJSON (and fromJSON) should be able to export/import the whole files, that is their content and properties (type, ownership, permissions, times, and so forth). This would be a breaking change for some people though. I'm thinking of those who use Jest snapshots for instance. So this change should come with a way of configuring the current UID, GID, and times (among other things I guess). Because it should be possible to have a toJSON output independent from the current user, time, or machine. First and foremost, maybe adding options to toJSON function would suffice for this particular aspect of the issue. But I think that adding the ability to configure memfs would also be needed rather rapidly.

Anyway, help (and PR) are welcome! 😃

streamich commented 5 years ago

@vjpr has suggested that this advanced export/import functionality could even be a separate module, so it can be used with real fs, as well as with memfs.