parent-package-json
Advanced tools
Comparing version 1.1.0 to 2.0.0
@@ -1,40 +0,44 @@ | ||
var fs = require('fs'), | ||
path = require('path'); | ||
'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
module.exports = function findPackageJSON(startPath, ignore) { | ||
startPath = startPath || process.cwd(); | ||
ignore = ignore || 0; | ||
var searchPath = path.join(startPath + '/..'), fileFound = false, nextPath = ''; | ||
startPath = startPath || process.cwd(); | ||
ignore = ignore || 0; | ||
while(!fileFound) { | ||
searchPath = nextPath || searchPath; | ||
let searchPath = path.join(startPath + '/..'); | ||
let fileFound = false; | ||
let nextPath = ''; | ||
try { | ||
fs.statSync(path.join(searchPath + '/package.json')); | ||
if(ignore > 0) { | ||
ignore--; | ||
} else { | ||
fileFound = true; | ||
} | ||
} catch (e) {} | ||
while (!fileFound) { | ||
searchPath = nextPath || searchPath; | ||
nextPath = path.join(searchPath + '/..'); | ||
if (nextPath === '/' || nextPath === '.' || nextPath === '..') { | ||
break; | ||
} | ||
} | ||
try { | ||
fs.statSync(path.join(searchPath + '/package.json')); | ||
if (ignore > 0) { | ||
ignore--; | ||
} else { | ||
fileFound = true; | ||
} | ||
} catch (err) {} | ||
if (fileFound) { | ||
return { | ||
read: function() { | ||
return fs.readFileSync(path.join(searchPath + '/package.json'), 'utf8'); | ||
}, | ||
parse: function() { | ||
return JSON.parse(fs.readFileSync(path.join(searchPath + '/package.json'), 'utf8')); | ||
}, | ||
path: path.join(searchPath + '/package.json') | ||
}; | ||
} else { | ||
return false; | ||
} | ||
nextPath = path.join(searchPath + '/..'); | ||
if (nextPath === path.normalize('/') || nextPath === '.' || nextPath === '..') { | ||
break; | ||
} | ||
} | ||
if (fileFound) { | ||
return { | ||
read: function () { | ||
return fs.readFileSync(path.join(searchPath + '/package.json'), 'utf8'); | ||
}, | ||
parse: function () { | ||
return JSON.parse(fs.readFileSync(path.join(searchPath + '/package.json'), 'utf8')); | ||
}, | ||
path: path.join(searchPath + '/package.json') | ||
}; | ||
} | ||
return false; | ||
}; |
{ | ||
"name": "parent-package-json", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "Find, read and parse the package.json of the parent module", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "mocha", | ||
"coverage": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -R spec ./test/*" | ||
"test": "xo && nyc ava", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls" | ||
}, | ||
@@ -23,9 +23,9 @@ "repository": { | ||
"devDependencies": { | ||
"chai": "3.x.x", | ||
"ava": "^0.16.0", | ||
"coveralls": "2.x.x", | ||
"istanbul": "0.x.x", | ||
"mocha": "2.x.x" | ||
"nyc": "^8.1.0", | ||
"xo": "^0.16.0" | ||
}, | ||
"engines": { | ||
"node": ">= 0.10" | ||
"node": ">= 4" | ||
}, | ||
@@ -37,6 +37,3 @@ "author": "Max Rittmüller", | ||
}, | ||
"homepage": "https://github.com/maxrimue/parentPackageJSON#readme", | ||
"dependencies": { | ||
"mock-fs": "^3.6.0" | ||
} | ||
"homepage": "https://github.com/maxrimue/parentPackageJSON#readme" | ||
} |
@@ -6,3 +6,7 @@ # parent-package-json | ||
[![Coverage Status](https://coveralls.io/repos/maxrimue/parent-package-json/badge.svg?branch=master&service=github)](https://coveralls.io/github/maxrimue/parent-package-json?branch=master) | ||
[![dependencies Status](https://david-dm.org/maxrimue/parent-package-json/status.svg)](https://david-dm.org/maxrimue/parent-package-json) | ||
[![devDependencies Status](https://david-dm.org/maxrimue/parent-package-json/dev-status.svg)](https://david-dm.org/maxrimue/parent-package-json?type=dev) | ||
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) | ||
Using parent-package-json, you can find the parent `package.json`, so the `package.json` of the module that uses your module. | ||
@@ -49,2 +53,4 @@ | ||
``` | ||
__Note__: The module's own `package.json` is __always__ ignored, even if the ignore parameter equals 0 | ||
## Processing the data | ||
@@ -51,0 +57,0 @@ |
@@ -1,67 +0,23 @@ | ||
var assert = require('assert'), | ||
parent = require('../lib/index.js'), | ||
mock = require('mock-fs'), | ||
fs = require('fs'), | ||
path = require('path'), | ||
expect = require('chai').expect; | ||
const path = require('path'); | ||
const test = require('ava').test; | ||
const parent = require('../lib/index.js'); | ||
var mockFs = { | ||
'test': { | ||
'first_folder': { | ||
'package.json': '{"version":"2.0.0"}', | ||
'second_folder': { | ||
'third_folder': { | ||
'package.json': '{"version":"1.0.0"}', | ||
'fourth_folder': { | ||
'package.json': '{"version":"3.0.0"}', | ||
'fifth_folder': { | ||
'package.json': '{"version":"4.0.0"}' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
test('find right package.json', t => { | ||
t.is(parent('./fixtures/firstFolder/secondFolder').path, path.normalize('fixtures/firstFolder/package.json')); | ||
}); | ||
mock(mockFs); | ||
describe('Finding files', function() { | ||
it('should find a package.json in the parent directory', function() { | ||
expect(parent('test/first_folder/second_folderthird_folder').path).to.equal(path.normalize('test/first_folder/package.json')); | ||
}); | ||
it('should find the parent package.json and ignore its own', function() { | ||
expect(parent('test/first_folder/second_folder/third_folder/fourth_folder').path).to.equal(path.normalize('test/first_folder/second_folder/third_folder/package.json')); | ||
}); | ||
it('should continue until a package.json is found', function() { | ||
expect(parent('test/first_folder/second_folder/third_folder').path).to.equal(path.normalize('test/first_folder/package.json')); | ||
}); | ||
it('should equal to false if no package.json can be found', function() { | ||
expect(parent('test/first_folder')).to.equal(false); | ||
}); | ||
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')); | ||
}); | ||
test('ignore one package.json', t => { | ||
t.is(parent('./fixtures/firstFolder/secondFolder/thirdFolder/fourthFolder', 1).path, path.normalize('fixtures/firstFolder/package.json')); | ||
}); | ||
describe('Reading content', function() { | ||
it('should be able to read a package.json', function() { | ||
expect(parent('test/first_folder/second_folder/third_folder').path).to.equal(path.normalize('test/first_folder/package.json')); | ||
expect(parent('test/first_folder/second_folder/third_folder').read()).to.equal('{"version":"2.0.0"}'); | ||
}); | ||
test('read package.json', t => { | ||
t.deepEqual(JSON.parse(parent('./fixtures/firstFolder/secondFolder').read()), {version: '1.0.0'}); | ||
}); | ||
describe('Parsing content', function() { | ||
it('should be able to read and parse a package.json', function() { | ||
expect(parent('test/first_folder/second_folder/third_folder').path).to.equal(path.normalize('test/first_folder/package.json')); | ||
expect(parent('test/first_folder/second_folder/third_folder').parse()).to.eql({'version': '2.0.0'}); | ||
expect(parent('test/first_folder/second_folder/third_folder').parse().version).to.equal('2.0.0'); | ||
}); | ||
test('parse package.json', t => { | ||
t.deepEqual(parent('./fixtures/firstFolder/secondFolder').parse(), {version: '1.0.0'}); | ||
}); | ||
after(function() { | ||
mock.restore(); | ||
test('fail at finding package.json', t => { | ||
t.is(parent('/'), false); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
0
11
74
1
6776
55
- Removedmock-fs@^3.6.0
- Removedmock-fs@3.12.1(transitive)
- Removedrewire@2.5.2(transitive)
- Removedsemver@5.3.0(transitive)