From a8cdd4a2576490f02fe258d38a4a283319f51ec7 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Wed, 6 May 2015 19:55:45 +0100 Subject: [PATCH] isDirectory method --- README.md | 24 ++++++++++++++++++++++++ lib/index.js | 6 ++++++ lib/is-dir.js | 18 ++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 lib/is-dir.js diff --git a/README.md b/README.md index 418c72cb..6b542aa2 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,8 @@ Methods - [ensureFileSync](#ensurefilefile-callback) - [ensureDir](#ensuredirdir-callback) - [ensureDirSync](#ensuredirdir-callback) +- [isDirectory](#isdirectoryfile-callback) +- [isDirectorySync](#isdirectoryfile-callback) - [mkdirs](#mkdirsdir-callback) - [mkdirsSync](#mkdirsdir-callback) - [move](#movesrc-dest-options-callback) @@ -197,6 +199,28 @@ fs.ensureDir(dir, function (err) { +### isDirectory(file, callback) + +Check if a file is a directory. Shortcut for `fs.stat()` followed by running `isDirectory()` on the result. + +Alias: `isDir()` + +Sync: `isDirectorySync()` (alias `isDirSync()`) + + +Example: + +```js +var fs = require('fs-extra') + +var file = '/tmp/path/to/a/file' +fs.isDirectory(file, function (err, isDir) { + console.log(isDir) // => true if is directory, false if not +}) +``` + + + ### mkdirs(dir, callback) Creates a directory. If the parent hierarchy doesn't exist, it's created. Like `mkdir -p`. diff --git a/lib/index.js b/lib/index.js index 59d47c68..086f4ef0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -87,6 +87,12 @@ Object.keys(empty).forEach(function (method) { fs[method] = empty[method] }) +var isDir = require('./is-dir') +fs.isDirectory = isDir.isDirectory +fs.isDirectorySync = isDir.isDirectorySync +fs.isDir = isDir.isDirectory +fs.isDirSync = isDir.isDirectorySync + module.exports = fs jsonFile.spaces = 2 // set to 2 diff --git a/lib/is-dir.js b/lib/is-dir.js new file mode 100644 index 00000000..edfd14dc --- /dev/null +++ b/lib/is-dir.js @@ -0,0 +1,18 @@ +var fs = require('fs') + +function isDirectory (path, callback) { + fs.stat(path, function (err, stats) { + if (err) return callback(err) + + callback(null, stats.isDirectory()) + }) +} + +function isDirectorySync (path) { + return fs.statSync(path).isDirectory() +} + +module.exports = { + isDirectory: isDirectory, + isDirectorySync: isDirectorySync +}