forked from untoldone/bloomapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.js
110 lines (90 loc) · 3.08 KB
/
make.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
require('shelljs/make');
var logger = require('./lib/logger');
// default target
target.all = function () {
console.log("\nAvailable commands:\n-------------------");
console.log(" node make bootstrap # fetch, process, and insert NPI data into database")
console.log(" node make fetch # like 'bootstrap' but assumes you already have the base sql database setup")
console.log(" node make server # run web/API server")
console.log(" node make check # checks your environment for configuration files and some dependencies")
console.log("\n For more information, visit the project README file on github at https://github.com/untoldone/bloomapi\n")
}
var Q = require('q');
Q.longStackSupport = true;
target.fetch = function () {
var pg = require('./lib/sources/pg'),
npi = require('./lib/sources/npi');
logger.data.info('attempting to sync datasources');
npi.update()
.fail(function (e) {
console.error("Error:\n" + e.stack);
process.exit(1);
})
.done(function () {
pg.end();
});
};
target.bootstrap = function () {
var pg = require('./lib/sources/pg'),
npi = require('./lib/sources/npi'),
fs = require('fs');
logger.data.info('bootstrapping bloomapi');
target.docs();
Q.nfcall(fs.readFile, './lib/bootstrap.sql', {encoding: 'utf8'})
.then(function (data) {
return Q.ninvoke(pg, 'query', data);
})
.fail(function (e) {
if(e.code == '42710'){ // "DUPLICATE OBJECT" according to http://www.postgresql.org/docs/9.0/static/errcodes-appendix.html
logger.data.info("looks like you already have the database schema set up. (bootstrap assumes you don't.)")
logger.data.info("if you need to update the NPI data, simply run `node make fetch`")
} else {
console.error("Error:\n", e.stack);
process.exit(1);
}
})
.then(npi.update)
.fail(function (e) {
console.error("Error:\n", e.stack);
process.exit(1);
})
.done(function () {
pg.end();
});
}
target.server = function () {
logger.server.info('starting bloomapi');
require('./lib/api/server');
};
target.docs = function () {
logger.data.info('building website');
var docs = require('./lib/docs');
docs.build().done();
};
target.check = function () {
if (!test('-f', './config.js')) {
console.log('ERROR: no configuration file found, copy and configure ./config.js.sample to ./config.js');
}
var valid = true,
pg = require('./lib/sources/pg'),
query = pg.query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'", function (err, result) {
if (err) {
console.log('ERROR: running query on database, check credentials');
valid = false;
}
if (!which('7z')) {
console.log('ERROR: p7zip is required for bootstrap task');
valid = false;
}
if (!which('pdftotext')) {
console.log('ERROR: pdftotext is required for bootstrap task');
valid = false;
}
if (valid) {
console.log('PASSED: all checks');
}
});
query.on('end', function () {
pg.end();
});
};