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 path = require('path')
var promise = require('promise')
@ -56,6 +57,7 @@ function(path, make){
})
}
// XXX might be good to add some caching...
var listDirfs =
module.listDirfs =
function(path, make){
@ -64,7 +66,28 @@ function(path, make){
// XXX the windows root path must have a trailing '/'
path = /^[a-zA-Z]:$/.test(path.trim()) ? path+'/' : path
// XXX expose these as config...
var fullpath = false
var showfiles = true
// get the drive list on windows...
if(os.type() == 'Windows_NT' && path == '/'){
return new promise(function(resolve, reject){
// NOTE: this is a bit brain-dead but it does the job done
// and faster than fancy modules like drivelist...
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.split('')
.forEach(function(drive){
// XXX error handling???
if(fs.existsSync(drive+':/')){
make(drive+':/')
}
})
resolve()
})
// list dirs...
} else {
var stat = promise.denodeify(fs.stat)
return new promise(function(resolve, reject){
@ -87,13 +110,22 @@ function(path, make){
if(!res){
return
}
var dir = res.isDirectory()
if(!dir && !showfiles) {
return
}
make(fullpath
? path +'/'+ file
: file + (res.isDirectory() ? '/' : ''))
: 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()
@ -102,6 +134,7 @@ function(path, make){
})
})
})
}
}
// NOTE: this should work from a chrome app and does not require anything