1 | # locate-path [![Build Status](https://travis-ci.com/sindresorhus/locate-path.svg?branch=master)](https://travis-ci.com/github/sindresorhus/locate-path)
|
---|
2 |
|
---|
3 | > Get the first path that exists on disk of multiple paths
|
---|
4 |
|
---|
5 | ## Install
|
---|
6 |
|
---|
7 | ```
|
---|
8 | $ npm install locate-path
|
---|
9 | ```
|
---|
10 |
|
---|
11 | ## Usage
|
---|
12 |
|
---|
13 | Here we find the first file that exists on disk, in array order.
|
---|
14 |
|
---|
15 | ```js
|
---|
16 | const locatePath = require('locate-path');
|
---|
17 |
|
---|
18 | const files = [
|
---|
19 | 'unicorn.png',
|
---|
20 | 'rainbow.png', // Only this one actually exists on disk
|
---|
21 | 'pony.png'
|
---|
22 | ];
|
---|
23 |
|
---|
24 | (async () => {
|
---|
25 | console(await locatePath(files));
|
---|
26 | //=> 'rainbow'
|
---|
27 | })();
|
---|
28 | ```
|
---|
29 |
|
---|
30 | ## API
|
---|
31 |
|
---|
32 | ### locatePath(paths, options?)
|
---|
33 |
|
---|
34 | Returns a `Promise<string>` for the first path that exists or `undefined` if none exists.
|
---|
35 |
|
---|
36 | #### paths
|
---|
37 |
|
---|
38 | Type: `Iterable<string>`
|
---|
39 |
|
---|
40 | Paths to check.
|
---|
41 |
|
---|
42 | #### options
|
---|
43 |
|
---|
44 | Type: `object`
|
---|
45 |
|
---|
46 | ##### concurrency
|
---|
47 |
|
---|
48 | Type: `number`\
|
---|
49 | Default: `Infinity`\
|
---|
50 | Minimum: `1`
|
---|
51 |
|
---|
52 | Number of concurrently pending promises.
|
---|
53 |
|
---|
54 | ##### preserveOrder
|
---|
55 |
|
---|
56 | Type: `boolean`\
|
---|
57 | Default: `true`
|
---|
58 |
|
---|
59 | Preserve `paths` order when searching.
|
---|
60 |
|
---|
61 | Disable this to improve performance if you don't care about the order.
|
---|
62 |
|
---|
63 | ##### cwd
|
---|
64 |
|
---|
65 | Type: `string`\
|
---|
66 | Default: `process.cwd()`
|
---|
67 |
|
---|
68 | Current working directory.
|
---|
69 |
|
---|
70 | ##### type
|
---|
71 |
|
---|
72 | Type: `string`\
|
---|
73 | Default: `'file'`\
|
---|
74 | Values: `'file' | 'directory'`
|
---|
75 |
|
---|
76 | The type of paths that can match.
|
---|
77 |
|
---|
78 | ##### allowSymlinks
|
---|
79 |
|
---|
80 | Type: `boolean`\
|
---|
81 | Default: `true`
|
---|
82 |
|
---|
83 | Allow symbolic links to match if they point to the chosen path type.
|
---|
84 |
|
---|
85 | ### locatePath.sync(paths, options?)
|
---|
86 |
|
---|
87 | Returns the first path that exists or `undefined` if none exists.
|
---|
88 |
|
---|
89 | #### paths
|
---|
90 |
|
---|
91 | Type: `Iterable<string>`
|
---|
92 |
|
---|
93 | Paths to check.
|
---|
94 |
|
---|
95 | #### options
|
---|
96 |
|
---|
97 | Type: `object`
|
---|
98 |
|
---|
99 | ##### cwd
|
---|
100 |
|
---|
101 | Same as above.
|
---|
102 |
|
---|
103 | ##### type
|
---|
104 |
|
---|
105 | Same as above.
|
---|
106 |
|
---|
107 | ##### allowSymlinks
|
---|
108 |
|
---|
109 | Same as above.
|
---|
110 |
|
---|
111 | ## Related
|
---|
112 |
|
---|
113 | - [path-exists](https://github.com/sindresorhus/path-exists) - Check if a path exists
|
---|
114 |
|
---|
115 | ---
|
---|
116 |
|
---|
117 | <div align="center">
|
---|
118 | <b>
|
---|
119 | <a href="https://tidelift.com/subscription/pkg/npm-locate-path?utm_source=npm-locate-path&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
|
---|
120 | </b>
|
---|
121 | <br>
|
---|
122 | <sub>
|
---|
123 | Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
---|
124 | </sub>
|
---|
125 | </div>
|
---|