-
Notifications
You must be signed in to change notification settings - Fork 2
/
puglatizer.js
175 lines (157 loc) · 7.18 KB
/
puglatizer.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
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
'use strict'
var pug = require('pug'),
wrap = require('pug-runtime/wrap'),
async = require('async'),
escape = require('escape-regexp'),
_ = require('lodash'),
path = require('path'),
fs = require('fs'),
minify = require('harp-minify'),
filePaths = []
var buildTemplateFromFile = function(filepath,options,respond) {
fs.stat(filepath,function(err,stat) {
if (err) { return respond(err) }
fs.readFile(filepath,function(err,fileData) {
if (err) { return respond(err) }
try {
options.filename = filepath
var compiledPug = pug.compile(fileData, options)
var template = wrap(compiledPug)
return respond(null,template)
} catch(err) {
console.log('Error at',filepath)
console.log(err)
}
})
})
}
module.exports = function(templateDirectories,outputFile,opts,done) {
if (typeof templateDirectories != 'string') { throw new Error('Template directory path must be string.') }
if (typeof outputFile != 'string') { throw new Error('Output path must be string.') }
if (!done) { done = function(){} }
var options = opts && typeof opts == 'object' ? opts : {},
results = {}
if(!options.hasOwnProperty('basedir')){
options.basedir = templateDirectories
}
async.waterfall([
// Unbuild old template file if exists
function(callback) {
fs.stat(outputFile,function(err,stat) {
if (err) { return callback(null) }
console.log('Found old ' + outputFile + ' so now removing')
fs.unlinkSync(outputFile)
return callback(null)
})
},
// Create the initial file
function(callback) {
fs.appendFileSync(outputFile,";(function(root,factory){\r\n")
fs.appendFileSync(outputFile," if (typeof define === 'function' && define.amd) {\r\n")
fs.appendFileSync(outputFile," define([], factory);\r\n")
fs.appendFileSync(outputFile," } else if (typeof exports === 'object') {\r\n")
fs.appendFileSync(outputFile," module.exports = factory();\r\n")
fs.appendFileSync(outputFile," } else {\r\n")
fs.appendFileSync(outputFile," if (typeof root === 'undefined' || root !== Object(root)) {\r\n")
fs.appendFileSync(outputFile," throw new Error('puglatizer: window does not exist or is not an object');\r\n")
fs.appendFileSync(outputFile," }\r\n")
fs.appendFileSync(outputFile," root.puglatizer = factory();\r\n")
fs.appendFileSync(outputFile," }\r\n")
fs.appendFileSync(outputFile,"}(this, function () {\r\n")
fs.appendFileSync(outputFile," function pug_classes_object(val) { var classString = '', padding = ''; for (var key in val) { if (key && val[key] && pug_has_own_property.call(val, key)) { var classString = classString + padding + key; var padding = ' '; } } return classString; }")
fs.appendFileSync(outputFile," function pug_classes_array(val, escaping) { var classString = '', className, padding = '', escapeEnabled = Array.isArray(escaping); for (var i = 0; i < val.length; i++) { var className = pug_classes(val[i]); if (!className) continue; escapeEnabled && escaping[i] && (className = pug_escape(className)); var classString = classString + padding + className; var padding = ' '; } return classString; }")
fs.appendFileSync(outputFile," " + minify.js(pug.runtime.merge.toString()) + '\r\n')
fs.appendFileSync(outputFile," " + minify.js(pug.runtime.classes.toString()) + '\r\n')
fs.appendFileSync(outputFile," " + minify.js(pug.runtime.style.toString()) + '\r\n')
fs.appendFileSync(outputFile," " + minify.js(pug.runtime.attr.toString()) + '\r\n')
fs.appendFileSync(outputFile," " + minify.js(pug.runtime.attrs.toString()) + '\r\n')
fs.appendFileSync(outputFile," " + minify.js(pug.runtime.escape.toString()).replace('pug_match_html','(/[\"&<>]/)') + '\r\n')
fs.appendFileSync(outputFile," " + minify.js(pug.runtime.rethrow.toString()) + '\r\n')
fs.appendFileSync(outputFile," var pug = {\r\n")
fs.appendFileSync(outputFile," merge:" + minify.js(pug.runtime.merge.toString()) + ',\r\n')
fs.appendFileSync(outputFile," classes:" + minify.js(pug.runtime.classes.toString()) + ',\r\n')
fs.appendFileSync(outputFile," style:" + minify.js(pug.runtime.style.toString()) + ',\r\n')
fs.appendFileSync(outputFile," attr:" + minify.js(pug.runtime.attr.toString()) + ',\r\n')
fs.appendFileSync(outputFile," attrs:" + minify.js(pug.runtime.attrs.toString()) + ',\r\n')
fs.appendFileSync(outputFile," escape:" + minify.js(pug.runtime.escape.toString()).replace('pug_match_html','(/[\"&<>]/)') + ',\r\n')
fs.appendFileSync(outputFile," rethrow:" + minify.js(pug.runtime.rethrow.toString()) + '\r\n')
fs.appendFileSync(outputFile," }\r\n")
fs.appendFileSync(outputFile,"\r\n")
fs.appendFileSync(outputFile,' var puglatizer = {}')
fs.appendFileSync(outputFile,"\r\n")
return callback()
},
// Here we build all the pug functions
function(callback) {
var fileLoop = function(currentDir,templateDirectories,cb) {
var callback_has_been_called = false
fs.readdir(currentDir,function(err,files) {
if (err) {
return console.log('Unable to find files in path',currentDir)
}
var num = files.length
var finishFile = function(i) {
if (!(--num)) {
if (!callback_has_been_called) {
callback_has_been_called = true
return cb()
}
} else {
buildFileData(i+1)
}
}
var buildFileData = function(i) {
var file = files[i]
var filepath = currentDir + '/' + file
fs.stat(filepath,function(err2,stats) {
if (err2) {
return console.log('Unable to find file',filepath)
}
if (stats && stats.isDirectory()) {
var pugatizerPath = ' puglatizer' + currentDir.replace(templateDirectories,'').replace(/\//g,'"]["') + '"]["' + file.replace('.pug','') + '"] = {}\r\n'
pugatizerPath = pugatizerPath.replace('puglatizer"]','puglatizer')
fs.appendFileSync(outputFile,pugatizerPath)
fileLoop(filepath,templateDirectories,function() {
finishFile(i)
})
} else if (stats && stats.isFile()) {
var ext = path.extname(filepath)
if (ext == '.pug') {
var pugatizerPath = ' puglatizer' + currentDir.replace(templateDirectories,'').replace(/\//g,'"]["') + '"]["' + file.replace('.pug','') + '"] = '
pugatizerPath = pugatizerPath.replace('puglatizer"]','puglatizer')
buildTemplateFromFile(filepath,options,function(err,template) {
pugatizerPath += minify.js(template.toString()) + ';\r\n\r\n'
fs.appendFileSync(outputFile,pugatizerPath)
finishFile(i)
})
} else {
finishFile(i)
}
} else {
finishFile(i)
}
})
}
if (!num) {
return console.log('Unable to find files in path',currentDir)
} else {
buildFileData(0)
}
})
}
fileLoop(templateDirectories,templateDirectories,function() {
return callback()
})
},
// Finalize the file
function(callback) {
fs.appendFileSync(outputFile,"\r\n")
fs.appendFileSync(outputFile," return puglatizer;\r\n")
fs.appendFileSync(outputFile,"}));\r\n")
return callback()
}
],function(err,result) {
if (err) { return done(err) }
return done(null)
})
}