[6a3a178] | 1 | 'use strict';
|
---|
| 2 | const {promisify} = require('util');
|
---|
| 3 | const fs = require('fs');
|
---|
| 4 |
|
---|
| 5 | async 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 |
|
---|
| 22 | function 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 |
|
---|
| 38 | exports.isFile = isType.bind(null, 'stat', 'isFile');
|
---|
| 39 | exports.isDirectory = isType.bind(null, 'stat', 'isDirectory');
|
---|
| 40 | exports.isSymlink = isType.bind(null, 'lstat', 'isSymbolicLink');
|
---|
| 41 | exports.isFileSync = isTypeSync.bind(null, 'statSync', 'isFile');
|
---|
| 42 | exports.isDirectorySync = isTypeSync.bind(null, 'statSync', 'isDirectory');
|
---|
| 43 | exports.isSymlinkSync = isTypeSync.bind(null, 'lstatSync', 'isSymbolicLink');
|
---|