Fork me on GitHub

hike

Build Status

Javascript port of Hike (Ruby) - a library for finding files in a set of paths. Use it to implement search paths, load paths, and the like.

See API docs for details on methods.

Examples

Find JavaScript files in this project:

trail = new hike.Trail("/home/ixti/Projects/hike-js");
trail.extensions.append(".js");
trail.paths.append("lib", "test");

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

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

Explore your shell path:

trail = new hike.Trail("/");
trail.paths.append(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) 2012 Vitaly Puzrin

Released under the MIT license. See LICENSE for details.

class

Trail

Description

Public container class for holding paths and extensions.

Constructor

Instance methods

constructor

Trail.new

    • new Trail(root = ".")

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

instance method

Trail#entries

    • Trail#entries(pathname)
      • Array

Wrapper over entries using one-time instance of index.

instance method

Trail#find

    • Trail#find(logical_paths[, options][, fn])
      • String
    • logical_paths
      • String
      • Array
    • One or many (fallbacks) logical paths.

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

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

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

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

trail.extensions.append(".js");
trail.paths.append("lib", "test");

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

trail.find("test_trail");
// -> "/home/ixti/Projects/hike/test/test_trail.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 block function (fn).

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

This allows you to filter your matches by any condition.

trail.find("application", function (path) {
  if ("text/css" == mime_type_for(path)) {
    return path;
  }
});
Options
  • basePath (String): You can specify "alternative" base path to be used upon searching. Default: root.
Block function

Some kind of iterator that is called on each matching pathname. Once this function returns anything but undefned - iteration is stopped and the value of this function returned.

Default:

function (path) { return path; }
instance method

Trail#stat

    • Trail#stat(pathname)
      • Stats
      • Null

Wrapper over stat using one-time instance of index.

instance property

Trail#aliases

Mutable mapping of an extension aliases.

trail = new Trail();
trail.paths.append("/home/ixti/Projects/hike/site");
trail.aliases.append('html', ['.htm', '.xhtml', '.php']);

Aliases provide a fallback when the primary extension is not matched. In the example above, a lookup for "foo.html" will check for the existence of "foo.htm", "foo.xhtml", or "foo.php".

instance property

Trail#extensions

Mutable Extensions collection.

trail = new Trail();
trail.paths.append("/home/ixti/Projects/hike/lib");
trail.extensions.append(".rb");

Extensions allow you to find files by just their name omitting their extension. Is similar to Ruby's require mechanism that allows you to require files with specifiying foo.rb.

instance property

Trail#index

Returns an Index object that has the same interface as Trail. An Index is a cached Trail object that does not update when the file system changes. If you are confident that you are not making changes the paths you are searching, index will avoid excess system calls.

index = trail.index;
index.find("hike/trail");
index.find("test_trail");
instance property

Trail#paths

Mutable Paths collection.

trail = new Trail();
trail.paths.append("/home/ixti/Projects/hike/lib",
                   "/home/ixti/Projects/hike/test");

The order of the paths is significant. Paths in the beginning of the collection will be checked first. In the example above, /home/ixti/Projects/hike/lib/hike.rb would shadow the existent of /home/ixti/Projects/hike/test/hike.rb.

instance property

Trail#root

read-onlyinternal
    • Trail#root
      • String

Root path. This attribute is immutable.