-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote-json-parser.js
145 lines (117 loc) · 2.97 KB
/
remote-json-parser.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Usage: require this with processing function
exports = module.exports = function(done) {
migration.loadData('[]', [processing function], done);
};
*/
var request = require('request'),
fs = require('fs'),
readline = require('readline'),
dir = __dirname + '/data';
// Settings
var cacheFile = true, // cache the file locally for use
logErrorsToFile = true;
// Ensure the data dir exists
try {
if (!fs.statSync(dir).isDirectory()) {
throw new Error(dir + ' exists and is not a directory');
}
} catch (err) {
if (err.code === 'ENOENT') {
fs.mkdirSync(dir);
} else {
throw err;
}
}
/**
Data Loading
============
Loads the data to migrate from .json files on a remote server
*/
var dataServer = '[server]',
dataPath = '[foler page]',
dataKeyMap = {
'[key]': '[filename]'
};
exports.loadData = function(key, it, callback) {
var path = dataServer + '/' + dataPath + '/' + dataKeyMap[key],
localFilePath = dir + '/' + dataKeyMap[key];
if (!(key in dataKeyMap)) {
throw new Error('Invalid data key provided (' + key + ').')
}
// if the file exists locally to use it, otherwise get it from remote.
fs.exists(dataKeyMap[key], function(exists) {
if (exists) {
return readDataFile();
} else {
request(path).on('end', function() {
return readDataFile();
}).pipe(fs.createWriteStream(dataKeyMap[key]));
}
});
var lines = 0,
complete = false;
var check = function() {
if (!lines && complete) callback();
}
var readDataFile = function() {
var rd = readline.createInterface({
input: fs.createReadStream(dataKeyMap[key]),
output: process.stdout,
terminal: false
});
rd.on('line', function(line) {
lines++;
try {
var obj = JSON.parse(line.toString()); // parse the JSON
it(obj, function() {
lines--;
check();
});
}
catch(e){
lines--;
console.log("------------------------------");
console.log("ERROR PROCESSING JSON:");
console.log("------------------------------");
// log errors to console
console.log(e);
console.log(line.toString());
// log error to file
if (logErrorsToFile)
logErrorToFile(line.toString());
console.log("------------------------------");
}
}).on('close', function() {
if (!cacheFile) {
fs.unlink(localFilePath, function(err) {
complete = true;
check();
});
} else {
complete = true;
check();
}
});
}
// Log errors to file
var logErrorToFile = function(data) {
var logFile = 'errors.log';
// Check the error log exists
fs.exists(logFile, function(exists) {
if (!exists) {
// if the file doesn't - create it
fs.writeFile(logFile, data, function (err) {
if (err) { console.log('Unable to create log file'); }
return;
});
} else {
// if it does - append to it
fs.appendFile(logFile, '\n' + data, function (err) {
if (err) { console.log('Unable to appended error to file!'); }
return;
});
}
});
}
}