Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isDirectory method #132

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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`.
Expand Down
6 changes: 6 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions lib/is-dir.js
Original file line number Diff line number Diff line change
@@ -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()
Copy link

@yvele yvele Nov 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't isDirectorySync supposed not to throw if the path doesn't exists? Because it will throw if you don't catch statSync for ENOENT errors.

The strange thing is that isDirectory swallows the errors and isDirectorySync not.. they don't have the same behavior. Edit: Oops.. I didn't noticed the if (err) return callback(err)

@overlookmotel I think you have the exact same problem here: https://github.com/overlookmotel/fs-extra-promise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yvele I don't see it as a bug. I think it should throw if the path doesn't exist, same as calling fs.stat()/fs.statSync() would.

}

module.exports = {
isDirectory: isDirectory,
isDirectorySync: isDirectorySync
}