UNPKG

6.25 kBMarkdownView Raw
1# escalade [![CI](https://github.com/lukeed/escalade/workflows/CI/badge.svg)](https://github.com/lukeed/escalade/actions) [![codecov](https://badgen.now.sh/codecov/c/github/lukeed/escalade)](https://codecov.io/gh/lukeed/escalade)
2
3> A tiny (186B to 214B) and [fast](#benchmarks) utility to ascend parent directories
4
5With [escalade](https://en.wikipedia.org/wiki/Escalade), you can scale parent directories until you've found what you're looking for.<br>Given an input file or directory, `escalade` will continue executing your callback function until either:
6
71) the callback returns a truthy value
82) `escalade` has reached the root directory (`process.cwd()`)
9
10> **Important:**<br>Please note that `escalade` will never traverse _beyond_ the root directory.<br>Additionally, `escalade` only deals with direct ancestry – it will not dive into parents' sibling directories.
11
12## Install
13
14```
15$ npm install --save escalade
16```
17
18
19## Modes
20
21There are two "versions" of `escalade` available:
22
23#### "async"
24> **Node.js:** >= 8.x<br>
25> **Size (gzip):** 214 bytes<br>
26> **Availability:** [CommonJS](https://unpkg.com/escalade/dist/index.js), [ES Module](https://unpkg.com/escalade/dist/index.mjs)
27
28This is the primary/default mode. It makes use of `async`/`await` and [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original).
29
30#### "sync"
31> **Node.js:** >= 6.x<br>
32> **Size (gzip):** 186 bytes<br>
33> **Availability:** [CommonJS](https://unpkg.com/escalade/sync/index.js), [ES Module](https://unpkg.com/escalade/sync/index.mjs)
34
35This is the opt-in mode, ideal for scenarios where `async` usage cannot be supported.
36
37
38## Usage
39
40***Example Structure***
41
42> Assume `process.cwd()` is `/Users/lukeed/oss/escalade`
43
44```
45/Users/lukeed
46 └── oss
47 ├── license
48 └── escalade
49 ├── package.json
50 └── test
51 └── fixtures
52 ├── index.js
53 └── foobar
54 └── demo.js
55```
56
57***Example Usage***
58
59```js
60//~> demo.js
61import { join } from 'path';
62import escalade from 'escalade';
63
64const input = join(__dirname, 'demo.js');
65// or: const input = __dirname;
66
67const pkg = await escalade(input, (dir, names) => {
68 console.log('~> dir:', dir);
69 console.log('~> names:', names);
70 console.log('---');
71
72 if (names.includes('package.json')) {
73 // will be resolved into absolute
74 return 'package.json';
75 }
76});
77
78//~> dir: /Users/lukeed/oss/escalade/test/fixtures/foobar
79//~> names: ['demo.js']
80//---
81//~> dir: /Users/lukeed/oss/escalade/test/fixtures
82//~> names: ['index.js', 'foobar']
83//---
84//~> dir: /Users/lukeed/oss/escalade/test
85//~> names: ['fixtures']
86//---
87//~> dir: /Users/lukeed/oss/escalade
88//~> names: ['package.json', 'test']
89//---
90
91console.log(pkg);
92//=> /Users/lukeed/oss/escalade/package.json
93
94// Now search for "license"
95// ~> Will never leave "/Users/lukeed/oss/escalade" directory
96const license = await escalade(input, (dir, names) => {
97 console.log('~> dir:', dir);
98 return names.includes('license') && 'license';
99});
100
101//~> dir: /Users/lukeed/oss/escalade/test/fixtures/foobar
102//~> dir: /Users/lukeed/oss/escalade/test/fixtures
103//~> dir: /Users/lukeed/oss/escalade/test
104//~> dir: /Users/lukeed/oss/escalade
105
106console.log(license);
107//=> undefined
108```
109
110> **Note:** To run the above example with "sync" mode, import from `escalade/sync` and remove the `await` keyword.
111
112
113## API
114
115### escalade(input, callback)
116Returns: `string|void` or `Promise<string|void>`
117
118When your `callback` locates a file, `escalade` will resolve/return with an absolute path.<br>
119If your `callback` was never satisfied, then `escalade` will resolve/return with nothing (undefined).
120
121> **Important:**<br>The `sync` and `async` versions share the same API.<br>The **only** difference is that `sync` is not Promise-based.
122
123#### input
124Type: `string`
125
126The path from which to start ascending.
127
128This may be a file or a directory path.<br>However, when `input` is a file, `escalade` will begin with its parent directory.
129
130> **Important:** Unless given an absolute path, `input` will be resolved from `process.cwd()` location.
131
132#### callback
133Type: `Function`
134
135The callback to execute for each ancestry level. It always is given two arguments:
136
1371) `dir` - an absolute path of the current parent directory
1382) `names` - a list (`string[]`) of contents _relative to_ the `dir` parent
139
140> **Note:** The `names` list can contain names of files _and_ directories.
141
142When your callback returns a _falsey_ value, then `escalade` will continue with `dir`'s parent directory, re-invoking your callback with new argument values.
143
144When your callback returns a string, then `escalade` stops iteration immediately.<br>
145If the string is an absolute path, then it's left as is. Otherwise, the string is resolved into an absolute path _from_ the `dir` that housed the satisfying condition.
146
147> **Important:** Your `callback` can be a `Promise/AsyncFunction` when using the "async" version of `escalade`.
148
149## Benchmarks
150
151> Running on Node.js v10.13.0
152
153```
154# Load Time
155 find-up: 3.948ms
156 escalade: 0.493ms
157 escalade/sync: 0.327ms
158
159# Levels: 6 (target = "foo.txt"):
160 find-up x 30,614 ops/sec ±7.80% (46 runs sampled)
161 escalade x 46,814 ops/sec ±3.38% (74 runs sampled)
162 escalade/sync x 9,319 ops/sec ±0.53% (89 runs sampled)
163
164# Levels: 12 (target = "package.json"):
165 find-up x 26,421 ops/sec ±16.54% (32 runs sampled)
166 escalade x 49,618 ops/sec ±2.82% (80 runs sampled)
167 escalade/sync x 4,721 ops/sec ±0.51% (92 runs sampled)
168
169# Levels: 14 (target = "missing123.txt"):
170 find-up x 30,254 ops/sec ±15.38% (66 runs sampled)
171 escalade x 50,126 ops/sec ±2.45% (84 runs sampled)
172 escalade/sync x 4,173 ops/sec ±0.45% (93 runs sampled)
173```
174
175
176## Related
177
178- [premove](https://github.com/lukeed/premove) - A tiny (247B) utility to remove items recursively
179- [totalist](https://github.com/lukeed/totalist) - A tiny (195B to 224B) utility to recursively list all (total) files in a directory
180- [mk-dirs](https://github.com/lukeed/mk-dirs) - A tiny (420B) utility to make a directory and its parents, recursively
181
182## License
183
184MIT © [Luke Edwards](https://lukeed.com)