resolve-file
Advanced tools
Comparing version 0.1.0 to 0.2.0
124
index.js
@@ -10,7 +10,4 @@ /*! | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var expandTilde = require('expand-tilde'); | ||
var resolve = require('resolve'); | ||
var dir = require('cwd'); | ||
var utils = require('./utils'); | ||
@@ -27,3 +24,3 @@ /** | ||
* ```js | ||
* var fp = resolveFile('./index.js') | ||
* var fp = resolve('./index.js') | ||
* //=> /path/to/resolve-file/index.js | ||
@@ -34,44 +31,107 @@ * ``` | ||
* @param {Object} `options` Additional options to specify `cwd` | ||
* @return {String} Resolve `filepath` if found | ||
* @return {String} Resolved `filepath` if found | ||
* @api public | ||
*/ | ||
function resolveFile(name, options) { | ||
options = options || {}; | ||
var cwd = dir(options.cwd || process.cwd()); | ||
var first = name.charAt(0); | ||
if (first === '.') { | ||
return path.resolve(cwd, name); | ||
} | ||
function resolve(name, options) { | ||
var file = resolve.file(name, options); | ||
return file && file.path; | ||
} | ||
if (first === '/') { | ||
return path.resolve(name); | ||
/** | ||
* Resolve the path to a file located in one of the following places: | ||
* | ||
* - local to the current project (`'./index.js'`) | ||
* - absolute (`'/usr/something.rc'`) | ||
* - node module "main" file (`'cwd'`) | ||
* - specific file inside a node module (`'cwd/LICENSE'`) | ||
* - file located in user's home directory (`'~/.npmrc'`) | ||
* | ||
* ```js | ||
* var file = resolve.file('./index.js') | ||
* //=> { | ||
* //=> cwd: '/path/to/resolve-file', | ||
* //=> path: '/path/to/resolve-file/index.js' | ||
* //=> } | ||
* ``` | ||
* | ||
* @param {String} `name` Filename to resolve | ||
* @param {Object} `options` Additional options to specify `cwd` | ||
* @return {Object} File object with resolved `path` if found. | ||
* @api public | ||
*/ | ||
resolve.file = function(name, options) { | ||
var file = {}; | ||
if (name && typeof name === 'object' && name.path) { | ||
file = name; | ||
name = file.path; | ||
} | ||
if (first === '~') { | ||
return expandTilde(name); | ||
if (typeof options === 'function') { | ||
options = { resolve: options }; | ||
} | ||
try { | ||
var file, i; | ||
if ((i = name.indexOf('/')) !== -1) { | ||
var fp = resolve.sync(name.slice(0, i)); | ||
var rest = path.normalize(name.slice(i + 1)); | ||
var res = path.resolve(path.dirname(fp), rest); | ||
if (fs.existsSync(res)) { | ||
return res; | ||
var opts = utils.extend({cwd: process.cwd()}, options); | ||
var first = name.charAt(0); | ||
file.cwd = opts.cwd; | ||
if (name.indexOf('npm:') === 0) { | ||
try { | ||
file.path = utils.resolve.sync(path.resolve(file.cwd, name.slice(4))); | ||
file.cwd = path.dirname(file.path); | ||
} catch (err) { | ||
throw new Error(`cannot resolve: '${name}'`); | ||
} | ||
} else { | ||
switch (first) { | ||
case '~': | ||
file.cwd = utils.home(); | ||
file.path = utils.expandTilde(name); | ||
break; | ||
case '@': | ||
file.path = path.resolve(file.cwd, name.slice(2)); | ||
file.cwd = path.dirname(file.cwd); | ||
break; | ||
case '.': | ||
default: { | ||
file.path = path.resolve(file.cwd, name); | ||
break; | ||
} | ||
} | ||
return resolve.sync(name); | ||
} catch(err) {}; | ||
} | ||
var fp = path.resolve(cwd, name); | ||
return fs.existsSync(fp) ? fp : null; | ||
} | ||
if (!utils.exists(file.path)) { | ||
try { | ||
if (/[\\\/]/.test(name)) { | ||
file.basename = path.basename(name); | ||
var dirname = path.dirname(name); | ||
file.name = path.basename(dirname); | ||
file.main = require.resolve(dirname); | ||
file.path = path.resolve(path.dirname(file.main), file.basename); | ||
} | ||
if (!utils.exists(file.path)) { | ||
file.path = utils.resolve.sync(name); | ||
file.main = file.path; | ||
} | ||
} catch (err) { | ||
if (err.code !== 'MODULE_NOT_FOUND' && !/Cannot find module/.test(err.message)) { | ||
throw err; | ||
} | ||
}; | ||
} | ||
if (utils.exists(file.path)) { | ||
return utils.decorate(file, opts.resolve); | ||
} | ||
return null; | ||
}; | ||
/** | ||
* Export `resolveFile` | ||
* Export `resolve` | ||
* @type {Function} | ||
*/ | ||
module.exports = resolveFile; | ||
module.exports = resolve; |
{ | ||
"name": "resolve-file", | ||
"description": "Resolve an absolute file path from local directories, local node_modules or global node_modules.", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/doowb/resolve-file", | ||
@@ -13,3 +13,4 @@ "author": "Brian Woodward (https://github.com/doowb)", | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"utils.js" | ||
], | ||
@@ -23,10 +24,26 @@ "main": "index.js", | ||
}, | ||
"dependencies": { | ||
"cwd": "^0.10.0", | ||
"expand-tilde": "^1.2.2", | ||
"extend-shallow": "^2.0.1", | ||
"fs-exists-sync": "^0.1.0", | ||
"global-modules": "^0.2.2", | ||
"lazy-cache": "^2.0.1", | ||
"os-homedir": "^1.0.1", | ||
"resolve": "^1.1.7" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*" | ||
"gulp": "^3.9.1", | ||
"gulp-eslint": "^2.0.0", | ||
"gulp-format-md": "^0.1.9", | ||
"gulp-istanbul": "^0.10.4", | ||
"gulp-mocha": "^2.2.0", | ||
"gulp-unused": "^0.1.2", | ||
"mocha": "^2.5.3", | ||
"npm-install-global": "^0.1.0" | ||
}, | ||
"dependencies": { | ||
"cwd": "^0.7.0", | ||
"expand-tilde": "^1.2.0", | ||
"resolve": "^1.1.6" | ||
}, | ||
"keywords": [ | ||
"file", | ||
"resolve" | ||
], | ||
"verb": { | ||
@@ -36,8 +53,22 @@ "related": { | ||
"cwd", | ||
"expand-tilde", | ||
"look-up", | ||
"expand-tilde", | ||
"resolve" | ||
] | ||
} | ||
}, | ||
"toc": false, | ||
"layout": "default", | ||
"tasks": [ | ||
"readme" | ||
], | ||
"plugins": [ | ||
"gulp-format-md" | ||
], | ||
"lint": { | ||
"reflinks": true | ||
}, | ||
"reflinks": [ | ||
"verb" | ||
] | ||
} | ||
} |
@@ -1,9 +0,11 @@ | ||
# resolve-file [![NPM version](https://badge.fury.io/js/resolve-file.svg)](http://badge.fury.io/js/resolve-file) [![Build Status](https://travis-ci.org/doowb/resolve-file.svg)](https://travis-ci.org/doowb/resolve-file) | ||
# resolve-file [![NPM version](https://img.shields.io/npm/v/resolve-file.svg?style=flat)](https://www.npmjs.com/package/resolve-file) [![NPM downloads](https://img.shields.io/npm/dm/resolve-file.svg?style=flat)](https://npmjs.org/package/resolve-file) [![Build Status](https://img.shields.io/travis/doowb/resolve-file.svg?style=flat)](https://travis-ci.org/doowb/resolve-file) | ||
> Resolve an absolute file path from local directories, local node_modules or global node_modules. | ||
Resolve an absolute file path from local directories, local node_modules or global node_modules. | ||
Install with [npm](https://www.npmjs.com/) | ||
## Install | ||
Install with [npm](https://www.npmjs.com/): | ||
```sh | ||
$ npm i resolve-file --save | ||
$ npm install resolve-file --save | ||
``` | ||
@@ -19,3 +21,3 @@ | ||
### [resolveFile](index.js#L36) | ||
### [resolve](index.js#L33) | ||
@@ -34,3 +36,3 @@ Resolve the path to a file located in one of the following places: | ||
* `options` **{Object}**: Additional options to specify `cwd` | ||
* `returns` **{String}**: Resolve `filepath` if found | ||
* `returns` **{String}**: Resolved `filepath` if found | ||
@@ -40,13 +42,61 @@ **Example** | ||
```js | ||
var fp = resolveFile('./index.js') | ||
var fp = resolve('./index.js') | ||
//=> /path/to/resolve-file/index.js | ||
``` | ||
### [.file](index.js#L61) | ||
Resolve the path to a file located in one of the following places: | ||
* local to the current project (`'./index.js'`) | ||
* absolute (`'/usr/something.rc'`) | ||
* node module "main" file (`'cwd'`) | ||
* specific file inside a node module (`'cwd/LICENSE'`) | ||
* file located in user's home directory (`'~/.npmrc'`) | ||
**Params** | ||
* `name` **{String}**: Filename to resolve | ||
* `options` **{Object}**: Additional options to specify `cwd` | ||
* `returns` **{Object}**: File object with resolved `path` if found. | ||
**Example** | ||
```js | ||
var file = resolve.file('./index.js') | ||
//=> { | ||
//=> cwd: '/path/to/resolve-file', | ||
//=> path: '/path/to/resolve-file/index.js' | ||
//=> } | ||
``` | ||
## Related projects | ||
* [cwd](https://github.com/jonschlinkert/cwd): Easily get the CWD (current working directory) of a project based on package.json, optionally starting… [more](https://github.com/jonschlinkert/cwd) | ||
* [expand-tilde](https://github.com/jonschlinkert/expand-tilde): Bash-like tilde expansion for node.js. Expands a leading tilde in a file path to the… [more](https://github.com/jonschlinkert/expand-tilde) | ||
* [look-up](https://github.com/jonschlinkert/look-up): Like findup-sync and supports the same features but 20x-40x faster on avg. | ||
* [resolve](https://github.com/substack/node-resolve): resolve like require.resolve() on behalf of files asynchronously and synchronously | ||
You might also be interested in these projects: | ||
* [cwd](https://www.npmjs.com/package/cwd): Easily get the CWD (current working directory) of a project based on package.json, optionally starting… [more](https://github.com/jonschlinkert/cwd) | [homepage](https://github.com/jonschlinkert/cwd "Easily get the CWD (current working directory) of a project based on package.json, optionally starting from a given path. (Node.js/javascript util)") | ||
* [expand-tilde](https://www.npmjs.com/package/expand-tilde): Bash-like tilde expansion for node.js. Expands a leading tilde in a file path to the… [more](https://github.com/jonschlinkert/expand-tilde) | [homepage](https://github.com/jonschlinkert/expand-tilde "Bash-like tilde expansion for node.js. Expands a leading tilde in a file path to the user home directory, or `~+` to the cwd.") | ||
* [look-up](https://www.npmjs.com/package/look-up): Faster drop-in replacement for find-up and findup-sync. | [homepage](https://github.com/jonschlinkert/look-up "Faster drop-in replacement for find-up and findup-sync.") | ||
* [resolve](https://www.npmjs.com/package/resolve): resolve like require.resolve() on behalf of files asynchronously and synchronously | [homepage](https://github.com/substack/node-resolve#readme "resolve like require.resolve() on behalf of files asynchronously and synchronously") | ||
## Contributing | ||
This document was generated by [verb](https://github.com/verbose/verb), please don't edit directly. Any changes to the readme must be made in [.verb.md](.verb.md). See [Building Docs](#building-docs). | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/doowb/resolve-file/issues/new). | ||
## Building docs | ||
Generate readme and API documentation with [verb](https://github.com/verbose/verb): | ||
```sh | ||
$ npm install -g verb verb-readme-generator && verb | ||
``` | ||
Or, if [verb](https://github.com/verbose/verb) is installed globally: | ||
```sh | ||
$ verb | ||
``` | ||
## Running tests | ||
@@ -57,9 +107,5 @@ | ||
```sh | ||
$ npm i -d && npm test | ||
$ npm install -d && npm test | ||
``` | ||
## Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/doowb/resolve-file/issues/new) | ||
## Author | ||
@@ -69,12 +115,12 @@ | ||
+ [github/doowb](https://github.com/doowb) | ||
+ [twitter/doowb](http://twitter.com/doowb) | ||
* [github/doowb](https://github.com/doowb) | ||
* [twitter/doowb](http://twitter.com/doowb) | ||
## License | ||
Copyright © 2015 Brian Woodward | ||
Released under the MIT license. | ||
Copyright © 2016, [Brian Woodward](https://github.com/doowb). | ||
Released under the [MIT license](https://github.com/doowb/resolve-file/blob/master/LICENSE). | ||
*** | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on August 01, 2015._ | ||
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on June 06, 2016._ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
11594
5
177
121
8
8
5
1
+ Addedextend-shallow@^2.0.1
+ Addedfs-exists-sync@^0.1.0
+ Addedglobal-modules@^0.2.2
+ Addedlazy-cache@^2.0.1
+ Addedos-homedir@^1.0.1
+ Addedcwd@0.10.0(transitive)
+ Addedextend-shallow@2.0.1(transitive)
+ Addedfind-file-up@0.1.3(transitive)
+ Addedfind-pkg@0.1.2(transitive)
+ Addedfs-exists-sync@0.1.0(transitive)
+ Addedglobal-modules@0.2.3(transitive)
+ Addedglobal-prefix@0.1.5(transitive)
+ Addedhomedir-polyfill@1.0.3(transitive)
+ Addedini@1.3.8(transitive)
+ Addedis-windows@0.2.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedlazy-cache@2.0.2(transitive)
+ Addedparse-passwd@1.0.0(transitive)
+ Addedresolve-dir@0.1.1(transitive)
+ Addedset-getter@0.1.1(transitive)
+ Addedto-object-path@0.3.0(transitive)
+ Addedwhich@1.3.1(transitive)
- Removedarr-diff@2.0.0(transitive)
- Removedarr-flatten@1.1.0(transitive)
- Removedarray-unique@0.2.1(transitive)
- Removedbraces@1.8.5(transitive)
- Removedcwd@0.7.0(transitive)
- Removedexpand-brackets@0.1.5(transitive)
- Removedexpand-range@1.8.2(transitive)
- Removedextglob@0.3.2(transitive)
- Removedfilename-regex@2.0.1(transitive)
- Removedfill-range@2.2.4(transitive)
- Removedfor-in@1.0.2(transitive)
- Removedfor-own@0.1.5(transitive)
- Removedglob-base@0.3.0(transitive)
- Removedglob-parent@2.0.0(transitive)
- Removedis-dotfile@1.0.3(transitive)
- Removedis-equal-shallow@0.1.3(transitive)
- Removedis-extglob@1.0.0(transitive)
- Removedis-glob@2.0.1(transitive)
- Removedis-number@2.1.04.0.0(transitive)
- Removedis-posix-bracket@0.1.1(transitive)
- Removedis-primitive@2.0.0(transitive)
- Removedisarray@1.0.0(transitive)
- Removedisobject@2.1.0(transitive)
- Removedkind-of@6.0.3(transitive)
- Removedlook-up@0.7.2(transitive)
- Removedmath-random@1.0.4(transitive)
- Removedmicromatch@2.3.11(transitive)
- Removednormalize-path@2.1.1(transitive)
- Removedobject.omit@2.0.1(transitive)
- Removedparse-glob@3.0.4(transitive)
- Removedpreserve@0.2.0(transitive)
- Removedrandomatic@3.1.1(transitive)
- Removedregex-cache@0.4.4(transitive)
- Removedremove-trailing-separator@1.1.0(transitive)
- Removedrepeat-element@1.1.4(transitive)
- Removedrepeat-string@1.6.1(transitive)
Updatedcwd@^0.10.0
Updatedexpand-tilde@^1.2.2
Updatedresolve@^1.1.7