-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (160 loc) · 4.13 KB
/
index.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
176
177
178
'use strict';
var async = require('async'),
fs = require('fs-extra'),
path = require('path'),
_ = require('lodash'),
Template = require('cartapacio.core.template'),
Writer = require('cartapacio.core.writer'),
Amass = require('cartapacio.core.amass')
// /*
// params:
// tplPath: rootFile for templates
// output: output dir
// */
function Cartapacio (webpageFolder, tplPath, output, toBuild) {
this.templatesRoot = tplPath
this.outputFolder = output
this.webpageFolder = webpageFolder
this.dbPath = path.join(webpageFolder, 'cartapacio_db')
this.toBuild = toBuild
}
Cartapacio.prototype.init = function(callbackInit) {
this.database = new Amass(this.dbPath)
this.templates = new Template(this.templatesRoot)
this.writer = new Writer(this.outputFolder)
this.templates.load(function (err){
if(err){
throw new Error(err)
callbackInit(err)
}
callbackInit(null)
})
};
Cartapacio.prototype.buildWebsite = function() {
var self = this
async.waterfall([
// initialize everything
function (next){
console.info('initializing');
self.init(function (err){
if(!err){
next(null)
}
})
},
// copy static files
function (next){
console.info('copying static files')
var input = path.join(self.webpageFolder, 'static')
var output = path.join(self.outputFolder, 'static')
fs.copy(input, output, function (err){
if (err){
next(err)
}
next(null)
})
},
// copy assets
function (next){
console.info('copying assets files')
var input = path.join(self.templatesRoot, 'src', 'assets')
var output = path.join(self.outputFolder, 'assets')
fs.copy(input, output, function (err){
if (err){
next(err)
}
next(null)
})
},
function (next){
async.eachSeries(self.toBuild, function (item, itemDone){
console.info('building ', item)
async.waterfall([
function (_next){
self.getData(item.detail, function (docs){
console.info('get data ', item.detail)
_next(null, docs)
})
},
function (docs, _next){
if(item.list){
var itemList = self.makeList(docs, 'default', item.list)
self.render(itemList, function (err){
_next(null, docs)
})
} else {
_next(null, docs)
}
},
function (docs, _next){
console.info('render ', item.detail);
self.render(docs, function (err){
_next(err)
})
}
],
function (err){
if(err){
console.error(err);
}
console.info('done ', item.detail);
itemDone(null)
}
)
}, function (err){
if(err){
throw new Error('building array: ', err)
}
console.info('build tasks done');
next(null)
})
}
],
function (err){
if(err){
throw new Error('build website: ', err)
}
console.log('all done');
}
)
};
Cartapacio.prototype.getData = function(doctype, callback) {
this.database.documents(doctype, function (err, docs){
if(err){
throw new Error(err)
}
callback(docs)
})
};
Cartapacio.prototype.render = function(docs, renderDone) {
var self = this
async.each(docs, function (doc, nextDoc){
self.templates.render(doc, function (err, html){
var _path = self.makePath(doc)
self.writer.write(_path, html, function (err){
nextDoc(err)
})
})
}, function (err){
if(err){
console.error(err)
}
renderDone(null)
})
};
Cartapacio.prototype.makeList = function(docs, layout, page) {
var doc = [{
layout: layout,
page: page,
document: docs
}]
return doc
};
Cartapacio.prototype.makePath = function(doc) {
if(doc.slug){
return doc.page+'/'+doc.slug+'/index.html'
} else {
return doc.page+'/index.html'
}
};
module.exports = Cartapacio