UNPKG

1.12 kBJavaScriptView Raw
1/*!
2 * is-directory <https://github.com/jonschlinkert/is-directory>
3 *
4 * Copyright (c) 2014-2015, Jon Schlinkert.
5 * Licensed under the MIT License.
6 */
7
8'use strict';
9
10var fs = require('fs');
11
12/**
13 * async
14 */
15
16function 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 * sync
41 */
42
43isDirectory.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 * Expose `isDirectory`
63 */
64
65module.exports = isDirectory;