+34
| 'use strict'; | ||
| const MODULE_REQUIRE = 1 | ||
| /* built-in */ | ||
| , fs = require('fs') | ||
| , path = require('path') | ||
| /* NPM */ | ||
| /* in-package */ | ||
| ; | ||
| let packageOf = function(id, mod) { | ||
| let packageJson = null; | ||
| let pathname = require.resolve(id, mod.paths); | ||
| let dirname = fs.statSync(pathname).isDirectory() ? pathname : path.dirname(pathname); | ||
| do { | ||
| let packageJsonPath = path.join(dirname, 'package.json'); | ||
| if (fs.existsSync(packageJsonPath)) { | ||
| packageJson = require(packageJsonPath); | ||
| break; | ||
| } | ||
| if (dirname == (dirname = path.dirname(dirname))) { | ||
| throw new Error('package.json not found'); | ||
| } | ||
| } while(1); | ||
| return packageJson; | ||
| }; | ||
| module.exports = packageOf; |
+6
-0
@@ -5,2 +5,8 @@ # noda Change Log | ||
| ## [0.5.0] - May 13th, 2018 | ||
| * `noda.packageOf()` added. | ||
| * `noda.packageRoot()` added. | ||
| * `noda.inResolve()` updated to accept more arguments as `path.resolve()` does. | ||
| ## [0.4.0] - April 1st, 2018 - BETA | ||
@@ -7,0 +13,0 @@ |
+12
-5
@@ -161,7 +161,6 @@ /** | ||
| */ | ||
| let inResolve = (subpath) => { | ||
| let inResolve = function(...subpath) { | ||
| // Find home directory of the package in which the caller is located. | ||
| let dirname = getCallerPackageDir(); | ||
| return path.join(dirname, subpath); | ||
| return path.join.apply(path, [ dirname ].concat(subpath)); | ||
| }; | ||
@@ -341,5 +340,13 @@ | ||
| /** | ||
| * Return the root path of current package. | ||
| */ | ||
| let packageRoot = function() { | ||
| return inResolve('.'); | ||
| }; | ||
| module.exports = { | ||
| bindings: require('./bindings'), | ||
| packageOf: require('./packageOf'), | ||
| currentPackage, | ||
@@ -360,3 +367,3 @@ inExists, | ||
| downResolve, | ||
| 'existsInPackage': inExists, | ||
@@ -363,0 +370,0 @@ 'readInPackage': inRead, |
+3
-1
| { | ||
| "name": "noda", | ||
| "version": "0.4.0", | ||
| "version": "0.5.0", | ||
| "description": "NOde Developing Assistant", | ||
@@ -10,3 +10,5 @@ "main": "index.js", | ||
| "keywords": [ | ||
| "bindings", | ||
| "require", | ||
| "require-addon", | ||
| "require-all", | ||
@@ -13,0 +15,0 @@ "package-homepath" |
+8
-5
@@ -47,2 +47,5 @@ # noda | ||
| * __noda.packageOf__(string *id*, Object *module*) | ||
| Return the object parsed from package.json which belongs to the special package named *id* according to the view angle of special *module*. | ||
| * __noda.currentPackage__() | ||
@@ -88,15 +91,15 @@ Return the object parsed from package.json which belongs to current package. | ||
| * __noda.existsInPackage__ | ||
| * __noda.existsInPackage__ | ||
| Alias of `noda.inExists`. | ||
| * __noda.readInPackage__ | ||
| * __noda.readInPackage__ | ||
| Alias of `noda.inRead`. | ||
| * __noda.requireInPackage__ | ||
| * __noda.requireInPackage__ | ||
| Alias of `noda.inRequire`. | ||
| * __noda.requireDirInPackage__ | ||
| * __noda.requireDirInPackage__ | ||
| Alias of `noda.inRequireDir`. | ||
| * __noda.resolveInPackage__ | ||
| * __noda.resolveInPackage__ | ||
| Alias of `noda.inResolve`. | ||
@@ -103,0 +106,0 @@ |
21642
6.66%9
12.5%460
7.23%135
2.27%10
25%