Permalink
Browse files

[api] Update `pkginfo` API to take the module to extend. This solves …

…the module caching issue when multiple modules require pkginfo
  • Loading branch information...
1 parent 36b827b commit ca2382c978be9b083266acaafd0f675e2890663b @indexzero committed Jun 8, 2011
@@ -6,7 +6,7 @@
*/
var util = require('util'),
- pkginfo = require('../lib/pkginfo')();
+ pkginfo = require('../lib/pkginfo')(module);
exports.someFunction = function () {
console.log('some of your custom logic here');
@@ -7,7 +7,7 @@
*/
var util = require('util'),
- pkginfo = require('../lib/pkginfo')(['version', 'author']);
+ pkginfo = require('../lib/pkginfo')(module, ['version', 'author']);
exports.someFunction = function () {
console.log('some of your custom logic here');
@@ -6,7 +6,7 @@
*/
var util = require('util'),
- pkginfo = require('../lib/pkginfo')('version', 'author');
+ pkginfo = require('../lib/pkginfo')(module, 'version', 'author');
exports.someFunction = function () {
console.log('some of your custom logic here');
@@ -7,7 +7,7 @@
*/
var util = require('util'),
- pkginfo = require('../lib/pkginfo')({
+ pkginfo = require('../lib/pkginfo')(module, {
include: ['version', 'author']
});
@@ -6,7 +6,7 @@
*/
var util = require('util'),
- pkginfo = require('../lib/pkginfo')('version');
+ pkginfo = require('../lib/pkginfo')(module, 'version');
exports.someFunction = function () {
console.log('some of your custom logic here');
View
@@ -10,6 +10,7 @@ var fs = require('fs'),
//
// ### function pkginfo ([options, 'property', 'property' ..])
+// #### @pmodule {Module} Parent module to read from.
// #### @options {Object|Array|string} **Optional** Options used when exposing properties.
// #### @arguments {string...} **Optional** Specified properties to expose.
// Exposes properties from the package.json file for the parent module on
@@ -23,8 +24,8 @@ var fs = require('fs'),
//
// `require('pkginfo')({ include: ['version', 'author'] });`
//
-var pkginfo = module.exports = function (options) {
- var args = [].slice.call(arguments, 1).filter(function (arg) {
+var pkginfo = module.exports = function (pmodule, options) {
+ var args = [].slice.call(arguments, 2).filter(function (arg) {
return typeof arg === 'string';
});
@@ -51,8 +52,7 @@ var pkginfo = module.exports = function (options) {
//
// **Setup default options**
//
- options = options || { include: [] };
- options.target = options.target || module.parent.exports;
+ options = options || { include: [] };
if (args.length > 0) {
//
@@ -63,14 +63,14 @@ var pkginfo = module.exports = function (options) {
options.include = options.include.concat(args);
}
- var pkg = pkginfo.read(options.dir).package;
+ var pkg = pkginfo.read(pmodule, options.dir).package;
Object.keys(pkg).forEach(function (key) {
if (options.include.length > 0 && !~options.include.indexOf(key)) {
return;
}
- if (!options.target[key]) {
- options.target[key] = pkg[key];
+ if (!pmodule.exports[key]) {
+ pmodule.exports[key] = pkg[key];
}
});
@@ -79,12 +79,13 @@ var pkginfo = module.exports = function (options) {
//
// ### function find (dir)
+// #### @pmodule {Module} Parent module to read from.
// #### @dir {string} **Optional** Directory to start search from.
// Searches up the directory tree from `dir` until it finds a directory
// which contains a `package.json` file.
//
-pkginfo.find = function (dir) {
- dir = dir || module.parent.filename;
+pkginfo.find = function (pmodule, dir) {
+ dir = dir || pmodule.filename;
dir = path.dirname(dir);
var files = fs.readdirSync(dir);
@@ -94,21 +95,23 @@ pkginfo.find = function (dir) {
}
if (dir === '/') {
- throw new Error('Could not find package.json up from: ' + module.parent.filename);
+ throw new Error('Could not find package.json up from: ' + dir);
}
return pkginfo.find(dir);
};
//
-// ### function read (dir)
+// ### function read (pmodule, dir)
+// #### @pmodule {Module} Parent module to read from.
// #### @dir {string} **Optional** Directory to start search from.
// Searches up the directory tree from `dir` until it finds a directory
// which contains a `package.json` file and returns the package information.
//
-pkginfo.read = function (dir) {
- var dir = pkginfo.find(dir),
- data = fs.readFileSync(dir).toString();
+pkginfo.read = function (pmodule, dir) {
+ dir = pkginfo.find(pmodule, dir);
+
+ var data = fs.readFileSync(dir).toString();
return {
dir: dir,
@@ -119,7 +122,7 @@ pkginfo.read = function (dir) {
//
// Call `pkginfo` on this module and expose version.
//
-pkginfo({
+pkginfo(module, {
dir: __dirname,
include: ['version'],
target: pkginfo

0 comments on commit ca2382c

Please sign in to comment.