Repository navigation
Expand file tree
/
Copy pathfs.js
More file actions
54 lines (47 loc) · 1.38 KB
/
Copy pathfs.js
File metadata and controls
54 lines (47 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var Poset = require('./')
var fs = require('fs')
var path = require('path')
var FsPoset = module.exports = (function (root) {
var stats = {}
return Poset({
get: function (file, cb) {
if(file[0] == '.' || file[0] !== '/')
var file = path.resolve(file)
if(stats[file])
return cb(null, stats[file])
fs.lstat(file, function (err, stat) {
if(err) throw err
stats[file] = stat
stat.id = stat.filename = file
stat.type =
stat.isFile() ? 'file'
: stat.isDirectory() ? 'dir'
: stat.isBlockDevice() ? 'block'
: stat.isCharacterDevice() ? 'char'
: stat.isSymbolicLink() ? 'link'
: stat.isSocket() ? 'socket'
: (function () { throw new Error('unknown file type')})()
cb(err, stat)
})
},
children: function (cb) {
var value = this.value
if(value.type != 'dir') return cb(null, [])
var filename = value.filename
fs.readdir(value.filename, function (err, ls) {
if(err) throw err
cb(null, ls.map(function (file) {
return path.resolve(filename, file)
}))
})
}
})
})()
if(! module.parent) {
new FsPoset().get(process.cwd(), function (err, item) {
item.topo(function (item) {
if(item)
console.log(item.value.filename)
})
})
}