|
1 | 1 | var browserify = require("browserify");
|
2 | 2 | var UglifyJS = require("uglify-js");
|
3 | 3 | var fs = require("fs");
|
4 | | - |
5 | | -var b = browserify(); |
6 | | -b.require("./AuthProvider", {expose:"AuthProvider"}); |
7 | | -//b.require("markdown"); |
8 | | -b.require(__dirname+"/node_modules/highlight.js/lib/index.js", {expose:"highlight"}); |
9 | | -b.require("querystring"); |
10 | | -//b.require("url"); |
11 | | -b.require("async"); |
12 | | -b.bundle(function(e,buff){ |
13 | | - if(e) throw e; |
14 | | - var min = UglifyJS.parse(buff.toString("utf-8")); |
15 | | - min.figure_out_scope(); |
16 | | - fs.writeFile( |
17 | | - __dirname+"/dist/api.min.js", |
18 | | - min.print_to_string(), |
19 | | - function(e){ |
20 | | - if(e) throw e; |
21 | | - console.log("wrote the js"); |
22 | | - } |
23 | | - ); |
24 | | -}); |
25 | | - |
| 4 | +var async = require("async"); |
26 | 5 | var ejs = require('ejs');
|
27 | 6 |
|
| 7 | +module.exports.compileHTML = function compileHTML(inputdir, templatepath, outputdir, next){ |
| 8 | + fs.readdir(pagepath,function(e,files){ |
| 9 | + if(e) return next(e); |
| 10 | + async.each(files,function(file,next){ |
| 11 | + var input = inputdir+"/"+file; |
| 12 | + fs.stat(input,function(e,s){ |
| 13 | + if(e) return next(e); |
| 14 | + if(s.isDirectory()){ |
| 15 | + return next("things to be compiled are not expecting a directory"); |
| 16 | + } |
| 17 | + var filename = file.split(".")[0]; |
| 18 | + var output = outputdir+"/"+filename+".html"; |
| 19 | + ejs.renderFile( |
| 20 | + input, |
| 21 | + { |
| 22 | + cur_page:{}, |
| 23 | + templatepath:templatepath |
| 24 | + }, |
| 25 | + function(e,file){ |
| 26 | + if(e) return next(e); |
| 27 | + fs.writeFile( |
| 28 | + output, |
| 29 | + file, |
| 30 | + function(e){ |
| 31 | + if(e) return next(e); |
| 32 | + next(void(0), { |
| 33 | + input:input, |
| 34 | + output:output |
| 35 | + }); |
| 36 | + } |
| 37 | + ); |
| 38 | + } |
| 39 | + ); |
| 40 | + }) |
| 41 | + },next); |
| 42 | + }); |
| 43 | +}; |
28 | 44 |
|
29 | | - |
30 | | -ejs.renderFile( |
31 | | - __dirname+"/html/blog.ejs", |
32 | | - { |
33 | | - cur_page:{}, |
34 | | - htmlpath:__dirname+"/html" |
35 | | - }, |
36 | | - function(e,file){ |
37 | | - if(e) throw e; |
38 | | - fs.writeFile( |
39 | | - __dirname+"/dist/index.html", |
40 | | - file, |
41 | | - function(e){ |
42 | | - if(e) throw e; |
43 | | - console.log("wrote index.html"); |
44 | | - } |
45 | | - ); |
| 45 | +module.exports.compileJS = function compileJS(inputrequire, outputdir,next){ |
| 46 | + var b = browserify(); |
| 47 | + var l = inputrequire.length; |
| 48 | + while(l--){ |
| 49 | + var item = inputrequire[l]; |
| 50 | + if(typeof item === "string"){ |
| 51 | + return b.require(item); |
| 52 | + } |
| 53 | + if(!item.name){ |
| 54 | + return next("when specifying something to require, "+ |
| 55 | + "\n\t you must provide a valid string or"+ |
| 56 | + "\n\t you must provide an object with {path:\"valid path or name\"}"); |
| 57 | + } |
| 58 | + b.require(item.path, item.options); |
46 | 59 | }
|
47 | | -); |
48 | | -ejs.renderFile( |
49 | | - __dirname+"/html/article.ejs", |
50 | | - { |
51 | | - cur_page:{}, |
52 | | - htmlpath:__dirname+"/html" |
53 | | - }, |
54 | | - function(e,file){ |
55 | | - if(e) throw e; |
56 | | - fs.writeFile( |
57 | | - __dirname+"/dist/article.html", |
58 | | - file, |
59 | | - function(e){ |
60 | | - if(e) throw e; |
61 | | - console.log("wrote article.html"); |
| 60 | + b.require("./AuthProvider", {expose:"AuthProvider"}); |
| 61 | + //b.require("markdown"); |
| 62 | + b.require(__dirname+"/node_modules/highlight.js/lib/index.js", {expose:"highlight"}); |
| 63 | + b.require("querystring"); |
| 64 | + b.require("async"); |
| 65 | + b.bundle(function(e,buff){ |
| 66 | + if(e) return next(e); |
| 67 | + async.parrallel([ |
| 68 | + function(){ |
| 69 | + fs.writeFile( |
| 70 | + outputdir+"/api.js", |
| 71 | + buff, |
| 72 | + function(e){ |
| 73 | + if(e) return next(e); |
| 74 | + next(void(0), {input:inputrequire,output:outputdir+"/api.js"}); |
| 75 | + } |
| 76 | + ); |
| 77 | + },function(next){ |
| 78 | + var min = UglifyJS.parse(buff.toString("utf-8")); |
| 79 | + min.figure_out_scope(); |
| 80 | + fs.writeFile( |
| 81 | + outputdir+"/api.min.js", |
| 82 | + min.print_to_string(), |
| 83 | + function(e){ |
| 84 | + if(e) return next(e); |
| 85 | + next(void(0), {input:inputrequire,output:outputdir+"/api.min.js"}); |
| 86 | + } |
| 87 | + ); |
62 | 88 | }
|
63 | | - ); |
64 | | - } |
65 | | -); |
| 89 | + ],next); |
| 90 | + }); |
| 91 | +}; |
| 92 | + |
| 93 | +module.exports.compile = function(inputs, templatepath, outputdir, next){ |
| 94 | + async.parrallel([ |
| 95 | + compileHTML.bind(void(0),inputs.dir, templatepath, outputdir), |
| 96 | + compileJS.bind(void(0),inputs.require, outputdir) |
| 97 | + ],function(e,results){ |
| 98 | + if(e) return next(e); |
| 99 | + var net = []; |
| 100 | + var l = results.length; |
| 101 | + while(l--){ |
| 102 | + net = net.concat(results[l]); |
| 103 | + } |
| 104 | + next(void(0),net); |
| 105 | + }); |
| 106 | +}; |
| 107 | + |
| 108 | +if(!module.parent){ |
| 109 | + var inputs = { |
| 110 | + require:[ |
| 111 | + {path:"./AuthProvider", options:{expose:"AuthProvider"}}, |
| 112 | + {path:__dirname+"/node_modules/highlight.js/lib/index.js", options:{expose:"highlight"}}, |
| 113 | + "querystring", |
| 114 | + "async", |
| 115 | +// "markdown" |
| 116 | + ], |
| 117 | + dir:__dirname+"/html" |
| 118 | + }; |
| 119 | +} |
0 commit comments