euberdeveloper/dree

Feature request: Mutate Dree object (custom fields?)

Closed this issue · 3 comments

It would be great to be able to mutate the Dree object as it is scanned using the onFile and onDir callbacks. As far as I can tell the library doesn't currently do this. Similar to how node-directory-tree handles it

I'd be happy to make a PR but would need some pointers regarding the api and typings.

Thanks!

Hello @lightlii, sorry for the late answer.

As of now, the library itself passes the same object that will be added to the final directory tree, as you can see here.

This means, that if you edit the object on the callback "onFile" or "onDir", the result will actually be affected by those changes.

This does not count actually for the typings. In that case I think I will invent a way with type templates so that you can handle it also with typescript. If it is something that you need now, you can change the object and do some tricks with any/unknown, in the next days I will think about something.

Also, you made me have the idea that there could be something like "map" for the arrays, so that you could have something like this:

dree.scan('myPath', {}, onFile: node => ({ desc: `${node.name} (${node.size})` }));

so that it returns something like:

{
  desc: 'myfolder (39MB)',
  children: [
    {
      desc: 'myfile (34MB)'
    },
    {
      desc: 'myfolder (5MB)',
      children: [
        {
          desc: 'anotherFile (5MB)'
        },
        {
          desc: 'smallFile (5KB)'
        }
      ]
    }
  ]
}