forked from mozilla/nunjucks
/
node-loaders.js
70 lines (57 loc) · 1.99 KB
/
node-loaders.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
var fs = require('fs');
var path = require('path');
var lib = require('./lib');
var Loader = require('./loader');
var chokidar = require('chokidar');
// Node <0.7.1 compatibility
var existsSync = fs.existsSync || path.existsSync;
var FileSystemLoader = Loader.extend({
init: function(searchPaths, noWatch) {
this.pathsToNames = {};
if(searchPaths) {
searchPaths = lib.isArray(searchPaths) ? searchPaths : [searchPaths];
// For windows, convert to forward slashes
this.searchPaths = searchPaths.map(path.normalize);
}
else {
this.searchPaths = ['.'];
}
if(!noWatch) {
// Watch all the templates in the paths and fire an event when
// they change
lib.each(this.searchPaths, function(p) {
if(existsSync(p)) {
var watcher = chokidar.watch(p, { ignoreInitial: true });
watcher.on("all", function(event, fullname) {
if(event == "change" && fullname in this.pathsToNames) {
this.emit('update', this.pathsToNames[fullname]);
}
}.bind(this));
}
}.bind(this));
}
},
getSource: function(name) {
var fullpath = null;
var paths = this.searchPaths;
for(var i=0; i<paths.length; i++) {
var p = path.join(paths[i], name);
// Only allow the current directory and anything
// underneath it to be searched
if((paths[i] == '.' || p.indexOf(paths[i]) === 0) &&
existsSync(p)) {
fullpath = p;
break;
}
}
if(!fullpath) {
return null;
}
this.pathsToNames[fullpath] = name;
return { src: fs.readFileSync(fullpath, 'utf-8'),
path: fullpath };
}
});
module.exports = {
FileSystemLoader: FileSystemLoader
};