Skip to content

Commit

Permalink
Adding the npmbin utility to search for binaries in node_modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
alinex committed May 8, 2014
1 parent 30eebfe commit b3869a4
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 7 deletions.
61 changes: 54 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,53 @@ __Example:__
}


### npmbin

Find the defined binary in node_modules directories. It will search in the current
and upper package directories.

__Arguments:__

* `bin`
name of the binary to search for
* `dir`
Module directory to start search from.
* `callback(err, cmd)`
The callback will be called just if an error occurred or after finished.
The command path to be called.

__Example:__

var fs = require('alinex-fs');
fs.npmbin('coffee', function(err, cmd) {
console.log("Coffee command found at:"+cmd);
});


### npmbinSync

Find the defined binary in node_modules directories. It will search in the current
and upper package directories.

__Arguments:__

* `bin`
name of the binary to search for
* `dir`
Module directory to start search from.

__Return:__

* `cmd`
the command path to be called.

__Example:__

var fs = require('alinex-fs');
cmd = fs.npmbin('coffee');
console.log("Coffee command found at:"+cmd);


Filter
-------------------------------------------------

Expand All @@ -295,11 +342,11 @@ Additionally some methods may have special options for filtering.
- `user` owner name or id
- `group` owner group name or id

- `accessedAfter`
- `accessedAfter`
- `accessedBefore`
- `modifiedAfter`
- `modifiedAfter`
- `modifiedBefore`
- `createdAfter`
- `createdAfter`
- `createdBefore`

If you use multiple options all of them have to match the file to be valid.
Expand All @@ -310,8 +357,8 @@ See the details below.

This is based on glob expressions like used in unix systems. You may use these
as the `include` or `exclude` pattern while the `exclude` pattern has the higher
priority. All files are matched which are in the include pattern and not in the
exclude pattern.
priority. All files are matched which are in the include pattern and not in the
exclude pattern.

The pattern may be a regular expression or a glob pattern string with
the following specification:
Expand Down Expand Up @@ -372,7 +419,7 @@ size of the matching files in bytes:

### Owner and Group

You may also specify files based on the user which owns the files or the group
You may also specify files based on the user which owns the files or the group
of the files.

Both may be specified as id (uid or gid) or using the alias name.
Expand All @@ -381,7 +428,7 @@ Both may be specified as id (uid or gid) or using the alias name.
### Time specification

It is also possible to select files based on their `creation`, last `modified`
or last `accessed` time.
or last `accessed` time.

Specify the `Before` and `After` time as:

Expand Down
65 changes: 65 additions & 0 deletions src/npmbin.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Extension of nodes fs utils
# =================================================

# Node Modules
# -------------------------------------------------

# include base modules
fs = require 'fs'
path = require 'path'
async = require 'async'


# Find binary in node_modules or parent
# -------------------------------------------------
#
# __Arguments:__
#
# * `bin`
# name of the binary to search for
# * `dir`
# Module directory to start search from.
# * `callback(err, file)`
# The callback will be called just if an error occurred or after finished.
# The file is the path to the binary if found.
npmbin = module.exports.async = (bin, dir, cb) ->
unless cb
cb = dir
dir = path.dirname __dirname
file = path.join dir, 'node_modules', '.bin', bin
# search for file
fs.exists file, (exists) ->
return cb null, file if exists
# find in parent
parent = path.join dir, '..', '..'
if parent is dir
return cb "Could not find #{bin} program."
npmbin bin, parent, cb

# Find binary in node_modules or parent(synchronous)
# -------------------------------------------------
#
# __Arguments:__
#
# * `bin`
# name of the binary to search for
# * `dir`
# Module directory to start search from.
#
# __Return:__
#
# * `callback(err, file)`
# The callback will be called just if an error occurred or after finished.
# The file is the path to the binary if found.
npmbinSync = module.exports.sync = (bin, dir) ->
unless cb
cb = dir
dir = path.dirname __dirname
file = path.join dir, 'node_modules', '.bin', bin
# search for file
return file if fs.exists file
# find in parent
parent = path.join dir, '..', '..'
if parent is dir
return cb "Could not find #{bin} program."
npmbinSync bin, parent

0 comments on commit b3869a4

Please sign in to comment.