/
tree.js
45 lines (37 loc) · 1.01 KB
/
tree.js
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
/**
* This example shows how to create an object representing a directory tree with basic ignore file support.
*/
'use strict'
if (typeof Promise === 'undefined')
global.Promise = require('bluebird')
var fs = require('fs'),
resolve = require('path').resolve,
adir = require('../'),
dir = resolve(__dirname, '../'),
gignore = [
'.DS_Store',
'.git',
'.idea'
],
ignore = fs.readFileSync(resolve(dir, '.gitignore'), 'utf8')
.split(/\n+|\r+/g)
.filter(Boolean)
.concat(gignore),
tree = {}
function onEntry(stats, subtree) {
var name = stats.basename
if (~ignore.indexOf(name))
// stop aggregation under the excluded folder
return adir.break
if (stats.isDirectory())
return subtree[ name ] = {}
else
subtree[ name ] = stats.size
}
function done(err) {
if (err)
console.error(err.stack)
else
console.log(tree)
}
adir(dir, onEntry, tree, done)