-
Notifications
You must be signed in to change notification settings - Fork 45
/
jade
executable file
·125 lines (105 loc) · 3.01 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
#!/usr/bin/env node
/**
* Module dependencies.
*/
var fs = require('fs')
, program = require('commander')
, path = require('path')
, basename = path.basename
, dirname = path.dirname
, resolve = path.resolve
, join = path.join
, mkdirp = require('mkdirp')
, jade = require('../');
// jade options
var options = {};
// options
program
.version(jade.version)
.usage('[options] [dir|file ...]')
.option('-o, --obj <str>', 'javascript options object')
.option('-O, --out <dir>', 'output the compiled html to <dir>')
.option('-p, --path <path>', 'filename used to resolve includes')
program.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' # translate jade the templates dir');
console.log(' $ jade templates');
console.log('');
console.log(' # create {foo,bar}.html');
console.log(' $ jade {foo,bar}.jade');
console.log('');
console.log(' # jade over stdio');
console.log(' $ jade < my.jade > my.html');
console.log('');
console.log(' # jade over stdio');
console.log(' $ echo "h1 Jade!" | jade');
console.log('');
console.log(' # foo, bar dirs rendering to /tmp');
console.log(' $ jade foo bar --out /tmp ');
console.log('');
});
program.parse(process.argv);
// options given, parse them
if (program.obj) options = eval('(' + program.obj + ')');
// filename
if (program.path) options.filename = program.path;
// left-over args are file paths
var files = program.args;
// compile files
if (files.length) {
console.log();
files.forEach(renderFile);
process.on('exit', console.log);
// stdio
} else {
stdin();
}
/**
* Compile from stdin.
*/
function stdin() {
var buf = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk){ buf += chunk; });
process.stdin.on('end', function(){
var fn = jade.compile(buf, options);
process.stdout.write(fn(options));
}).resume();
}
/**
* Process the given path, compiling the jade files found.
* Always walk the subdirectories.
*/
function renderFile(path) {
var re = /\.jade$/;
fs.lstat(path, function(err, stat) {
if (err) throw err;
// Found jade file
if (stat.isFile() && re.test(path)) {
fs.readFile(path, 'utf8', function(err, str){
if (err) throw err;
options.filename = path;
var fn = jade.compile(str, options);
path = path.replace(re, '.html');
if (program.out) path = join(program.out, basename(path));
var dir = resolve(dirname(path));
mkdirp(dir, 0755, function(err){
if (err) throw err;
fs.writeFile(path, fn(options), function(err){
if (err) throw err;
console.log(' \033[90mrendered \033[36m%s\033[0m', 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(renderFile);
});
}
});
}