npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

up-the-tree

v0.3.2

Published

Navigate up - up - up the tree - till found what we're looking for

Downloads

11

Readme

up-the-tree!

Come away, Hobbits. We climb. We must climb. Up, up, up the tree we go.

Super fast: (go to quickstart)
Get the path of a file or folder that is somewhere up the folder tree. Create a Twig to call require from up there, for easy path resolving.

Slightly more detailed:
Navigate up-the-tree from any path until any condition is met.

The condition is evaluated from the starting path. If the condition is not met it will go up a folder, until the condition is met or it runs out of folders.

Installation

$ npm install up-the-tree

Usage

Quickstart

var $upTheTree = require('up-the-tree');

// basic usage, see docs for condition and options
$upTheTree(condition, options);

// find folder containing package.json
$upTheTree('package.json');

// find path and include package.json in the result
$upTheTree.resolve('package.json');

// create a Twig at my project root
var rootTwig = $upTheTree('package.json', { twig: true });

// load a file relative to my project root
var someLib = rootTwig.require('./lib/some');

// what is the parent folder of my project root?
rootTwig.dirname()

// how far away is the Twig path from __dirname?
rootTwig.relative(__dirname)

// how far away is it from the current working dir?
rootTwig.relative('.')

Condition string, function

The condition is evaluated for every folder in the path, until matched.

If the condition is a string it is assumed to be a file, folder, or path, and is wrapped in a function using return $fs.existsSync($path.join(path, condition)).

If the condition is a function it will be called for every folder in options.start until the function returns true.

If you're looking for a folder called booger in the path /User/me/some/path, it will be checked against the paths: /User/me/some/path, /User/me/some, /User/me, /User, and /, until returning false.

If you are looking for a folder called some in the path /User/me/some/path, it will be checked against the paths: /User/me/some/path, /User/me/some, /User/me and will return /User/me as that is the path that contains the folder some.

Finding a file or folder

This could be a file or folder name, or part of a path. It will be compared using path.join(currentPath, condition), where currentPath equals the directory that is being evaluated.

Examples

var $upTheTree = require('up-the-tree');

var pathContainingFile = $upTheTree('package.json');

var pathContainingFolder = $upTheTree('node_modules');

var pathContainingPath = $upTheTree('node_modules/extend');

var nodeModulesRootPath = $upTheTree.resolve('node_modules');

Custom condition

You may provide a function as condition to perform a custom check on the path. The function should return true or false to indicate success on the path. Returning true will return the currentPath to the calling function.

var $upTheTree = require('up-the-tree');

var pathWithIndexOf = $upTheTree(function (currentPath) {
	return currentPath.indexOf('node_modules') > -1;
});

Options object, optional

options.start (default: '.')

Deepest path to look in. This indicates the starting point, it will traverse up the folder tree until the condition is met, or it runs out of folders, or until it hits options.end.

options.end (default: '/')

Undeepest path to look in. This indicates how far up the tree it should go. It will look in this folder last.

Note: options.start should be within options.end.

options.resolve (default: false)

Performs a path.resolve on found path and searched path. Note that the condition must be a string (path), otherwise it cannot be resolved.

options.twig (default: false)

Creates a twig on the returned path.

Twig (class)

Now that's awesome, lock that resulting path in place and perform tricks with it.

var $upTheTree = require('up-the-tree');

var twigFromConfig = $upTheTree('package.json', { twig: true });

var manualTwig = new $upTheTree.Twig('./node_modules');

Twig.toString()

Returns the current path as string. Will sometime be called automatically when you attempt to use the instance as a string.

Twig.require(path)

Calls require relative from Twig path

Twig.move(path)

Changes the current path of the Twig to the path. Uses $path.resolve.

Twig.resolve(path)

Returns $path.resolve(Twig.toString(), path)

Twig.relative(path)

Returns $path.relative(Twig.toString(), path)

Twig.dirname()

Returns $path.dirname(Twig.toString())

Twig.parse()

Returns $path.parse(Twig.toString())

NOTE: Only present for version Node v0.12.x