/
jade
executable file
·206 lines (177 loc) · 3.96 KB
/
jade
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
198
199
200
201
202
203
204
205
206
#!/usr/bin/env node
/**
* Module dependencies.
*/
var fs = require('fs')
, path = require('path')
, resolve = path.resolve
, basename = path.basename
, dirname = path.dirname
, jade;
try {
jade = require('../lib/jade');
} catch (err) {
jade = require('jade');
}
/**
* Arguments.
*/
var args = process.argv.slice(2);
/**
* Options javascript.
*/
var options = {};
/**
* Destination dir.
*/
var dest;
/**
* Watcher hash.
*/
var watchers;
/**
* Usage information.
*/
var usage = ''
+ '\n'
+ ' Usage: jade [options]\n'
+ ' [path ...]\n'
+ ' < in.jade > out.jade'
+ ' \n'
+ ' Options:\n'
+ ' -o, --options <str> JavaScript options object passed\n'
+ ' -h, --help Output help information\n'
+ ' -w, --watch Watch file(s) or folder(s) for changes and re-compile\n'
+ ' -v, --version Output jade version\n'
+ ' --out <dir> Output the compiled html to <dir>\n';
+ '\n';
// Parse arguments
var arg
, files = [];
while (args.length) {
arg = args.shift();
switch (arg) {
case '-h':
case '--help':
console.log(usage);
process.exit(1);
case '-v':
case '--version':
console.log(jade.version);
process.exit(1);
case '-o':
case '--options':
var str = args.shift();
if (str) {
options = eval('(' + str + ')');
} else {
console.error('-o, --options requires a string.');
process.exit(1);
}
break;
case '-w':
case '--watch':
watchers = {};
break;
case '--out':
dest = args.shift();
break;
default:
files.push(arg);
}
}
// Watching and no files passed - watch cwd
if (watchers && !files.length) {
fs.readdirSync(process.cwd()).forEach(processFile);
// Process passed files
} else if (files.length) {
files.forEach(processFile);
// Stdio
} else {
var buf = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk){
buf += chunk;
}).on('end', function(){
console.log(jade.render(buf, options));
}).resume();
}
/**
* Process the given path, compiling the jade files found.
* Always walk the subdirectories.;
*/
function processFile(path) {
fs.lstat(path, function(err, stat) {
if (err) throw err;
// Found jade file
if (stat.isFile() && path.match(/\.jade$/)) {
renderJade(path);
// Found directory
} else if (stat.isDirectory()) {
fs.readdir(path, function(err, files) {
if (err) throw err;
files.map(function(filename) {
return path + '/' + filename;
}).forEach(processFile);
});
}
});
}
/**
* Render jade
*/
function renderJade(jadefile) {
jade.renderFile(jadefile, options, function(err, html) {
if (err) throw err;
writeFile(jadefile, html);
});
}
/**
* mkdir -p implementation.
*/
function mkdirs(path, fn) {
var segs = dirname(path).split('/')
, dir = '';
(function next() {
var seg = segs.shift();
if (seg) {
dir += seg + '/';
fs.mkdir(dir, 0755, function(err){
if (!err) return next();
if ('EEXIST' == err.code) return next();
});
} else {
fn();
}
})();
}
/**
* Write the html output to a file.
*/
function writeFile(src, html) {
var path = src.replace('.jade', '.html');
if (dest) path = dest + '/' + path;
mkdirs(path, function(err){
if (err) throw err;
fs.writeFile(path, html, function(err) {
if (err) throw err;
console.log(' \033[90mcompiled\033[0m %s', path);
watch(src, renderJade);
});
});
}
/**
* Watch the given `file` and invoke `fn` when modified.
*/
function watch(file, fn) {
// not watching
if (!watchers) return;
// already watched
if (watchers[file]) return;
// watch the file itself
watchers[file] = true;
console.log(' \033[90mwatching\033[0m %s', file);
fs.watchFile(file, { interval: 50 }, function(curr, prev){
if (curr.mtime > prev.mtime) fn(file);
});
}