Fork me on GitHub

hike

Build Status NPM version

Inspired by Hike (Ruby) - a library for finding files in a set of paths. It's done specially for mincer to simplify maintenance and not recommended for direct use in other projects. Please remember, we don't accept feature requests not related to mincer improvments.

See API docs for details on methods.

Examples

Find JavaScript files in this project:

trail = new Hike("/home/ixti/Projects/hike-js");
trail.appendExtensions([".js"]);
trail.appendPaths(["lib", "test"]);

trail.find("hike");
# => "/home/ixti/Projects/hike-js/lib/hike.js"

trail.find("test_hike");
# => "/home/ixti/Projects/hike-js/test/test_hike.rb"

Explore your shell path:

trail = new Hike("/");
trail.appendPaths(process.env.PATH.split(":"));

trail.find("ls");
# => "/bin/ls"

trail.find("gem");
# => "/home/ixti/.rvm/rubies/ruby-1.9.2-p290/bin/gem"

Installation

$ npm install hike

License

Copyright (c) 2014 Vitaly Puzrin, Aleksey V Zapparov

Released under the MIT license. See LICENSE for details.

class

Cached

internal

Description

Cached variant of Hike. It assumes the file system does not change between find calls. All stat and entries calls are cached for the lifetime of the Cached object.

Instance methods

Instance properties

instance method

Cached#entries

    • Cached#entries(pathname)
      • Array
    • pathname
      • String
    • Pathname to get files list for.

See Hike#entries for details.

instance method

Cached#find

    • Cached#find(logicalPaths[, options][, callback])
      • String
      • undefined
    • logicalPaths
      • String
      • Array
    • One or many (fallbacks) logical paths.

    • options
      • Object
    • Options hash. See description below.

    • callback
      • Function
    • Function to execute on each matching path. See description below.

See Hike#find for details.

instance method

Cached#stat

    • Cached#stat(pathname)
      • fs.Stats
      • undefined
    • pathname
      • String
    • Pathname to get stats for.

See Hike#stat for details.

instance property

Cached#extensions

    • Cached#extensions
      • Array

Returns array of registered lookup extensions.

instance property

Cached#paths

    • Cached#paths
      • Array

Returns array of registered lookup paths.

instance property

Cached#root

    • Cached#root
      • String

Returns hike's root.

class

Hike

Description

Public container class for holding paths and extensions.

Constructor

Instance properties

constructor

Hike.new

    • new Hike(root = ".")
    • root
      • String
    • root path

A Hike accepts an optional root path that defaults to your current working directory. Any relative paths added to Hike#paths will be expanded relatively to the root.

instance method

Hike#appendExtensions

    • Hike#appendExtensions(extensions)
      • void
    • extensions
      • Array
    • extensions to append

Appends extensions to the list of lookup extensions.

instance method

Hike#appendPaths

    • Hike#appendPaths(pathnames)
      • void
    • pathnames
      • Array
    • paths to append

Appends pathnames to the list of lookup paths.

instance method

Hike#entries

    • Hike#entries(pathname)
      • Array
    • pathname
      • String
    • Pathname to get files list for.

ENOENT-aware wraper over fs.readdirSync that filters out . files and ~ swap files. Returns an empty Array if the directory does not exist.

instance method

Hike#find

    • Hike#find(logicalPaths[, options][, callback])
      • String
      • undefined
    • logicalPaths
      • String
      • Array
    • One or many (fallbacks) logical paths.

    • options
      • Object
    • Options hash. See description below.

    • callback
      • Function
    • Function to execute on each matching path. See description below.

Returns the expanded path for a logical path in the path collection.

trail = new Hike("/home/ixti/Projects/hike-js");

trail.appendExtensions([".js"]);
trail.appendPaths(["lib", "test"]);

trail.find("hike");
// -> "/home/ixti/Projects/hike-js/lib/hike.js"

trail.find("test_hike");
// -> "/home/ixti/Projects/hike-js/test/test_hike.js"

find accepts multiple fallback logical paths that returns the first match.

trail.find(["hike", "hike/index"]);

is equivalent to

trail.find("hike") || trail.find("hike/index");

Though find always returns the first match, it is possible to iterate over all shadowed matches and fallbacks by supplying a callback:

trail.find(["hike", "hike/index"], function (pathname) {
  console.warn(pathname);
});

This allows you to filter your matches by any condition.

trail.find("application", function (pathname) {
  if ("text/css" == mime_type_for(pathname)) {
    // stop iteration and return matched pathname
    return true;
  }
});

Options

  • basePath (String): You can specify "alternative" base path to be used upon searching. Default: Hike#root.

Callback

Iterator that is called on each matching pathname.

It receives matching pathname and must return true-ish value in order to stop iteration and return given pathname as result of search.

Default:
function (pathname) { return true; }
instance method

Hike#prependExtensions

    • Hike#prependExtensions(extensions)
      • void
    • extensions
      • Array
    • extensions to prepend

Prepends extensions to the list of lookup extensions.

instance method

Hike#prependPaths

    • Hike#prependPaths(pathnames)
      • void
    • pathnames
      • Array
    • paths to prepend

Prepends pathnames to the list of lookup paths.

instance method

Hike#removeExtension

    • Hike#removeExtension([extensions])
      • Array
    • extensions
      • Array
    • extensions to remove

Removes extensions from the list of lookup extensions. If no extensions given (or an empty array), then all registered lookup extensions are removed.

Returns list of removed extensions.

instance method

Hike#removePath

    • Hike#removePath([pathnames])
      • Array
    • pathnames
      • Array
    • paths to remove

Removes pathnames from the list of lookup paths. If no pathnames given (or an empty array), then all registered lookup paths are removed.

Returns list of removed pathnames.

instance method

Hike#stat

    • Hike#stat(pathname)
      • fs.Stats
      • undefined
    • pathname
      • String
    • Pathname to get stats for.

ENOENT-aware wraper over fs.statSync. Returns undefined if file does not exists.

instance property

Hike#cached

A Cached is a cached Hike object that caches FS ops. If you are confident that you are not making changes the paths you are searching, cached version will avoid excess system calls.

var cached = trail.cached;

cached.find("hike/trail"); // hits FS
cached.find("hike/trail"); // returns cached result
cached.find("test_trail"); // hits FS
instance property

Hike#extensions

    • Hike#extensions
      • Array

Returns array of registered lookup extensions.

instance property

Hike#paths

    • Hike#paths
      • Array

Returns array of registered lookup paths.

instance property

Hike#root

    • Hike#root
      • String

Returns hike's root.