Export 0.2.1
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / grunt / node_modules / temporary / node_modules / package / lib / package.js
1 /**
2  * package - Easy package.json exports.
3  * 
4  * Author: Veselin Todorov <hi@vesln.com>
5  * Licensed under the MIT License.
6  */
7
8 /**
9  * Dependencies.
10  */
11 var fs = require('fs');
12 var path = require('path');
13 var exists = fs.existsSync || path.existsSync;
14
15 /**
16  * Package.
17  * 
18  * @param {String|null} location
19  * @returns {Object} package.json data
20  */
21 var package = function(location) {
22   if (location === Object(location)) {
23     location = package.discover(location);
24   }
25   return package.read(path.normalize(location + '/package.json'));
26 };
27
28 /**
29  * Reads and parses a package.json file.
30  * 
31  * @param {String} file
32  * @returns {Object} package.json data
33  */
34 package.read = function(file) {
35   var data = fs.readFileSync(file, 'utf8');
36   return JSON.parse(data);
37 };
38
39 /**
40  * Makes an atempt to find package.json file.
41  * 
42  * @returns {Object} package.json data
43  */
44 package.discover = function(module) {
45   var location = path.dirname(module.filename);
46   var found = null;
47   
48   while (!found) {
49     if (exists(location + '/package.json')) {
50       found = location;
51     } else if (location !== '/') {
52       location = path.dirname(location);
53     } else {
54       throw new Error('package.json can not be located');
55     }
56   }
57   
58   return found;
59 };
60
61 /**
62  * Exporting the lib.
63  */
64 module.exports = package;