Skip to content
Browse files

Using findpkg utility

  • Loading branch information...
1 parent 7b0d501 commit 24f0b6aaa6f0bf03789250f19368ab2f935e4a9a Frank Hellwig committed Sep 11, 2012
Showing with 6 additions and 34 deletions.
  1. +2 −1 package.json
  2. +4 −33 pkgconfig.js
View
3 package.json
@@ -1,6 +1,6 @@
{
"name": "pkgconfig",
- "version": "0.0.1",
+ "version": "0.0.2",
"description": "Reads and validates JSON configuration files for Node.js packages.",
"keywords": ["config","json","schema"],
"licenses": [{
@@ -14,6 +14,7 @@
},
"main": "./pkgconfig.js",
"dependencies": {
+ "findpkg": "0.0.x",
"jsvutil": "0.0.x"
},
"engine": {
View
37 pkgconfig.js
@@ -23,10 +23,10 @@
// Required modules
var fs = require('fs');
var path = require('path');
+var findpkg = require('findpkg');
var jsvutil = require('jsvutil');
// Pathname constants
-var PACKAGE_FILE = 'package.json';
var DEFAULT_SCHEMA = path.join('config', 'schema');
var DEFAULT_CONFIG = path.join('config', 'config');
@@ -42,24 +42,6 @@ function isFile(pathname) {
}
/**
- * Given a directory, tries to find the file in that directory. Tries the
- * parent directory if not found. Returns the pathname or null if not found.
- */
-function findFile(directory, filename) {
- var current = directory;
- var previous = null;
- while (current !== previous) {
- var pathname = path.join(current, filename);
- if (isFile(pathname)) {
- return pathname;
- }
- previous = current;
- current = path.join(current, '..');
- }
- return null;
-}
-
-/**
* Reads the file as either a JavaScript module or a JSON file.
* Returns a JavaScript object or null if the file is not found.
*/
@@ -104,24 +86,12 @@ function removeExtension(pathname) {
}
/**
- * Returns the directory for the package of the parent module.
- */
-function getPackageBase() {
- var directory = path.dirname(module.parent.filename);
- var pathname = findFile(directory, PACKAGE_FILE);
- if (pathname === null) {
- throw new Error('Package file not found: ' + PACKAGE_FILE);
- }
- return path.dirname(pathname);
-}
-
-/**
* Validates the options argument passed to the pkgconfig function.
* Returns an options object with the default values applied.
*/
function validateOptions(options) {
if (typeof options === 'undefined') {
- options = {};
+ options = {};
}
return jsvutil.validate(options, {
type: 'object',
@@ -175,7 +145,8 @@ function getConfig(base, option) {
* The function exported by this module.
*/
function pkgconfig(options) {
- var base = getPackageBase();
+ var pkginfo = findpkg(module.parent);
+ var base = pkginfo.dirname;
var options = validateOptions(options);
var schema = getSchema(base, options.schema);
var config = getConfig(base, options.config);

0 comments on commit 24f0b6a

Please sign in to comment.
Something went wrong with that request. Please try again.