-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Helper '#withPackageOf' adds file-path relative to the project root
- Added as "@relativepath" - Refactorings for when resolving the package.json of a file - Remove dependency on `find-package` in favour of a custom implementation
- Loading branch information
Showing
8 changed files
with
152 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const path = require('path') | ||
const fs = require('fs') | ||
const debug = require('debug')('thought:resolve-package') | ||
|
||
module.exports = {resolvePackageRoot} | ||
|
||
/** | ||
* Find the `package.json` by walking up from a given file. | ||
* The result contains the following properties | ||
* | ||
* * **packageRoot**: The base-directory of the package containing the file (i.e. the parent of the `package.json` | ||
* * **relativeFile**: The path of "file" relative to the packageRoot | ||
* * **packageJson**: The required package.json | ||
* | ||
* @param file | ||
* @return {{packageRoot: string, packageJson: object, relativeFile: string}} the path to the package.json | ||
*/ | ||
function resolvePackageRoot (file) { | ||
try { | ||
const fullPath = path.resolve(file) | ||
for (let lead = fullPath; path.dirname(lead) !== lead; lead = path.dirname(lead)) { | ||
debug('Looking for package.json in ' + lead) | ||
let packagePath = path.join(lead, 'package.json') | ||
try { | ||
if (fs.statSync(packagePath).isFile()) { | ||
return Promise.resolve({ | ||
packageRoot: path.relative(process.cwd(), lead), | ||
relativeFile: path.relative(lead, fullPath), | ||
packageJson: require(packagePath) | ||
}) | ||
} | ||
} catch (e) { | ||
/* istanbul ignore else */ | ||
switch (e.code) { | ||
case 'ENOTDIR': | ||
case 'ENOENT': | ||
continue | ||
default: | ||
throw e | ||
} | ||
} | ||
} | ||
} catch (e) { | ||
return Promise.reject(e) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "mini-project", | ||
"version": "1.0.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/*! | ||
* thought <https://github.com/nknapp/thought> | ||
* | ||
* Copyright (c) 2015 Nils Knappmeier. | ||
* Released under the MIT license. | ||
*/ | ||
|
||
/* eslint-env mocha */ | ||
|
||
'use strict' | ||
|
||
var chai = require('chai') | ||
chai.use(require('chai-as-promised')) | ||
chai.use(require('dirty-chai')) | ||
var expect = chai.expect | ||
var {resolvePackageRoot} = require('../lib/utils/resolve-package-root') | ||
|
||
describe('The "resolve-package-root" utility', function () { | ||
var statSync | ||
var fs = require('fs') | ||
|
||
beforeEach(function () { | ||
statSync = fs.statSync | ||
}) | ||
|
||
afterEach(function () { | ||
fs.statSync = statSync | ||
}) | ||
|
||
it('find a package.json file and provide the relative path of the given file', function () { | ||
return expect(resolvePackageRoot('test/fixtures/mini-project/a/b/test.txt')).to.eventually.deep.equal({ | ||
packageRoot: 'test/fixtures/mini-project', | ||
relativeFile: 'a/b/test.txt', | ||
packageJson: {'name': 'mini-project', 'version': '1.0.0'} | ||
}) | ||
}) | ||
|
||
it('find not be bothered by directory named "package.json"', function () { | ||
return expect(resolvePackageRoot('test/fixtures/mini-project/a/b/package.json/test.txt')).to.eventually.deep.equal({ | ||
packageRoot: 'test/fixtures/mini-project', | ||
relativeFile: 'a/b/package.json/test.txt', | ||
packageJson: {'name': 'mini-project', 'version': '1.0.0'} | ||
}) | ||
}) | ||
|
||
it('should return a rejected promise on unexpected exceptions', function () { | ||
fs.statSync = function () { | ||
throw new Error('Test-Error') | ||
} | ||
return expect(resolvePackageRoot('test/fixtures/mini-project/a/b/package.json/test.txt')).to.be.rejectedWith('Test-Error') | ||
}) | ||
}) |