UNPKG

3.5 kBJavaScriptView Raw
1/*
2 * pkginfo.js: Top-level include for the pkginfo module
3 *
4 * (C) 2011, Charlie Robbins
5 *
6 */
7
8var 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//
27var 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 || { include: [] };
56
57 if (args.length > 0) {
58 //
59 // If additional string arguments have been passed in
60 // then add them to the properties to expose on the
61 // parent module.
62 //
63 options.include = options.include.concat(args);
64 }
65
66 var pkg = pkginfo.read(pmodule, options.dir).package;
67 Object.keys(pkg).forEach(function (key) {
68 if (options.include.length > 0 && !~options.include.indexOf(key)) {
69 return;
70 }
71
72 if (!pmodule.exports[key]) {
73 pmodule.exports[key] = pkg[key];
74 }
75 });
76
77 return pkginfo;
78};
79
80//
81// ### function find (dir)
82// #### @pmodule {Module} Parent module to read from.
83// #### @dir {string} **Optional** Directory to start search from.
84// Searches up the directory tree from `dir` until it finds a directory
85// which contains a `package.json` file.
86//
87pkginfo.find = function (pmodule, dir) {
88 dir = dir || pmodule.filename;
89 dir = path.dirname(dir);
90
91 var files = fs.readdirSync(dir);
92
93 if (~files.indexOf('package.json')) {
94 return path.join(dir, 'package.json');
95 }
96
97 if (dir === '/') {
98 throw new Error('Could not find package.json up from: ' + dir);
99 }
100 else if (!dir || dir === '.') {
101 throw new Error('Cannot find package.json from unspecified directory');
102 }
103
104 return pkginfo.find(pmodule, dir);
105};
106
107//
108// ### function read (pmodule, dir)
109// #### @pmodule {Module} Parent module to read from.
110// #### @dir {string} **Optional** Directory to start search from.
111// Searches up the directory tree from `dir` until it finds a directory
112// which contains a `package.json` file and returns the package information.
113//
114pkginfo.read = function (pmodule, dir) {
115 dir = pkginfo.find(pmodule, dir);
116
117 var data = fs.readFileSync(dir).toString();
118
119 return {
120 dir: dir,
121 package: JSON.parse(data)
122 };
123};
124
125//
126// Call `pkginfo` on this module and expose version.
127//
128pkginfo(module, {
129 dir: __dirname,
130 include: ['version'],
131 target: pkginfo
132});
\No newline at end of file