added windows drive listing...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2015-10-12 02:29:08 +03:00
parent 6b5a3828a2
commit 66634f3316

View File

@ -4,6 +4,7 @@
* *
**********************************************************************/ **********************************************************************/
var os = require('os')
var fs = require('fs') var fs = require('fs')
var path = require('path') var path = require('path')
var promise = require('promise') var promise = require('promise')
@ -56,6 +57,7 @@ function(path, make){
}) })
} }
// XXX might be good to add some caching...
var listDirfs = var listDirfs =
module.listDirfs = module.listDirfs =
function(path, make){ function(path, make){
@ -64,44 +66,75 @@ function(path, make){
// XXX the windows root path must have a trailing '/' // XXX the windows root path must have a trailing '/'
path = /^[a-zA-Z]:$/.test(path.trim()) ? path+'/' : path path = /^[a-zA-Z]:$/.test(path.trim()) ? path+'/' : path
// XXX expose these as config...
var fullpath = false var fullpath = false
var stat = promise.denodeify(fs.stat) var showfiles = true
return new promise(function(resolve, reject){ // get the drive list on windows...
fs.readdir(path, function(err, files){ if(os.type() == 'Windows_NT' && path == '/'){
// XXX return new promise(function(resolve, reject){
if(err){ // NOTE: this is a bit brain-dead but it does the job done
reject(err) // and faster than fancy modules like drivelist...
return 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
} .split('')
var res = [] .forEach(function(drive){
// XXX error handling???
if(fs.existsSync(drive+':/')){
make(drive+':/')
}
})
resolve()
})
files.map(function(file){ // list dirs...
return stat(path +'/'+ file) } else {
.catch(function(err){ var stat = promise.denodeify(fs.stat)
make(fullpath
? path +'/'+ file return new promise(function(resolve, reject){
: file, null, true) fs.readdir(path, function(err, files){
}) // XXX
.then(function(res){ if(err){
if(!res){ reject(err)
return return
} }
make(fullpath var res = []
? path +'/'+ file
: file + (res.isDirectory() ? '/' : '')) files.map(function(file){
}) return stat(path +'/'+ file)
// NOTE: we are not using promise.all(..) here because it .catch(function(err){
// triggers BEFORE the first make(..) is called... make(fullpath
.then(function(){ ? path +'/'+ file
res.push(file) : file, null, true)
if(res.length == files.length){ })
resolve() .then(function(res){
} if(!res){
}) return
}
var dir = res.isDirectory()
if(!dir && !showfiles) {
return
}
make(fullpath
? path +'/'+ file
: file + (dir ? '/' : ''))
})
// NOTE: we are not using promise.all(..) here because it
// triggers BEFORE the first make(..) is called...
// ...not sure I fully understand why...
.then(function(){
// NOTE: this will get called for all results
// including ones that generate errors, not
// sure if this is a bug in .denodeify(..)
// or by-design though...
res.push(file)
if(res.length == files.length){
resolve()
}
})
})
}) })
}) })
}) }
} }
// NOTE: this should work from a chrome app and does not require anything // NOTE: this should work from a chrome app and does not require anything