forked from krunkosaurus/NodeInterval
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·171 lines (143 loc) · 5.35 KB
/
Copy pathindex.js
File metadata and controls
executable file
·171 lines (143 loc) · 5.35 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
165
166
167
168
169
170
171
var fs = require('fs'),
_ = require('underscore'),
watch = require('nodewatch'),
log = require('simple-logger');
(function(NodeInterval){
NodeInterval.Watcher = function(options){
var that = this;
this.options = _.extend(this.defaults, options);
_(this).extend(this.options);
this.watchFiles = [];
// TODO: We should store the current set of files in an array for future
// comparing.
_.each(this.getFilesFrom(this.watchFolder), function(file){
that.watchFiles.push(file);
});
// Also watch the input file for changes.
// TODO: If inputFile changes, unwatch removed file, or watch new file.
if (options.inputFile instanceof Array){
_.each(options.inputFile, function(file){
that.watchFiles.push(file);
});
}else{
this.watchFiles.push(options.inputFile);
}
// Render files on start.
log.log_level = 'info';
this.updateIndex();
return this;
}
_.extend(NodeInterval.Watcher.prototype, {
defaults: {
watchFolder: '../src/templates/',
inputFile: '../src/html/index.html',
replacementString: '@templates@',
outputFile: '../assets/index.html'
},
startWatch: function(){
var that = this;
_.each(this.watchFiles, function(file){
watch.add(file);
});
log.info('NodeInterval is watching for changes. Press Ctrl-C to stop.');
// Start the watch change listener.
watch.onChange(function(file, prevTime, currTime){
log.info('>>> Change detected to:', file);
that.updateIndex();
});
return this;
},
stopWatch: function(){
watch.clearListeners();
_.each(this.watchFiles, function(file){
watch.remove(file);
});
return this;
},
// Recursively return an array of all files (paths) in a directory.
getFilesFrom: function(dir){
var filesAr = [];
var recurse;
// Normalize path.
if (dir[dir.length-1] !== '/'){
dir = dir + '/';
}
recurse = function(dir){
var arr = fs.readdirSync(dir);
if (arr.length){
_.each(arr, function(fileOrDir){
if (fileOrDir[0] !== '.'){
if (fs.lstatSync(dir + fileOrDir).isDirectory()){
recurse(dir + fileOrDir + '/');
}else{
filesAr.push(dir + fileOrDir);
}
}
});
}
}
recurse(dir);
return filesAr;
},
// Get the text contents of a file.
getFileContents: function(file){
return fs.readFileSync(file, 'utf8');
},
// Concat an array of files together into a single string.
stringFromFiles: function(filesAr){
var that = this;
var str = '';
_.each(filesAr, function(file){
str += that.getFileContents(file);
});
return str;
},
// Write a string to a file.
writeToFile: function(file, str){
log.info('overwrite', file);
fs.writeFileSync(file, str, 'utf8');
},
// Update target page with new templates.
updateIndex: function(){
var date = new Date();
// Ar of sorted ids from each template.
var idsAr = [];
// Lookup hash of ids to template content.
var fileHash = {};
// Final concated text content.
var content = '';
var template;
var f;
// For each file get its template id so we can sort alphabetically.
_.each(this.getFilesFrom(this.watchFolder), function(file, i, list){
var contents = this.getFileContents(file);
var id = contents.match(/id=['"](.*)['"]/);
if (id){
id = id[1];
// Add to filehash
fileHash[id] = contents;
idsAr.push(id);
}else{
throw new Error('Template missing id: ' + file);
}
}, this);
idsAr.sort();
_.each(idsAr, function(id){
content += fileHash[id];
});
if (this.outputFile instanceof Array){
for (f = 0; f < this.outputFile.length; f++){
template = this.getFileContents(this.inputFile[f]);
template = template.replace(this.replacementString, content);
this.writeToFile(this.outputFile[f], template);
}
}else{
template = this.getFileContents(this.inputFile);
template = template.replace(this.replacementString, content);
this.writeToFile(this.outputFile, template);
}
log.info('Completed in ' + ((new Date() - date) / 1000) + ' seconds.');
}
});
module.exports = NodeInterval;
})({});