memfs icon indicating copy to clipboard operation
memfs copied to clipboard

toJson for links

Open amitgilad3 opened this issue 6 years ago • 2 comments

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?

amitgilad3 avatar Apr 04 '19 13:04 amitgilad3

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! 😃

pizzafroide avatar Apr 08 '19 08:04 pizzafroide

@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.

  • https://github.com/streamich/memfs/issues/48#issuecomment-340409439

streamich avatar Apr 08 '19 08:04 streamich