source: trip-planner-front/node_modules/locate-path/index.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.3 KB
Line 
1'use strict';
2const path = require('path');
3const fs = require('fs');
4const {promisify} = require('util');
5const pLocate = require('p-locate');
6
7const fsStat = promisify(fs.stat);
8const fsLStat = promisify(fs.lstat);
9
10const typeMappings = {
11 directory: 'isDirectory',
12 file: 'isFile'
13};
14
15function checkType({type}) {
16 if (type in typeMappings) {
17 return;
18 }
19
20 throw new Error(`Invalid type specified: ${type}`);
21}
22
23const matchType = (type, stat) => type === undefined || stat[typeMappings[type]]();
24
25module.exports = async (paths, options) => {
26 options = {
27 cwd: process.cwd(),
28 type: 'file',
29 allowSymlinks: true,
30 ...options
31 };
32 checkType(options);
33 const statFn = options.allowSymlinks ? fsStat : fsLStat;
34
35 return pLocate(paths, async path_ => {
36 try {
37 const stat = await statFn(path.resolve(options.cwd, path_));
38 return matchType(options.type, stat);
39 } catch (_) {
40 return false;
41 }
42 }, options);
43};
44
45module.exports.sync = (paths, options) => {
46 options = {
47 cwd: process.cwd(),
48 allowSymlinks: true,
49 type: 'file',
50 ...options
51 };
52 checkType(options);
53 const statFn = options.allowSymlinks ? fs.statSync : fs.lstatSync;
54
55 for (const path_ of paths) {
56 try {
57 const stat = statFn(path.resolve(options.cwd, path_));
58
59 if (matchType(options.type, stat)) {
60 return path_;
61 }
62 } catch (_) {
63 }
64 }
65};
Note: See TracBrowser for help on using the repository browser.