1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | var fs = require('fs');
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function isDirectory(filepath, cb) {
|
17 | if (typeof cb !== 'function') {
|
18 | throw new Error('expected a callback function');
|
19 | }
|
20 |
|
21 | if (typeof filepath !== 'string') {
|
22 | cb(new Error('expected filepath to be a string'));
|
23 | return;
|
24 | }
|
25 |
|
26 | fs.stat(filepath, function(err, stats) {
|
27 | if (err) {
|
28 | if (err.code === 'ENOENT') {
|
29 | cb(null, false);
|
30 | return;
|
31 | }
|
32 | cb(err);
|
33 | return;
|
34 | }
|
35 | cb(null, stats.isDirectory());
|
36 | });
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | isDirectory.sync = function isDirectorySync(filepath) {
|
44 | if (typeof filepath !== 'string') {
|
45 | throw new Error('expected filepath to be a string');
|
46 | }
|
47 |
|
48 | try {
|
49 | var stat = fs.statSync(filepath);
|
50 | return stat.isDirectory();
|
51 | } catch (err) {
|
52 | if (err.code === 'ENOENT') {
|
53 | return false;
|
54 | } else {
|
55 | throw err;
|
56 | }
|
57 | }
|
58 | return false;
|
59 | };
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | module.exports = isDirectory;
|