forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadFs.js
More file actions
executable file
·164 lines (125 loc) · 3.69 KB
/
readFs.js
File metadata and controls
executable file
·164 lines (125 loc) · 3.69 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
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
var fs = require('fs');
var path = require('path');
var mime = require('mime');
var log = require('log')();
var stripIndents = require('textUtil/stripIndents');
function readFs(dir) {
var files = fs.readdirSync(dir);
var hadErrors = false;
files = files.filter(function(file) {
if (file[0] == ".") return false;
var filePath = path.join(dir, file);
if (fs.statSync(filePath).isDirectory()) {
log.error("Directory not allowed: " + file);
hadErrors = true;
}
var type = mime.lookup(file).split('/');
if (type[0] != 'text' && type[1] != 'json' && type[1] != 'javascript') {
log.error("Bad file extension: " + file);
hadErrors = true;
}
return true;
});
if (hadErrors) {
return null;
}
files = files.sort(function(fileA, fileB) {
var extA = fileA.slice(fileA.lastIndexOf('.') + 1);
var extB = fileB.slice(fileB.lastIndexOf('.') + 1);
if (extA == extB) {
return fileA > fileB ? 1 : -1;
}
// html always first
if (extA == 'html') return 1;
if (extB == 'html') return -1;
// then goes CSS
if (extA == 'css') return 1;
if (extB == 'css') return -1;
// then JS
if (extA == 'js') return 1;
if (extB == 'js') return -1;
// then other extensions
return fileA > fileB ? 1 : -1;
});
var filesForPlunk = {};
for (var i = 0; i < files.length; i++) {
var file = files[i];
filesForPlunk[file] = {
filename: file,
content: stripIndents(fs.readFileSync(path.join(dir, file), 'utf-8'))
};
}
return filesForPlunk;
}
module.exports = readFs;
/*
function* readFs(dir) {
var files = fs.readdirSync(dir);
var errors = [];
files = files.filter(function(file) {
if (file[0] == ".") return false;
var filePath = path.join(dir, file);
if (fs.statSync(filePath).isDirectory()) {
errors.push("Directory not allowed: " + file);
return false;
}
var type = mime.lookup(file).split('/');
if (type[0] != 'text' && type[1] != 'json' && type[1] != 'javascript') {
errors.push("Bad file extension: " + file);
}
return true;
});
var meta = {};
var plunkFilePath = path.join(dir, '.plnkr');
if (fs.existsSync(plunkFilePath)) {
var existingPlunk = fs.readFileSync(plunkFilePath, 'utf-8');
existingPlunk = JSON.parse(existingPlunk);
// dir name change (2 levels up) = new plunk
var plunkDirName = fs.realpathSync(dir);
plunkDirName = path.basename(path.dirname(plunkDirName)) + path.sep + path.basename(plunkDirName);
if (existingPlunk.name == plunkDirName) {
meta = existingPlunk;
}
}
if (errors.length) {
log.error(errors);
return false;
}
files = files.sort(function(fileA, fileB) {
var extA = fileA.slice(fileA.lastIndexOf('.') + 1);
var extB = fileB.slice(fileB.lastIndexOf('.') + 1);
if (extA == extB) {
return fileA > fileB ? 1 : -1;
}
// html always first
if (extA == 'html') return 1;
if (extB == 'html') return -1;
// then goes CSS
if (extA == 'css') return 1;
if (extB == 'css') return -1;
// then JS
if (extA == 'js') return 1;
if (extB == 'js') return -1;
// then other extensions
return fileA > fileB ? 1 : -1;
});
var filesForPlunk = {};
for (var i = 0; i < files.length; i++) {
var file = files[i];
filesForPlunk[file] = {
filename: file,
content: fs.readFileSync(path.join(dir, file), 'utf-8')
};
}
return {
meta: meta,
files: filesForPlunk
};
}
*/
/*
require('co')(readPlunkContent('/private/var/site/js-dev/tutorial/03-more/11-css-for-js/17-css-sprite/height48'))(function(err, res) {
if (err) console.error(err.message, err.stack);
else console.log(res);
});
*/