/
utils.js
56 lines (48 loc) · 1.07 KB
/
utils.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
var ini = require('ini');
var types = {
json: readJson,
yaml: readYaml,
ini: readIni,
env: readIni,
default: readFile // default for unsupported format or no extension file
};
var extensionPattern = /\.([^\.]+)$/i;
function readFile(grunt, file) {
return grunt.file.read(file);
};
function readJson(grunt, file) {
try {
return grunt.file.readJSON(file);
} catch(e) {
return;
}
}
function readYaml(grunt, file) {
try {
return grunt.file.readYAML(file);
} catch(e) {
return;
}
}
function readIni(grunt, file) {
try {
return ini.parse(grunt.file.read(file));
} catch(e) {
return;
}
}
module.exports = {
// Export a single parse function that call the proper parsing function
// Grunt is used here as it as already some parsing functions
parse: function (grunt, file) {
var match = file.match(extensionPattern);
var type = match ? match[1] : 'default'; // default to ini format
try {
var parseFn = types[type] || types.default;
return parseFn(grunt, file) || {};
}
catch (e) {
return;
}
}
};