forked from plasma-umass/browsix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xhrfs-index
executable file
·104 lines (88 loc) · 2.45 KB
/
xhrfs-index
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env node
// based on jvilk's XHRIndexer, but in plain JS using async APIs
'use strict';
var fs = require('fs');
var path = require('path');
var exclude = [
// /^\./, // dotfiles
/^bower_components$/,
/^node_modules$/,
/^build$/,
/^dist$/,
/~$/, // emacs backups
];
function shouldExclude(name) {
for (var i = 0; i < exclude.length; i++) {
if (name.match(exclude[i]))
return true;
}
return false;
}
var outstanding = 1;
var tree = {};
function subtreeAt(relative) {
if (!relative)
return tree;
var parts = relative.split(path.sep);
var subtree = tree;
// start at 1 to skip the leading /
for (var i = 1; i < parts.length; i++)
subtree = subtree[parts[i]];
return subtree;
}
function maybeFinished(file, err, onComplete) {
if (--outstanding === 0) {
onComplete(err, tree);
}
}
function walkDir(base, curr, onComplete) {
var parent = curr.slice(base.length);
fs.readdir(curr, function(err, files) {
if (err) {
process.stderr.write('error reading dir: ' + err);
maybeFinished(curr, err, onComplete);
return;
}
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (shouldExclude(file))
continue;
outstanding++;
var absFile = path.join(curr, file)
fs.stat(absFile, function statFile(absFile, err, stats) {
if (err) {
maybeFinished(absFile, err, onComplete);
return;
}
var parts = absFile.split(path.sep);
var file = parts[parts.length-1];
var subtree = subtreeAt(parent);
if (stats.isDirectory()) {
subtree[file] = {};
walkDir(base, absFile, onComplete);
} else {
subtree[file] = null;
maybeFinished(absFile, null, onComplete);
}
}.bind(null, absFile));
}
maybeFinished(curr, null, onComplete);
});
}
function main() {
var pathToNode = process.argv[0];
var pathToScript = process.argv[1];
var args = process.argv.slice(2);
var root = process.cwd();
if (args.length > 0)
root = args[0];
walkDir(root, root, function writeJSON(err, tree) {
if (err) {
process.stderr.write('error: ' + err + '\n');
process.exit(-1);
return;
}
process.stdout.write(JSON.stringify(tree) + '\n');
});
}
main();