-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-multiline.js
More file actions
61 lines (48 loc) · 1.55 KB
/
string-multiline.js
File metadata and controls
61 lines (48 loc) · 1.55 KB
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
var fs = require('fs');
var exceptions = {
fileNotExists: function(filePath){
return new Error('File not found: ' + filePath);
},
errorRunningRegEx: function(data){
return new Error('Unknown error running the RegExp on: ' + data);
}
};
function parseFile(error, data, callback, errorCallback){
if( error ){
console.error( error );
errorCallback( error );
return null;
}
var rawPattern = '<<<([A-z0-9_\-]+)[^]([^]*?)[^]>>>';
var pattern = new RegExp(rawPattern, 'gmi');
var rowPattern = new RegExp(rawPattern, 'mi');
try{
var matches = data.match(pattern);
}catch(exception){
console.error( exception );
errorCallback( exceptions.errorRunningRegEx(data) );
return null;
}
var varsCollector = {};
if( matches ){
matches = matches.forEach(function(item){
var rowMatch = item.match(rowPattern);
varsCollector[rowMatch[1]] = rowMatch[2];
});
}
callback(varsCollector);
}
exports.parseMultilineVars = function(filePath, callback, errorCallback, encoding){
encoding = encoding || 'utf8';
fs.exists(filePath, function(exists){
if( !exists ){
console.error( exceptions.fileNotExists(filePath) );
errorCallback( exceptions.fileNotExists(filePath) );
return null;
}
fs.readFile(filePath, encoding, function(error, data){
parseFile(error, data, callback, errorCallback);
});
});
}
exports.exceptions = exceptions;