-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·106 lines (88 loc) · 2.88 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
const through = require('through2');
const gutil = require('gulp-util');
const path = require('path');
const Vinyl = require('vinyl');
const dateFormat = require('dateformat');
var filenamePattern = new RegExp(/^([a-z]+)_([0-9]{6})_(.*)\.txt$/);
var joinedBuffer = new Buffer('');
function compile(options) {
options = options || {};
function quote(val) {
if (typeof val === 'string') {
return "'" + val.replace(/'/g, "\\'") + "'";
}
else if (val === undefined) {
return null;
}
return val;
}
function bufferContents(file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
else if (file.isStream()) {
cb(new gutil.PluginError('Streaming not supported'));
return;
}
try {
var tMessage = null;
var tClassName = null;
var osSdk = null;
var osRelease = null;
var crMid = null;
var content = JSON.parse(file.contents.toString());
var crdate = content.date;
var filename = path.basename(file.path);
var fnmatches = filenamePattern.exec(filename);
crMid = fnmatches[2]
var t = content.throwable;
var os = content.os;
var packageInfo = content.packageInfo;
var versionName = null;
if (t) {
tMessage = t.message;
tClassName = t.className;
}
if (os) {
osSdk = os.SDK_INT,
osRelease = os.RELEASE
}
if (packageInfo) {
versionName = packageInfo.versionName;
}
var insertStmt =
'insert into '
+ (options.table || 'crashreport')
+ ' (mid, ts, ex, msg, midversion, os_sdk, os_release) values ('
+ quote(crMid) + ', '
+ quote(crdate) + ', '
+ quote(tClassName) + ', '
+ quote(tMessage) + ', '
+ quote(versionName) + ', '
+ osSdk + ', '
+ quote(osRelease)
+ ');'
joinedBuffer = Buffer.concat([joinedBuffer, new Buffer(insertStmt + '\n')]);
//console.log(insertStmt);
}
catch (err) {
this.emit('error', new gutil.PluginError('crashreports', err, { fileName: file.path }));
}
cb();
}
function endStream(cb) {
var p = (options.filename || 'crashreport_' + dateFormat(new Date(), 'yyyy-mm-dd_HH-MM-ss') + ".sql");
var joinedFile = new Vinyl({
path: p,
contents: new Buffer(joinedBuffer)
});
this.push(joinedFile);
cb();
}
return through.obj(bufferContents, endStream);
}
module.exports = function (options) {
return compile(options);
};