node.js fs.readdir recursive directory search

前端 未结 30 2082
醉酒成梦
醉酒成梦 2020-11-22 15:55

Any ideas on an async directory search using fs.readdir? I realise that we could introduce recursion and call the read directory function with the next directory to read, bu

30条回答
  •  面向向阳花
    2020-11-22 16:09

    check out loaddir https://npmjs.org/package/loaddir

    npm install loaddir

      loaddir = require('loaddir')
    
      allJavascripts = []
      loaddir({
        path: __dirname + '/public/javascripts',
        callback: function(){  allJavascripts.push(this.relativePath + this.baseName); }
      })
    

    You can use fileName instead of baseName if you need the extension as well.

    An added bonus is that it will watch the files as well and call the callback again. There are tons of configuration options to make it extremely flexible.

    I just remade the guard gem from ruby using loaddir in a short while

提交回复
热议问题