-
Notifications
You must be signed in to change notification settings - Fork 3
/
bin.js
executable file
·90 lines (86 loc) · 2.7 KB
/
bin.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
#!/usr/bin/env node
'use strict';
require('colors');
var fs = require('fs');
var convert = require('./');
var path = require('path');
var argv = require('yargs')
.usage('Usage: $0 inTable [outTable] [options]')
.alias('p', 'postgres')
.describe('p', 'postgres connection config, should be a path to a json file'.yellow)
.default('p', null, '$POSTGRES_CONFIG')
.alias('c', 'cartodb')
.describe('c', 'cartodb connection config, should be a path to a json file'.yellow)
.default('c', null, '$CARTODB_CONFIG')
.alias('g', 'geometry')
.alias('v', 'version')
.describe('g', 'geometry field'.yellow)
.default('g', 'shape')
.alias('m', 'method')
.describe('m', 'import method'.yellow)
.default('m', 'create')
.alias('r', 'replace')
.describe('r', 'switch to replace mode'.yellow)
.alias('a', 'append')
.describe('a', 'switch to append mode'.yellow)
.alias('b', 'batchsize')
.describe('b', 'set the batch size'.yellow)
.default('b', 200)
.alias('d', 'direct')
.default('d', false)
.describe('d', 'upload directly to the table (create/append only)'.yellow)
.alias('s', 'subdomainless')
.boolean('s')
.default('s', undefined)
.describe('s', 'whether to use subdomainless url mode'.yellow)
.alias('D', 'domain')
.describe('D', 'whether to use the non default domain'.yellow)
.example('$0 -p ./postgres.json -c ./cartodb.json inTable outTable', 'specify the files'.green)
.example('$0 inTable', 'use environmental variables and the same table names'.green)
.example('$0 inTable outTable --no-g', 'use environmental variables and pass no geometry'.green)
.help('h', 'Show Help'.yellow)
.alias('h', 'help')
.argv;
if (argv.v || argv._[0] === 'version') {
console.log(require('./package.json').version);
process.exit();
}
var postgresConn = JSON.parse(fs.readFileSync(path.resolve(argv.postgres || process.env.POSTGRES_CONFIG)));
var cartodbConn = JSON.parse(fs.readFileSync(path.resolve(argv.cartodb || process.env.CARTODB_CONFIG)));
var geometry = argv.geometry;
var inTable = argv._[0];
var outTable = argv._[1] || inTable;
function getMethod() {
if (argv.a) {
return 'append';
}
if (argv.r) {
return 'replace';
}
return argv.m;
}
var config = {
postgres: {
geometry: geometry,
table: inTable,
connection: postgresConn
},
cartodb: {
connection: cartodbConn,
table: outTable
},
method: getMethod(),
progress: true,
batchSize: parseInt(argv.b, 10),
direct: argv.d,
domain: argv.D,
subdomainless: argv.s
};
convert(config, function (err) {
if (err) {
console.log(err && err.stack || err);
process.exit(1);
}
console.log('done'.green);
process.exit(0);
});