bbc8ba06e87e870a7e8936c738d77ab5d08ef349.svn-base
1.27 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
62
63
64
/**
* package - Easy package.json exports.
*
* Author: Veselin Todorov <hi@vesln.com>
* Licensed under the MIT License.
*/
/**
* Dependencies.
*/
var fs = require('fs');
var path = require('path');
var exists = fs.existsSync || path.existsSync;
/**
* Package.
*
* @param {String|null} location
* @returns {Object} package.json data
*/
var package = function(location) {
if (location === Object(location)) {
location = package.discover(location);
}
return package.read(path.normalize(location + '/package.json'));
};
/**
* Reads and parses a package.json file.
*
* @param {String} file
* @returns {Object} package.json data
*/
package.read = function(file) {
var data = fs.readFileSync(file, 'utf8');
return JSON.parse(data);
};
/**
* Makes an atempt to find package.json file.
*
* @returns {Object} package.json data
*/
package.discover = function(module) {
var location = path.dirname(module.filename);
var found = null;
while (!found) {
if (exists(location + '/package.json')) {
found = location;
} else if (location !== '/') {
location = path.dirname(location);
} else {
throw new Error('package.json can not be located');
}
}
return found;
};
/**
* Exporting the lib.
*/
module.exports = package;