818
Du
dustymarkdown / files
dustymarkdown / lib / build.js
4.9 KB
1// modules 2var fs = require('fs'), 3 jsp = require("uglify-js").parser, 4 pro = require("uglify-js").uglify, 5 dust = require('dust'), 6 sqwish = require('sqwish').minify, 7 kompressor = require('htmlKompressor'), 8 markdown = require("node-markdown").Markdown; 9 10dust.optimizers.format = function(ctx, node) { return node }; 11 12// helper functions 13// read files from directory 14function fetch (path, ext, cb) { 15 var readFiles = []; 16 17 fs.readdir(path, function (err, files) { 18 if (err) return cb(err); 19 20 var len = files.length; 21 22 !function reader (i) { 23 var filename = files[i], 24 re = new RegExp("\\."+ ext +"$"); 25 26 if (i === len) 27 return cb(null, readFiles); 28 29 else if (re.test(filename)) { 30 fs.readFile(path + filename, 'utf8', function (err, src) { 31 if (err) return cb(err); 32 33 readFiles.push({ 34 "name": filename, 35 "src": src 36 }); 37 reader(i+1); 38 }); 39 } 40 else 41 reader(i+1); 42 } (0); 43 44 }); 45} 46 47// html, markdown dust handlers 48function write (path, files, cb) { 49 var len = files.length; 50 !function writer (i) { 51 if (i === len) 52 return cb(null); 53 else { 54 var file = files[i]; 55 fs.writeFile(path + file.name, file.src, "utf8", function (err) { 56 if (err) return cb(err); 57 58 writer(i+1); 59 }); 60 } 61 } (0); 62} 63 64function marky (cb) { 65 // fecth mardown files 66 fetch("./markdown/", "md", function (err, files) { 67 if (err) return cb(err); 68 69 var content = {}; 70 // parse markdown 71 files.forEach(function (file, index) { 72 var name = file.name.replace(/\..+$/, ""); 73 content[name] = markdown(file.src); 74 }); 75 76 cb(null, content); 77 }); 78} 79 80function dusty (content, cb) { 81 // fecth html file templates 82 fetch("./htdocs/", "html", function (err, files) { 83 // compile the templates 84 files.forEach(function (file, index) { 85 var c = dust.compile(file.src, file.name); 86 dust.loadSource(c); 87 }); 88 89 // dust 90 var len = files.length; 91 !function duster (i) { 92 if (i === len) 93 return cb(null, files); 94 95 else { 96 var file = files[i]; 97 dust.render(file.name, content, function (err, html) { 98 if (err) return cb(err); 99 100 file.src = html; 101 102 duster(i+1); 103 }); 104 } 105 } (0); 106 }); 107} 108 109function html (min, cb) { 110 111 // mardown 112 marky(function (err, content) { 113 if (err) return cb(err); 114 // dust 115 dusty(content, function (err, htmlFiles) { 116 if (err) return cb(err); 117 118 //@TODO compress html 119 if (min) htmlFiles.forEach(function (file, idx) { 120 htmlFiles[idx].src = kompressor(file.src, true); 121 }); 122 123 // write them down 124 write("./static/", htmlFiles, function (err) { 125 if (err) return cb(err); 126 console.log("* HTML files complete."); 127 cb(null); 128 }); 129 }); 130 }); 131} 132 133function css (min, cb) { 134 // get the css 135 fetch("./htdocs/css/", "css", function (err, cssFiles) { 136 if (err) return cb(err); 137 138 if (min) { 139 cssFiles.forEach(function (file, idx) { 140 cssFiles[idx].src = sqwish(file.src); 141 }); 142 } 143 144 write("./static/css/", cssFiles, function (err) { 145 if (err) return cb(err); 146 147 console.log("* CSS files complete."); 148 cb(null); 149 }); 150 }); 151} 152 153function uglify (orig_code) { 154 var ast = jsp.parse(orig_code); // parse code and get the initial AST 155 ast = pro.ast_mangle(ast); // get a new AST with mangled names 156 ast = pro.ast_squeeze(ast); // get an AST with compression optimizations 157 var final_code = pro.gen_code(ast); // compressed code here 158 159 return final_code; 160} 161 162function libs (cb) { 163 fetch("./htdocs/js/libs/", "js", function (err, libs) { 164 if (err) return cb(err); 165 166 write("./static/js/libs/", libs, function (err) { 167 if (err) return cb(err); 168 169 console.log("* JS Libs complete."); 170 cb(null); 171 }); 172 }); 173} 174 175function js (min, cb) { 176 fetch("./htdocs/js/", "js", function (err, jsFiles) { 177 if (err) return cb(err); 178 179 if (min) jsFiles.forEach(function (file, idx) { 180 jsFiles[idx].src = uglify(file.src); 181 }); 182 183 write("./static/js/", jsFiles, function (err) { 184 if (err) return cb(err); 185 186 console.log("* JS Files complete (min: "+min+")."); 187 cb(null); 188 }); 189 }); 190} 191 192module.exports = function (prod) { 193 html(prod, function (err) { 194 if (err) throw err; 195 196 css(prod, function (err) { 197 if (err) throw err; 198 199 libs(function (err) { 200 if (err) throw err; 201 202 js(prod, function (err) { 203 if (err) throw err; 204 }); 205 }); 206 }); 207 }); 208} 209 210