-
Notifications
You must be signed in to change notification settings - Fork 12
/
gulpfile.js
278 lines (241 loc) · 7.68 KB
/
gulpfile.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
'use strict';
/* global require, console, Buffer */
var streamy = require('streamy-data'),
pesticide = require('./bin/pesticide'),
util = require('./bin/util'),
clone = util.clone,
copyIfAbsent = util.copyIfAbsent,
moa = require('./bin/zh-en-moa'),
unorm = require('unorm'),
gulp = require('gulp'),
File = require('vinyl'),
async = require('async'),
fs = require('fs');
Error.stackTraceLimit = Infinity;
gulp.task('data.download', [
'data.download.pesticide'
]);
/* This task shall write the following files:
* + /_raw/download/licenses.json
* + /_raw/download/index.json
* + /_raw/download/entries/{id}.json
*/
/* jshint asi: true */
gulp.task('data.download.pesticide', function (callback) {
// TODO: clear
// grab index json data from the website
pesticide.download.licenses(function (licenses) {
// TODO: use mkdirp or writefile
console.log('license file downloaded.');
var index = pesticide.build.indexFromLicenses(licenses),
ids = index.map(function (data) { return data.id });
async.parallel([
function (cb) {
// write licenses file
fs.writeFile('./_raw/download/pesticide/licenses.json',
JSON.stringify(licenses, null, '\t'), cb); // end 1
},
function (cb) {
/* Write index file. We could have deferred this part to
* building phase, but this will make everything easier.
*/
fs.writeFile('./_raw/download/pesticide/index.json',
JSON.stringify(index, null, '\t'), cb); // end 2
},
function (cb) {
// transform the index list into an object stream
streamy.array(ids)
// transform them into detailed data object from the website
.pipe(streamy.map(pesticide.download.entry))
// transform them into vinyl file format (to work with gulp.dest)
.pipe(streamy.file.vinylify(function (data) {
return './' + data.id + '.json';
}, {
stringify: { space: '\t' }
}))
// write file to specified destination
.pipe(gulp.dest('./_raw/download/pesticide/entries'))
.on('data', function () {})
.on('end', cb); // end 3
}
],
function () {
callback();
});
});
});
/* In hopes of manually calling download tasks, we keep build tasks away from
* dependencies to them.
*/
gulp.task('data.build', [
'data.build.pesticide',
'data.build.formulations'
]);
/* Precondition: all downloaded raw files are available, which includes:
* + /_raw/download/pesticide/licenses.json
* + /_raw/download/pesticide/index.json
* + /_raw/download/pesticide/entries/{id}.json
*
* This task shall write the following files:
* + /_data/pesticide/list.json
* + /_data/pesticide/entries/{id}.json
* + /pesticide/entries/{id}.html
* + /data/pesticide/usages-search.json
*/
function readAsString (filename, callback) {
return fs.readFile(filename, 'utf8', callback);
}
gulp.task('data.build.pesticide', function (callback) {
// callback is invoked when end is called three times
var end = streamy.util.wait(4, callback);
async.map(
[
'./_raw/download/pesticide/index.json',
'./_raw/download/pesticide/licenses.json',
'./_raw/tactri_moa_2014.txt'
],
readAsString,
function (err, data) {
var index = JSON.parse(data[0]),
licenses = JSON.parse(data[1]),
moaObj = moa(data[2]),
m = {},
usageSearchData = {};
// initialize entry objects
index.forEach(function (entry) {
entry.licenses = [];
m[entry.id] = entry;
});
async.series([
function (cb) {
fs.writeFile('./_data/pesticide/list.json', data[0], cb);
},
function (cb) {
streamy.array(index.slice(0)) // TODO: stream array bug
.pipe(streamy.map.sync(function (data) {
return new File({
path: './' + data.id + '.html',
contents: new Buffer('---\nlayout: pesticide-entry\nid: ' + data.id + '\n---\n')
});
}))
.pipe(gulp.dest('./pesticide/entries'))
.on('data', function () {})
.on('end', cb);
},
function (cb) {
var usages = usageSearchData.usages = [],
pesticideMap = usageSearchData.pesticides = {},
id, entry;
usageSearchData.corpMap = {};
// include information from license data
licenses.forEach(function (lic) {
id = lic['農藥代號'];
entry = m[id];
copyIfAbsent(entry, lic, '英文名稱');
// the composition ratio might be different per license
//copyIfAbsent(entry, lic, '化學成分');
entry.licenses.push({
'許可證號': lic['許可證號'],
'化學成分': lic['化學成分'],
'廠牌名稱': lic['廠牌名稱'],
'國外原製造廠商': lic['國外原製造廠商'],
'有效期限': lic['有效期限'],
'廠商名稱': lic['廠商名稱']
});
});
// collect pesticide search: pesticide entries
index.forEach(function (entry) {
var products = {};
entry.licenses.forEach(function (lic) {
if (lic['廠牌名稱'])
products[lic['廠牌名稱']] = 1;
});
pesticideMap[entry.id] = {
id: entry.id,
name: entry.name || '',
engName: entry['英文名稱'] || '',
products: Object.keys(products).join('#')
};
});
// XXX: should ignore entries which are not in the index
gulp.src(['./_raw/download/pesticide/entries/*'])
.pipe(streamy.file.unvinylify())
.on('data', function (data) {
// merge into pesticide entries
//console.log(data.id);
//console.log(m[data.id]);
var entry = m[data.id];
if (!entry) return;
var record = moaObj[unorm.nfc(entry.name)];
if (record) {
entry['作用機制'] = record
} else {
entry['作用機制'] = '-'
}
if (entry === undefined) {
console.warn(
'Should update',
'`./_raw/download/pesticide/index.json`',
'for ' + data.id + '.'
);
entry = data
} else {
copyIfAbsent(entry, data, '廠牌名稱');
copyIfAbsent(entry, data, '通過日期');
copyIfAbsent(entry, data, 'usages');
}
// collect pesticide search: usage entries
data.usages.forEach(function (u) {
var usage = clone(
{
pesticideId: data.id,
'作用機制': entry['作用機制']
},
u
);
usages.push(usage);
});
})
.on('end', function () {
// TODO: write to _data/pestcide/entries/[id].json, replace gulp.dest('./pesticide')
// write pesticide entry pages
streamy.array(index)
.pipe(streamy.map.sync(function (data) {
return new File({
path: './' + data.id + '.json',
contents: new Buffer(JSON.stringify(data, null, '\t'))
});
}))
.pipe(gulp.dest('./_data/pesticide/entries'))
.on('data', function () {})
.on('end', cb); // end 4
});
},
function (cb) {
// write usages-search.json
fs.writeFile('./data/pesticide/usages-search.json',
JSON.stringify(usageSearchData, null, '\t'), cb); // end 3
}
],
function () {
callback();
});
}
);
});
/* Precondition: the following files are available:
* + /_raw/manual/pesticide/fomulations.json
*
* This task shall write the following files:
* + /_data/pesticide/formulations.json
* + /data/pesticide/formulations.json
*/
gulp.task('data.build.formulations', function (callback) {
// callback is invoked when end is called 2 times
var success = callback.bind(this, null); // XXX: assume everything is fine
var end = streamy.util.wait(2, success);
fs.readFile('./_raw/manual/pesticide/formulations.json', 'utf8', function (err, data) {
fs.writeFile('./_data/pesticide/formulations.json', data, end); // end 1
fs.writeFile('./data/pesticide/formulations.json', data, end); // end 2
});
});