UNPKG

6.45 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 (196B to 224B) 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 `$HOME` directory ([`os.homedir()`](https://nodejs.org/api/os.html#os_os_homedir))
9
10> **Important:**<br>Please note that `escalade` will never traverse _beyond_ the user's home 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):** 224 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):** 196 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 24,856 ops/sec ±6.46% (55 runs sampled)
161 escalade x 73,084 ops/sec ±4.23% (73 runs sampled)
162 find-up.sync x 3,663 ops/sec ±1.12% (83 runs sampled)
163 escalade/sync x 9,360 ops/sec ±0.62% (88 runs sampled)
164
165# Levels: 12 (target = "package.json"):
166 find-up x 27,024 ops/sec ±11.20% (68 runs sampled)
167 escalade x 72,625 ops/sec ±5.18% (79 runs sampled)
168 find-up.sync x 1,644 ops/sec ±1.16% (92 runs sampled)
169 escalade/sync x 4,556 ops/sec ±0.57% (94 runs sampled)
170
171# Levels: 16 (target = "missing123.txt"):
172 find-up x 29,964 ops/sec ±12.71% (76 runs sampled)
173 escalade x 72,445 ops/sec ±25.38% (29 runs sampled)
174 find-up.sync x 1,087 ops/sec ±0.57% (93 runs sampled)
175 escalade/sync x 2,342 ops/sec ±0.51% (94 runs sampled)
176```
177
178
179## Related
180
181- [premove](https://github.com/lukeed/premove) - A tiny (247B) utility to remove items recursively
182- [totalist](https://github.com/lukeed/totalist) - A tiny (195B to 224B) utility to recursively list all (total) files in a directory
183- [mk-dirs](https://github.com/lukeed/mk-dirs) - A tiny (420B) utility to make a directory and its parents, recursively
184
185## License
186
187MIT © [Luke Edwards](https://lukeed.com)