Skip to content

Commit 03e810f

Browse files
committed
My whole thing is in an initial commit. Means this too easy. No stars
0 parents  commit 03e810f

File tree

6 files changed

+178
-0
lines changed

6 files changed

+178
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
node_js:
3+
- "0.10"
4+
5+
notifications:
6+
email: false

Readme.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
### resolve-dependency-path
2+
3+
> Convert a dependency path into a filepath
4+
5+
I literally wrote this simple function in a handful of projects and decided it should
6+
be its own module.
7+
8+
### Usage
9+
10+
```js
11+
var resolvePath = require('resolve-dependency-path');
12+
13+
var resolved = resolvePath(depPath, filename, directory);
14+
```
15+
16+
* `depPath`: the actual dependency path (probably extracted from a `require()`)
17+
* filename: the file that required this dependency (likely the file whose dependencies are being extracted)
18+
* directory: the root of all modules being processed. Dependencies are often about this root unless they're relative.
19+
20+
### Example
21+
22+
If you have a file like:
23+
24+
*myapp/foo.js*
25+
26+
```js
27+
var require('./bar');
28+
```
29+
30+
Then if you want to open the file associated with the dependency, you need to resolve `./bar` onto the filesystem.
31+
32+
Since `./bar` is a relative path, it should be resolved relative to `foo.js`,
33+
more specifically the directory containing `foo.js`, `myapp/`. This resolution would yield
34+
`myapp/bar.js`.
35+
36+
This is why the `filename` attribute is required to use this library.
37+
38+
If you have a non-relative dependency path like:
39+
40+
*myapp/foo.js*
41+
42+
```js
43+
define([
44+
'bar'
45+
], function(bar) {
46+
47+
});
48+
```
49+
50+
Then `bar` is relative to the root of all files, `myapp`. The resolution would yield
51+
`myapp/bar.js`.
52+
53+
A more complex example with subdirectories:
54+
55+
*myapp/feature1/foo.js*
56+
57+
```js
58+
define([
59+
'feature2/bar'
60+
], function(bar) {
61+
62+
});
63+
```
64+
65+
The dependency `feature2/bar` is relative to the root of all files, `myapp`, *not* the file `foo.js`.
66+
67+
This is why the `directory` attribute is required to use this library.

index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var path = require('path');
2+
3+
/**
4+
* Resolve a dependency's path
5+
* @param {String} dep - The dependency name to resolve
6+
* @param {String} filename - Filename that contains the dependency
7+
* @param {String} directory - Root of all files
8+
* @return {String} Absolute/resolved path of the dependency
9+
*/
10+
module.exports = function(dep, filename, directory) {
11+
if (!dep) { throw new Error('dependency path not given'); }
12+
if (!filename) { throw new Error('filename not given'); }
13+
if (!directory) { throw new Error('directory not given'); }
14+
15+
var depExt = path.extname(dep);
16+
var fileExt = path.extname(filename);
17+
var filepath;
18+
19+
if (isRelative(dep)) {
20+
filepath = path.resolve(path.dirname(filename), dep);
21+
} else {
22+
filepath = path.resolve(directory, dep);
23+
}
24+
25+
if (!depExt) {
26+
filepath += fileExt;
27+
}
28+
29+
return filepath;
30+
}
31+
32+
function isRelative(dep) {
33+
return dep.indexOf('..') === 0 || dep.indexOf('.') === 0;
34+
}

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "resolve-dependency-path",
3+
"version": "1.0.0",
4+
"description": "Convert a dependency path into a filepath",
5+
"main": "index.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"scripts": {
10+
"test": "jscs -p google index.js test/test.js && mocha test/test.js"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/mrjoelkemp/node-resolve-dependency-path.git"
15+
},
16+
"keywords": [
17+
"dependency",
18+
"filepath",
19+
"module"
20+
],
21+
"author": "Joel Kemp <[email protected]> (http://www.mrjoelkemp.com/)",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/mrjoelkemp/node-resolve-dependency-path/issues"
25+
},
26+
"homepage": "https://github.com/mrjoelkemp/node-resolve-dependency-path",
27+
"devDependencies": {
28+
"jscs": "~1.8.0",
29+
"mocha": "~2.0.1"
30+
}
31+
}

test/test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var assert = require('assert');
2+
var resolvePath = require('../');
3+
4+
describe('resolve-dependency-path', function() {
5+
it('resolves with absolute paths', function() {
6+
var resolved = resolvePath('./bar', __dirname + '/foo.js', __dirname);
7+
assert(resolved.indexOf(__dirname) === 0);
8+
});
9+
10+
it('resolves relative paths', function() {
11+
var resolved = resolvePath('./bar', __dirname + '/foo.js', __dirname);
12+
13+
assert(resolved === __dirname + '/bar.js');
14+
});
15+
16+
it('resolves non-relative paths', function() {
17+
var filename = __dirname + '/feature1/foo.js';
18+
var resolved = resolvePath('feature2/bar', filename, __dirname);
19+
assert(resolved === __dirname + '/feature2/bar.js');
20+
});
21+
22+
it('throws if the dependency path is missing', function() {
23+
assert.throws(function() {
24+
resolvePath();
25+
});
26+
});
27+
28+
it('throws if the filename is missing', function() {
29+
assert.throws(function() {
30+
resolvePath('./bar');
31+
});
32+
});
33+
34+
it('throws if the directory is missing', function() {
35+
assert.throws(function() {
36+
resolvePath('./bar', __dirname + '/foo.js');
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)