/
readJson.js
53 lines (43 loc) · 1.67 KB
/
readJson.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
var path = require('path');
var bowerJson = require('bower-json');
var Q = require('q');
// The valid options are the same as bower-json#readFile.
// If the "assume" option is passed, it will be used if no json file was found
// This promise is resolved with [json, deprecated, assumed]
// - json: The read json
// - deprecated: The deprecated filename being used or false otherwise
// - assumed: True if a dummy json was returned if no json file was found, false otherwise
function readJson(file, options) {
options = options || {};
// Read
return Q.nfcall(bowerJson.read, file, options)
.spread(function (json, jsonFile) {
var deprecated;
if (options.logger) {
var issues = bowerJson.getIssues(json);
if (issues.warnings.length > 0){
options.logger.warn('invalid-meta', 'for:' + jsonFile);
}
issues.warnings.forEach(function (warning) {
options.logger.warn('invalid-meta', warning);
});
}
jsonFile = path.basename(jsonFile);
deprecated = jsonFile === 'component.json' ? jsonFile : false;
return [json, deprecated, false];
}, function (err) {
// No json file was found, assume one
if (err.code === 'ENOENT' && options.assume) {
return [bowerJson.parse(options.assume, options), false, true];
}
err.details = err.message;
if (err.file) {
err.message = 'Failed to read ' + err.file;
err.data = { filename: err.file };
} else {
err.message = 'Failed to read json from ' + file;
}
throw err;
});
}
module.exports = readJson;