-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
83 lines (63 loc) · 1.61 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
var Firebase = require('firebase');
var args = require('./args');
var colors = require('colors');
var _ = require('./tools');
var Exporter = require('./exporter');
function fetch(url, cb, path) {
if (!cb) cb = function() {};
path = path || false;
var firebase = new Firebase(url);
firebase.on('value', function(snap) {
var value = snap.val();
var array = _.$toArray(value);
var data = _.prepareCSV(array);
var e = new Exporter(path);
if (path === false) {
return cb(e.toStream(data));
}
e.writeToFile(data);
e.on('finished', cb);
e.on('error', function(err) {
throw err;
});
});
}
if (require.main === module) {
args.extend('filepath', function() {
var path = this.penultimate();
if (!path) return false;
path = this.last();
return _.resolvePath(path);
});
args.extend('firebaseUrl', function() {
var arg = this.penultimate();
if (!arg) arg = this.last();
return arg;
});
var path = args.getArgument('filepath', './export.csv');
var firebaseUrl = args.getArgument('firebaseUrl', new Error('Must specify a firebase URL'));
colors.setTheme({
input: 'grey',
verbose: 'grey',
error: 'blue',
trace: 'red'
});
process.on('uncaughtException', function(error) {
console.log('Please correct the following error(s)'.underline)
if (!error.hasOwnProperty('type')) {
console.log( error.stack );
} else {
console.log( error.message.error.bold);
}
});
/*
fetch(firebaseUrl, function(stream) {
stream
.pipe(process.stdout);
stream.on('end', process.exit);
});*/
fetch(firebaseUrl, function() {
process.exit();
}, path);
}
module.exports = fetch;