source: trip-planner-front/node_modules/path-type/index.js@ 8d391a1

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

initial commit

  • Property mode set to 100644
File size: 1.1 KB
Line 
1'use strict';
2const {promisify} = require('util');
3const fs = require('fs');
4
5async function isType(fsStatType, statsMethodName, filePath) {
6 if (typeof filePath !== 'string') {
7 throw new TypeError(`Expected a string, got ${typeof filePath}`);
8 }
9
10 try {
11 const stats = await promisify(fs[fsStatType])(filePath);
12 return stats[statsMethodName]();
13 } catch (error) {
14 if (error.code === 'ENOENT') {
15 return false;
16 }
17
18 throw error;
19 }
20}
21
22function isTypeSync(fsStatType, statsMethodName, filePath) {
23 if (typeof filePath !== 'string') {
24 throw new TypeError(`Expected a string, got ${typeof filePath}`);
25 }
26
27 try {
28 return fs[fsStatType](filePath)[statsMethodName]();
29 } catch (error) {
30 if (error.code === 'ENOENT') {
31 return false;
32 }
33
34 throw error;
35 }
36}
37
38exports.isFile = isType.bind(null, 'stat', 'isFile');
39exports.isDirectory = isType.bind(null, 'stat', 'isDirectory');
40exports.isSymlink = isType.bind(null, 'lstat', 'isSymbolicLink');
41exports.isFileSync = isTypeSync.bind(null, 'statSync', 'isFile');
42exports.isDirectorySync = isTypeSync.bind(null, 'statSync', 'isDirectory');
43exports.isSymlinkSync = isTypeSync.bind(null, 'lstatSync', 'isSymbolicLink');
Note: See TracBrowser for help on using the repository browser.