-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiscovery-service-seeder.js
43 lines (39 loc) · 1.73 KB
/
discovery-service-seeder.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
var async = require('async');
var glob = require("glob");
var fs = require("fs");
var seeder = {
run: function (discovery, discovery_conf, cb) {
console.log('seeder is running, this may take a while ... come back later ... ... \nseriously ... ... ...');
var files = glob.sync('documents/src/*/{*html,*doc,*pdf}');
discovery_conf.documents = [];
async.eachOfSeries(files, function loop(filename, counter, callback) {
console.log(counter + ' of ' + files.length + ' - ' + filename + ' uploading...');
var file = fs.readFileSync(filename);
var filename_type = filename.split('.');
var type = filename_type[filename_type.length - 1];
var params = {
environment_id: discovery_conf.environment.environment_id,
collection_id: discovery_conf.collection.collection_id,
configuration_id: discovery_conf.configuration.configuration_id,
file: file
};
discovery.addDocument(params, function (err, data) {
if (!err) {
data.filename = filename;
console.log(JSON.stringify(data, null, 2));
discovery_conf.documents.push(data);
} else console.log('error:' + err);
callback();
});
}, function finish(err) {
if (!err) {
console.log('---------------- seeder finished ----------------');
fs.writeFileSync('documents/learn_docs.json', JSON.stringify(discovery_conf.documents, null, 2));
} else {
console.log('error:' + err);
}
cb();
});
}
};
module.exports = seeder;