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 (183B to 210B) and [fast](#benchmarks) utility to ascend parent directories
|
4 |
|
5 | With [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 |
|
7 | 1) the callback returns a truthy value
|
8 | 2) `escalade` has reached the system root directory (eg, `/`)
|
9 |
|
10 | > **Important:**<br>Please note that `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 |
|
21 | There are two "versions" of `escalade` available:
|
22 |
|
23 | #### "async"
|
24 | > **Node.js:** >= 8.x<br>
|
25 | > **Size (gzip):** 210 bytes<br>
|
26 | > **Availability:** [CommonJS](https://unpkg.com/escalade/dist/index.js), [ES Module](https://unpkg.com/escalade/dist/index.mjs)
|
27 |
|
28 | This 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):** 183 bytes<br>
|
33 | > **Availability:** [CommonJS](https://unpkg.com/escalade/sync/index.js), [ES Module](https://unpkg.com/escalade/sync/index.mjs)
|
34 |
|
35 | This is the opt-in mode, ideal for scenarios where `async` usage cannot be supported.
|
36 |
|
37 |
|
38 | ## Usage
|
39 |
|
40 | ***Example Structure***
|
41 |
|
42 | ```
|
43 | /Users/lukeed
|
44 | └── oss
|
45 | ├── license
|
46 | └── escalade
|
47 | ├── package.json
|
48 | └── test
|
49 | └── fixtures
|
50 | ├── index.js
|
51 | └── foobar
|
52 | └── demo.js
|
53 | ```
|
54 |
|
55 | ***Example Usage***
|
56 |
|
57 | ```js
|
58 | //~> demo.js
|
59 | import { join } from 'path';
|
60 | import escalade from 'escalade';
|
61 |
|
62 | const input = join(__dirname, 'demo.js');
|
63 | // or: const input = __dirname;
|
64 |
|
65 | const pkg = await escalade(input, (dir, names) => {
|
66 | console.log('~> dir:', dir);
|
67 | console.log('~> names:', names);
|
68 | console.log('---');
|
69 |
|
70 | if (names.includes('package.json')) {
|
71 | // will be resolved into absolute
|
72 | return 'package.json';
|
73 | }
|
74 | });
|
75 |
|
76 | //~> dir: /Users/lukeed/oss/escalade/test/fixtures/foobar
|
77 | //~> names: ['demo.js']
|
78 | //---
|
79 | //~> dir: /Users/lukeed/oss/escalade/test/fixtures
|
80 | //~> names: ['index.js', 'foobar']
|
81 | //---
|
82 | //~> dir: /Users/lukeed/oss/escalade/test
|
83 | //~> names: ['fixtures']
|
84 | //---
|
85 | //~> dir: /Users/lukeed/oss/escalade
|
86 | //~> names: ['package.json', 'test']
|
87 | //---
|
88 |
|
89 | console.log(pkg);
|
90 | //=> /Users/lukeed/oss/escalade/package.json
|
91 |
|
92 | // Now search for "missing123.txt"
|
93 | // (Assume it doesn't exist anywhere!)
|
94 | const missing = await escalade(input, (dir, names) => {
|
95 | console.log('~> dir:', dir);
|
96 | return names.includes('missing123.txt') && 'missing123.txt';
|
97 | });
|
98 |
|
99 | //~> dir: /Users/lukeed/oss/escalade/test/fixtures/foobar
|
100 | //~> dir: /Users/lukeed/oss/escalade/test/fixtures
|
101 | //~> dir: /Users/lukeed/oss/escalade/test
|
102 | //~> dir: /Users/lukeed/oss/escalade
|
103 | //~> dir: /Users/lukeed/oss
|
104 | //~> dir: /Users/lukeed
|
105 | //~> dir: /Users
|
106 | //~> dir: /
|
107 |
|
108 | console.log(missing);
|
109 | //=> undefined
|
110 | ```
|
111 |
|
112 | > **Note:** To run the above example with "sync" mode, import from `escalade/sync` and remove the `await` keyword.
|
113 |
|
114 |
|
115 | ## API
|
116 |
|
117 | ### escalade(input, callback)
|
118 | Returns: `string|void` or `Promise<string|void>`
|
119 |
|
120 | When your `callback` locates a file, `escalade` will resolve/return with an absolute path.<br>
|
121 | If your `callback` was never satisfied, then `escalade` will resolve/return with nothing (undefined).
|
122 |
|
123 | > **Important:**<br>The `sync` and `async` versions share the same API.<br>The **only** difference is that `sync` is not Promise-based.
|
124 |
|
125 | #### input
|
126 | Type: `string`
|
127 |
|
128 | The path from which to start ascending.
|
129 |
|
130 | This may be a file or a directory path.<br>However, when `input` is a file, `escalade` will begin with its parent directory.
|
131 |
|
132 | > **Important:** Unless given an absolute path, `input` will be resolved from `process.cwd()` location.
|
133 |
|
134 | #### callback
|
135 | Type: `Function`
|
136 |
|
137 | The callback to execute for each ancestry level. It always is given two arguments:
|
138 |
|
139 | 1) `dir` - an absolute path of the current parent directory
|
140 | 2) `names` - a list (`string[]`) of contents _relative to_ the `dir` parent
|
141 |
|
142 | > **Note:** The `names` list can contain names of files _and_ directories.
|
143 |
|
144 | When your callback returns a _falsey_ value, then `escalade` will continue with `dir`'s parent directory, re-invoking your callback with new argument values.
|
145 |
|
146 | When your callback returns a string, then `escalade` stops iteration immediately.<br>
|
147 | If 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.
|
148 |
|
149 | > **Important:** Your `callback` can be a `Promise/AsyncFunction` when using the "async" version of `escalade`.
|
150 |
|
151 | ## Benchmarks
|
152 |
|
153 | > Running on Node.js v10.13.0
|
154 |
|
155 | ```
|
156 | # Load Time
|
157 | find-up 3.891ms
|
158 | escalade 0.485ms
|
159 | escalade/sync 0.309ms
|
160 |
|
161 | # Levels: 6 (target = "foo.txt"):
|
162 | find-up x 24,856 ops/sec ±6.46% (55 runs sampled)
|
163 | escalade x 73,084 ops/sec ±4.23% (73 runs sampled)
|
164 | find-up.sync x 3,663 ops/sec ±1.12% (83 runs sampled)
|
165 | escalade/sync x 9,360 ops/sec ±0.62% (88 runs sampled)
|
166 |
|
167 | # Levels: 12 (target = "package.json"):
|
168 | find-up x 29,300 ops/sec ±10.68% (70 runs sampled)
|
169 | escalade x 73,685 ops/sec ± 5.66% (66 runs sampled)
|
170 | find-up.sync x 1,707 ops/sec ± 0.58% (91 runs sampled)
|
171 | escalade/sync x 4,667 ops/sec ± 0.68% (94 runs sampled)
|
172 |
|
173 | # Levels: 18 (target = "missing123.txt"):
|
174 | find-up x 21,818 ops/sec ±17.37% (14 runs sampled)
|
175 | escalade x 67,101 ops/sec ±21.60% (20 runs sampled)
|
176 | find-up.sync x 1,037 ops/sec ± 2.86% (88 runs sampled)
|
177 | escalade/sync x 1,248 ops/sec ± 0.50% (93 runs sampled)
|
178 | ```
|
179 |
|
180 |
|
181 | ## Related
|
182 |
|
183 | - [premove](https://github.com/lukeed/premove) - A tiny (247B) utility to remove items recursively
|
184 | - [totalist](https://github.com/lukeed/totalist) - A tiny (195B to 224B) utility to recursively list all (total) files in a directory
|
185 | - [mk-dirs](https://github.com/lukeed/mk-dirs) - A tiny (420B) utility to make a directory and its parents, recursively
|
186 |
|
187 | ## License
|
188 |
|
189 | MIT © [Luke Edwards](https://lukeed.com)
|