-
Notifications
You must be signed in to change notification settings - Fork 5
/
readify.js
197 lines (158 loc) · 5.24 KB
/
readify.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
/* global Filer */
(function(global) {
'use strict';
var fs,
squad,
format,
exec,
nicki,
WIN,
platform,
funcEmpty = function() {},
map = function(fn, array) {
return array.map(fn);
},
sort = function(fn, array) {
return array.sort(fn);
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = readify;
format = require('format-io');
exec = require('execon');
squad = require('squad');
if (typeof window !== 'undefined') {
fs = new Filer.FileSystem();
} else {
platform = process.platform;
WIN = platform === 'win32';
fs = require('fs');
nicki = !WIN && require('nicki');
}
} else {
global.readify = readify;
fs = new Filer.FileSystem();
format = window.Format;
squad = window.squad;
exec = window.exec;
}
var parseStats = exec.with(map, parseStat);
/* sorting on Win and node v0.8.0 */
var sortFiles = exec.with(sort, function(a, b) {
return a.name > b.name ? 1 : -1;
});
function readify(path, callback) {
check(path, callback);
fs.readdir(path, function(error, names) {
if (error)
callback(error);
else
getAllStats(path, names, callback);
});
}
function check(path, callback) {
var pathMsg = 'path should be string!',
callbackMsg = 'callback should be function!';
if (typeof path !== 'string')
throw Error(pathMsg);
if (typeof callback !== 'function')
throw Error(callbackMsg);
}
/**
* @param path
* @param names
*/
function getAllStats(path, names, callback) {
var funcs,
length = names.length,
dir = format.addSlashToEnd(path);
if (!length)
return fillJSON(dir, [], callback);
funcs = names.map(function(name) {
return exec.with(getStat, name, dir + name);
});
exec.parallel(funcs, function() {
var files = [].slice.call(arguments, 1);
fillJSON(dir, files, callback);
});
}
function emptyStat() {
return {
mode : 0,
size : 0,
isDirectory : funcEmpty
};
}
function getStat(name, path, callback) {
fs.stat(path, function(error, data) {
if (!data)
data = emptyStat();
data.name = name;
callback(null, data);
});
}
function parseStat(stat) {
var file, isDir, size, mode, modeStr,
owner = stat.uid || '';
/* Переводим права доступа в 8-ричную систему */
modeStr = Number(stat.mode).toString(8);
mode = Number(modeStr) || '';
isDir = stat.isDirectory();
size = isDir ? 'dir' : stat.size;
file = {
'name' : stat.name,
'size' : format.size(size),
'owner' : owner,
'mode' : mode && format.permissions.symbolic(mode)
};
return file;
}
/**
* Function fill JSON by file stats
*
* @param params - { files, stats, path }
*/
function fillJSON(path, stats, callback) {
var processFiles = squad(changeOrder, sortFiles, parseStats),
json = {
path : '',
files : processFiles(stats)
};
json.path = format.addSlashToEnd(path);
changeUIDToName(json, function(error, files) {
json.files = files;
callback(null, json);
});
}
function changeUIDToName(json, callback) {
if (!nicki)
callback(null, json.files);
else
nicki(function(error, names) {
var files = json.files.slice();
if (!error)
files = files.map(function(file) {
var owner = file.owner;
owner = names[owner];
if (owner)
file.owner = owner;
return file;
});
callback(error, files);
});
}
function isDir(file) {
return file.size === 'dir';
}
function not(fn) {
return function() {
return !fn.apply(null, arguments);
};
}
function changeOrder(json) {
var isFile = not(isDir),
dirs = json.filter(isDir),
files = json.filter(isFile),
sorted = dirs.concat(files);
return sorted;
}
})(this);