2 * package - Easy package.json exports.
4 * Author: Veselin Todorov <hi@vesln.com>
5 * Licensed under the MIT License.
11 var fs = require('fs');
12 var path = require('path');
13 var exists = fs.existsSync || path.existsSync;
18 * @param {String|null} location
19 * @returns {Object} package.json data
21 var package = function(location) {
22 if (location === Object(location)) {
23 location = package.discover(location);
25 return package.read(path.normalize(location + '/package.json'));
29 * Reads and parses a package.json file.
31 * @param {String} file
32 * @returns {Object} package.json data
34 package.read = function(file) {
35 var data = fs.readFileSync(file, 'utf8');
36 return JSON.parse(data);
40 * Makes an atempt to find package.json file.
42 * @returns {Object} package.json data
44 package.discover = function(module) {
45 var location = path.dirname(module.filename);
49 if (exists(location + '/package.json')) {
51 } else if (location !== '/') {
52 location = path.dirname(location);
54 throw new Error('package.json can not be located');
64 module.exports = package;