8000 jQuery for the fs: fsQuery? · Issue #1 · dominictarr/poset · GitHub
[go: up one dir, main page]

Skip to content

jQuery for the fs: fsQuery? #1

Open

Description

@dominictarr

jQuery is about selecting elements of a tree, based on their relationships,
and then applying actions on them, but what if you could work with the fs like this?

it would be very simple to express certain highlevel ideas.

Suppose, this expression might mean get all the modules requireable
from the current dir.

$('.../node_module/*/package.json')
  .parse(JSON.parse).unique(function(pkg) {return pkg.name})

sometimes /**/ means /*/ or /*/*/ or /*/*/*/ etc, so i'm extending .../
so that it means ./ and ../ and ../../ and ../../../ etc.

Then we open each file, then filter them, so you only get the first
one that you've seen.

This is exactly like the node_modules pattern, but expressed in one (longish) line.

could also do this

$('.../node_module/$MODULE_NAME/package.json')...

to find that file?

Also, there are some other constructions that would be very simple to do,
like -- rm -rf. rm stops you from unlinking a dir unless it's empty.
you must traverse the tree, (in topological sort order -- leaves first),
and delete each file, and then the containing directory.

// rm -rf dir
$('./dir/**/*').topologicalSort().unlink()

but, you could also delete just, say, the .coffee files.

$('./dir/**/*.coffee').topologicalSort().unlink()

This is the sort of thing that is awkward with the regular unix tools,
because you never know how consistent the recursive option is implemented,
indeed - it's part of each tool, rather than part of the shell.

cp -R could be implemented like this:

//traverse the files width first, to ensure that each dir is created as needed.
//depth first would also work.
$('./dir/**/*').widthFirst().each(function (item, cb) {
  var dest = path.resolve('./destDir', path.relative('./dir', item.filename))
  if(item.type == 'file') 
    fs.createReadStream(item.filename).pipe(fs.createWriteStream(dest))
    .on('close', cb).on('error', cb)
  else if(item.type == 'dir')
    fs.mkdir(dest, cb)
})

Of course, it would be just as easy to apply this selectively!

Activity

  1. dominictarr commented on Mar 20, 2013

    @dominictarr
    OwnerAuthor

    could also define an extended glob syntax like this:

    ./(node_modules/*)+/package.json which could be used to select the npm dependency tree at the current dir.
    here (...) and + have pretty much the same meaning as in regexp.

  2. refset commented on Mar 20, 2013

    @refset
    Collaborator

    I completely see the value in this. I'm not savvy enough to flesh out use-cases for developers working with fs but it will be so great to see that this same exact same API can work its magic with a distributed DB / thing.

  3. dominictarr commented on Mar 25, 2013

    @dominictarr
    OwnerAuthor

    hey @jez0990 what do you think about this: https://github.com/dominictarr/poset/blob/master/scratch.md

  4. refset commented on Mar 27, 2013

    @refset
    Collaborator

    I like the looks of it. I don't completely understand the syntax decisions but it is pretty intuitive to read!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0