-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
298 lines (274 loc) · 6.59 KB
/
index.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
'use strict';
/**
* @file dir2xml main
* @module dir2xml
* @version 1.3.0
* @author hex7c0 <hex7c0@gmail.com>
* @copyright hex7c0 2014
* @license GPLv3
*/
/*
* initialize module
*/
var path = require('path');
var fs = require('fs');
/*
* functions
*/
/**
* function wrapper for multiple require
*
* @function wrapper
* @param {Object} my - options
* @return {Object}
*/
function wrapper(my) {
var crypto = require('crypto').createHash;
var STORY = Object.create(null);
var header = '<?xml version="1.0" encoding="UTF-8"?><home>';
var footer = '</home>';
/**
* calculate hash of file
*
* @function checksum
* @param {Object|false} obj - object output
* @param {String} file - absolute path name
* @return {Object|String}
*/
function checksum(obj, file) {
var check = crypto(my.hash);
var ff = fs.readFileSync(file);
check.update(ff);
if (obj) {
obj.hash = check.digest('hex');
return obj;
}
return '<hash>' + check.digest('hex') + '</hash>';
}
/**
* end of work with cache
*
* @function cached
* @param {String|Object} h - header
* @param {String|null} f - footer
* @return {String}
*/
function cached(h, f) {
var stat;
// cache
if (my.cache && STORY.root === my.root) {
stat = fs.statSync(my.root);
if (stat && STORY.mtime === stat.mtime.getTime()
&& STORY.json === my.json && STORY.hash === my.hash) {
return STORY.body;
}
STORY = Object.create(null);
}
// build
var body;
body = dir_sync(my.root, h, '');
if (f) {
body += f;
}
if (my.cache) {
stat = fs.statSync(my.root);
if (stat) {
STORY.root = my.root;
STORY.body = body;
STORY.mtime = stat.mtime.getTime();
STORY.json = my.json;
STORY.hash = my.hash;
}
}
return body;
}
/**
* end of work
*
* @function simple
* @param {String|Object} h - header
* @param {String|null} f - footer
* @return {String}
*/
function simple(h, f) {
// build
var body = dir_sync(my.root, h, '');
return f ? body + f : body;
}
/**
* json builder
*
* @function json
* @param {String} head - header
* @param {String} after - post header
* @param {String} name - path name
* @param {String} abs - absolute path name
* @param {Object} stats - stats of file
* @param {Boolean} dir - flag for directory
* @param {String} root - root name
* @return {Array}
*/
function json(head, after, name, abs, stats, dir, root) {
var index;
var o;
if (dir) {
index = CCdir++;
} else {
index = CCfile++;
}
if (head[root]) {
o = head[root][index] = Object.create(null);
} else {
head[root] = Object.create(null);
o = head[root][index] = Object.create(null);
}
o.name = name;
o.ctime = stats.ctime.getTime();
o.mtime = stats.mtime.getTime();
if (dir) {
o.type = 'dir';
} else {
o.atime = stats.atime.getTime();
o.size = stats.size;
o = hash(o, abs);
o.type = 'file';
}
return [ head, null ];
}
/**
* xml builder
*
* @function xml
* @param {String} head - header
* @param {String} after - post header
* @param {String} name - path name
* @param {String} abs - absolute path name
* @param {Object} stats - stats of file
* @param {Boolean} dir - flag for directory
* @return {Array}
*/
function xml(head, after, name, abs, stats, dir) {
var h = '';
var a = '';
if (dir) {
h = '<dir id="' + CCdir++ + '">';
} else {
h = '<file id="' + CCfile++ + '">';
}
h += '<name>' + name + '</name>';
h += '<ctime>' + stats.ctime.getTime() + '</ctime>';
h += '<mtime>' + stats.mtime.getTime() + '</mtime>';
if (dir) {
a = '</dir>';
} else {
h += '<atime>' + stats.atime.getTime() + '</atime>';
h += '<size>' + stats.size + '</size>';
h += hash(false, abs);
h += '</file>';
}
return [ head + h, after + a ];
}
/**
* body
*
* @function dir_sync
* @param {String} prova - dir pathname
* @param {String} heads - header
* @param {String} afters - post header
* @return {String}
*/
function dir_sync(prova, heads, afters) {
var head = heads;
var after = afters;
var stat = fs.statSync(prova);
if (stat) {
var files = fs.readdirSync(prova);
if (files) {
for (var i = 0, ii = files.length; i < ii; ++i) {
var file = files[i];
if (my.exclude && my.exclude.test(file)) {
continue;
} else if (my.dotfiles && file[0] === '.') {
continue;
}
var root = prova + path.sep + file;
var stats = fs.statSync(root);
if (stats) {
var r;
var hea;
var afte;
if (stats.isDirectory()) {
r = build(head, after, file, root, stats, true, prova);
hea = r[0];
afte = r[1];
head = dir_sync(root, hea, afte); // recursive
after = '';
} else {
r = build(head, after, file, root, stats, false, prova);
head = r[0];
after = r[1];
}
}
}
}
}
return after ? head + after : head;
}
var CCdir = 0;
var CCfile = 0;
// cache or not
var end = simple;
if (my.cache) {
end = cached;
}
// hash
var hash = checksum;
if (!my.hash) {
hash = function(obj) {
return obj || '';
};
}
// output
var build = xml;
if (my.json) {
build = json;
return end(Object.create(null), null);
}
return end(header, footer);
}
/**
* options setting
*
* @exports dir
* @function dir
* @param {String} root - root path
* @param {Object} [opt] - various options. Check README.md
* @return {Object}
*/
function dir(root, opt) {
var resolve = path.resolve;
if (!root) {
throw new TypeError('root path required');
}
var r = resolve(root);
if (r[r.length - 1] == '/') {
r = r.substr(0, r.length - 1);
}
if (!fs.existsSync(root)) {
throw new Error('path not exists');
}
if (!fs.statSync(r).isDirectory()) {
throw new Error('path is not a directory');
}
var options = opt || Object.create(null);
var my = {
root: r,
exclude: options.exclude || false,
dotfiles: options.dotfiles === false ? false : true,
cache: options.cache === false ? false : true,
json: Boolean(options.json),
hash: options.hash === false ? false : String(options.hash || 'md5')
};
return wrapper(my);
}
module.exports = dir;