parent-package-json
Advanced tools
Comparing version 1.0.3 to 1.1.0
var fs = require('fs'), | ||
path = require('path'); | ||
module.exports = function findPackageJSON(startPath) { | ||
module.exports = function findPackageJSON(startPath, ignore) { | ||
startPath = startPath || process.cwd(); | ||
var searchPath = path.join(startPath + '/..'); | ||
ignore = ignore || 0; | ||
var searchPath = path.join(startPath + '/..'), fileFound = false, nextPath = ''; | ||
for (var fileFound = false, nextPath; !fileFound; searchPath = path.join(searchPath + '/..')) { | ||
while(!fileFound) { | ||
searchPath = nextPath || searchPath; | ||
try { | ||
fs.statSync(path.join(searchPath + '/package.json')); | ||
fileFound = true; | ||
break; | ||
if(ignore > 0) { | ||
ignore--; | ||
} else { | ||
fileFound = true; | ||
} | ||
} catch (e) {} | ||
@@ -21,3 +27,3 @@ | ||
if (!!fileFound) { | ||
if (fileFound) { | ||
return { | ||
@@ -24,0 +30,0 @@ read: function() { |
{ | ||
"name": "parent-package-json", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "Find, read and parse the package.json of the parent module", | ||
@@ -12,3 +12,3 @@ "main": "lib/index.js", | ||
"type": "git", | ||
"url": "git+https://github.com/maxrimue/parentPackageJSON.git" | ||
"url": "git+https://github.com/maxrimue/parent-package-json.git" | ||
}, | ||
@@ -15,0 +15,0 @@ "keywords": [ |
@@ -38,5 +38,12 @@ # parent-package-json | ||
```javascript | ||
var pathToParent = parent('/My/Cool/Folder').path; | ||
var pathToParentOfCustomPath = parent('/My/Cool/Folder').path; | ||
``` | ||
If you want to ignore a `package.json` (for example to find the parent `package.json` of the parent module), you can pass an | ||
ignore parameter (default: 0) saying how many `package.json`s you want to ignore when searching: | ||
```javascript | ||
var pathToParentOfParent = parent(null, 1).path; // Or, even more complicated: | ||
var pathToParentOfParentOfCustomPath = parent('/My/Cool/Folder', 1).path; | ||
``` | ||
## Processing the data | ||
@@ -56,2 +63,7 @@ | ||
var JSONOfParent = parent().parse(); | ||
var versionOfParent = JSONOfParent.version; | ||
// Or | ||
var versionOfParent = parent().parse().version; | ||
``` |
@@ -16,3 +16,6 @@ var assert = require('assert'), | ||
'fourth_folder': { | ||
'package.json': '{"version":"3.0.0"}' | ||
'package.json': '{"version":"3.0.0"}', | ||
'fifth_folder': { | ||
'package.json': '{"version":"4.0.0"}' | ||
} | ||
} | ||
@@ -40,2 +43,8 @@ } | ||
}); | ||
it('should ignore one package.json if an ignore parameter is passed', function() { | ||
expect(parent('test/first_folder/second_folder/third_folder/fourth_folder', 1).path).to.equal(path.normalize('test/first_folder/package.json')); | ||
}); | ||
it('should ignore two package.jsons if an ignore parameter is passed', function() { | ||
expect(parent('test/first_folder/second_folder/third_folder/fourth_folder/fifth_folder', 2).path).to.equal(path.normalize('test/first_folder/package.json')); | ||
}); | ||
}); | ||
@@ -42,0 +51,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8774
96
68