Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

resolve-file

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

resolve-file - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

utils.js

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc