nearest-file-path

1.1.0 • Public • Published

nearest-file-path

Find a relative path in a directory, travelling up the ancestral chain until found.

Build Status codecov.io

NPM

Example

/*
  File system:
  /
    x
    /a
      y
      /b
 */
 
import nearest from 'nearest-file-path'
// Find a file
console.log(await nearest('x', '/a/b'))   // '/x'
// Find a relative path
console.log(await nearest('a/y', '/a/b')) // '/a/y'
// Find a directory
console.log(await nearest('b', '/a/b'))   // '/a/b'
// Find the unfindable
console.log(await nearest('c', '/a/b'))   // Error
 
import { nearestSync } from 'nearest-file-path'
// or the synchronous way
console.log(nearestSync('x', '/a/b')) // '/x'

nearest(subpath[, basepath])

subpath is a relative path to a file or directory.

basepath is an optional path to a nested directory from which to search upwards until the subpath is found. Defaults to the current working directory.

Asynchronous with promises for that async/await goodness. Resolves with the absolute path as a string. Otherwise throws an error.

nearestSync(subpath[, basepath])

Identical arguments as #nearestsubpath-basepath.

Synchronous for specific use-cases where performance is not crucial. Remember to use asynchronous code where possible.

Readme

Keywords

Package Sidebar

Install

npm i nearest-file-path

Weekly Downloads

1

Version

1.1.0

License

MIT

Last publish

Collaborators

  • seb