forked from NodeOS/GitBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
170 lines (141 loc) · 3.38 KB
/
build.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
var fs = require("fs");
var path = require("path");
var async = require("async");
var browserify = require("browserify");
var ejs = require('ejs');
var UglifyJS = require("uglify-js");
var ob = module.exports = {};
// [email protected]:NodeOS/GitBlog.git
ob.compileHTML = function compileHTML(inputdir, templatepath, outputdir, next)
{
fs.readdir(inputdir,function(e,files)
{
if(e) return next(e);
async.each(files,function(file,next)
{
var input = inputdir+"/"+file;
fs.stat(input,function(e,s)
{
if(e) return next(e);
if(s.isDirectory())
return next("things to be compiled are not expecting a directory");
var filename = file.split(".")[0];
var output = outputdir+"/"+filename+".html";
ejs.renderFile(input,
{
cur_page:{},
templatepath:templatepath,
htmlpath:path.resolve(templatepath, "../")
},
function(e,file)
{
if(e) return next(e);
fs.writeFile(output, file, function(e)
{
if(e) return next(e);
next(void(0),
{
input:input,
output:output
});
});
});
});
},
next);
});
};
ob.compileJS = function compileJS(inputrequire, outputdir,next)
{
var b = browserify();
var l = inputrequire.length;
while(l--)
{
var item = inputrequire[l];
if(typeof item === "string")
{
b.require(item);
continue;
}
if(!item.path)
return next("when specifying something to require, "+
"\n\t you must provide a valid string or"+
"\n\t you must provide an object with {path:\"valid path or name\"}");
b.require(item.path, item.options);
inputrequire[l] = item.path;
}
b.bundle(function(e,buff)
{
if(e) return next(e);
async.parallel(
[
function(next)
{
fs.writeFile(outputdir+"/api.js", buff,
function(e)
{
if(e) return next(e);
next(void(0), {input:inputrequire,output:outputdir+"/api.js"});
});
},
function(next)
{
var min = UglifyJS.parse(buff.toString("utf-8"));
min.figure_out_scope();
fs.writeFile(outputdir+"/api.min.js", min.print_to_string(),
function(e)
{
if(e) return next(e);
next(void(0), {input:inputrequire,output:outputdir+"/api.min.js"});
});
}
],
next);
});
};
ob.compileAll = function compileAll(inputs, templatepath, outputdir, next)
{
console.log("compiling all");
async.parallel(
[
ob.compileHTML.bind(void(0),inputs.dir, templatepath, outputdir),
ob.compileJS.bind(void(0),inputs.require, outputdir)
],
function(e,results)
{
if(e) return next(e);
var net = [];
console.log(results);
var l = results.length;
while(l--)
net = net.concat(results[l]);
next(void(0),net);
});
};
if(!module.parent)
{
var inputs =
{
require:
[
"auth-provider",
{
path: __dirname+"/node_modules/highlight.js/lib/index.js",
options:
{
expose: "highlight"
}
},
"querystring",
"async",
// "markdown"
],
dir: __dirname+"/input"
};
ob.compileAll(inputs, __dirname+"/template.ejs", __dirname,
function(e,results)
{
if(e) throw e;
console.log(results);
});
}