-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.js
64 lines (47 loc) · 1.46 KB
/
lib.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
var async = require("async");
var fs = require("fs");
var path = require("path");
var scoutDir = function (dir, ext, templates, _cb) {
fs.readdir(dir, function (err, files) {
if (err) return _cb(err);
async.forEach(files, addFile(dir, ext, templates), _cb);
});
};
var addFile = function (dir, ext, templates) {
return function (file, _cb) {
var filePath = path.join(dir, file);
var fileName = path.basename(file, ext);
fs.stat(filePath, function (err, stats) {
if (err) return _cb(err);
if (stats.isDirectory()) {
var newDir = path.join(dir, file);
templates[file] = {};
scoutDir(newDir, ext, templates[file], _cb);
} else if (path.extname(file) === ext) {
fs.readFile(filePath, "utf8", function (err, data) {
if (err) return _cb(err);
templates[fileName] = data;
_cb();
});
} else {
_cb();
}
});
};
};
var middleware = function (opts) {
if (!opts) opts = {};
if (!opts.src) throw new Error("Must provide source directory.");
if (!opts.dest) throw new Error("Must provide destination path.");
if (!opts.ext) opts.ext = ".ejs";
if (!opts.varName) opts.varName = "Templates";
return function (req, res, next) {
var templates = {};
scoutDir(opts.src, opts.ext, templates, function (err) {
if (err) return next(err);
var scriptFile = "var " + opts.varName + " = " + JSON.stringify(templates) + ";";
fs.writeFile(opts.dest, scriptFile, next);
});
};
};
module.exports = middleware;