Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / pkginfo / lib / pkginfo.js
1 /*
2  * pkginfo.js: Top-level include for the pkginfo module
3  *
4  * (C) 2011, Charlie Robbins
5  *
6  */
7  
8 var fs = require('fs'),
9     path = require('path');
10
11 //
12 // ### function pkginfo ([options, 'property', 'property' ..])
13 // #### @pmodule {Module} Parent module to read from.
14 // #### @options {Object|Array|string} **Optional** Options used when exposing properties.
15 // #### @arguments {string...} **Optional** Specified properties to expose.
16 // Exposes properties from the package.json file for the parent module on 
17 // it's exports. Valid usage:
18 //
19 // `require('pkginfo')()`
20 //
21 // `require('pkginfo')('version', 'author');`
22 //
23 // `require('pkginfo')(['version', 'author']);`
24 //
25 // `require('pkginfo')({ include: ['version', 'author'] });`
26 //
27 var pkginfo = module.exports = function (pmodule, options) {
28   var args = [].slice.call(arguments, 2).filter(function (arg) {
29     return typeof arg === 'string';
30   });
31   
32   //
33   // **Parse variable arguments**
34   //
35   if (Array.isArray(options)) {
36     //
37     // If the options passed in is an Array assume that
38     // it is the Array of properties to expose from the
39     // on the package.json file on the parent module.
40     //
41     options = { include: options };
42   }
43   else if (typeof options === 'string') {
44     //
45     // Otherwise if the first argument is a string, then
46     // assume that it is the first property to expose from
47     // the package.json file on the parent module.
48     //
49     options = { include: [options] };
50   }
51   
52   //
53   // **Setup default options**
54   //
55   options = options || {};
56   
57   // ensure that includes have been defined
58   options.include = options.include || [];
59   
60   if (args.length > 0) {
61     //
62     // If additional string arguments have been passed in
63     // then add them to the properties to expose on the 
64     // parent module. 
65     //
66     options.include = options.include.concat(args);
67   }
68   
69   var pkg = pkginfo.read(pmodule, options.dir).package;
70   Object.keys(pkg).forEach(function (key) {
71     if (options.include.length > 0 && !~options.include.indexOf(key)) {
72       return;
73     }
74     
75     if (!pmodule.exports[key]) {
76       pmodule.exports[key] = pkg[key];
77     }
78   });
79   
80   return pkginfo;
81 };
82
83 //
84 // ### function find (dir)
85 // #### @pmodule {Module} Parent module to read from.
86 // #### @dir {string} **Optional** Directory to start search from.
87 // Searches up the directory tree from `dir` until it finds a directory
88 // which contains a `package.json` file. 
89 //
90 pkginfo.find = function (pmodule, dir) {
91   if (! dir) {
92     dir = path.dirname(pmodule.filename);
93   }
94   
95   var files = fs.readdirSync(dir);
96   
97   if (~files.indexOf('package.json')) {
98     return path.join(dir, 'package.json');
99   }
100   
101   if (dir === '/') {
102     throw new Error('Could not find package.json up from: ' + dir);
103   }
104   else if (!dir || dir === '.') {
105     throw new Error('Cannot find package.json from unspecified directory');
106   }
107   
108   return pkginfo.find(pmodule, path.dirname(dir));
109 };
110
111 //
112 // ### function read (pmodule, dir)
113 // #### @pmodule {Module} Parent module to read from.
114 // #### @dir {string} **Optional** Directory to start search from.
115 // Searches up the directory tree from `dir` until it finds a directory
116 // which contains a `package.json` file and returns the package information.
117 //
118 pkginfo.read = function (pmodule, dir) { 
119   dir = pkginfo.find(pmodule, dir);
120   
121   var data = fs.readFileSync(dir).toString();
122       
123   return {
124     dir: dir, 
125     package: JSON.parse(data)
126   };
127 };
128
129 //
130 // Call `pkginfo` on this module and expose version.
131 //
132 pkginfo(module, {
133   dir: __dirname,
134   include: ['version'],
135   target: pkginfo
136 });