[Pkg-javascript-commits] [node-fs-extra] 01/03: New upstream version 2.1.2
Julien Puydt
julien.puydt at laposte.net
Sat Mar 25 08:50:55 UTC 2017
This is an automated email from the git hooks/post-receive script.
jpuydt-guest pushed a commit to branch master
in repository node-fs-extra.
commit 02570a3afcd0b3a15a275508958bd315eaa9c40f
Author: Julien Puydt <julien.puydt at laposte.net>
Date: Sat Mar 25 09:42:36 2017 +0100
New upstream version 2.1.2
---
CHANGELOG.md | 29 +-
README.md | 52 +--
docs/copy-sync.md | 37 +++
docs/copy.md | 44 ++-
docs/{emptyDir.md => emptyDir-sync.md} | 12 +-
docs/emptyDir.md | 11 +-
docs/ensureDir-sync.md | 17 +
docs/ensureDir.md | 10 +-
docs/ensureFile-sync.md | 17 +
docs/ensureFile.md | 10 +-
docs/ensureLink-sync.md | 17 +
docs/ensureLink.md | 13 +-
docs/ensureSymlink-sync.md | 18 ++
docs/ensureSymlink.md | 14 +-
docs/move-sync.md | 24 ++
docs/move.md | 26 +-
docs/outputFile-sync.md | 19 ++
docs/outputFile.md | 16 +-
docs/outputJson-sync.md | 22 ++
docs/outputJson.md | 16 +-
docs/readJson-sync.md | 33 ++
docs/readJson.md | 27 +-
docs/remove-sync.md | 16 +
docs/remove.md | 17 +-
docs/writeJson-sync.md | 21 ++
docs/writeJson.md | 14 +-
lib/__tests__/fs-integration.test.js | 30 +-
lib/copy-sync/__tests__/broken-symlink.test.js | 54 ++--
lib/copy-sync/__tests__/copy-sync-dir.test.js | 179 +++++++----
lib/copy-sync/__tests__/copy-sync-file.test.js | 159 +++++----
.../__tests__/copy-sync-preserve-time.test.js | 46 +--
lib/copy-sync/__tests__/symlink.test.js | 50 +--
lib/copy-sync/copy-file-sync.js | 26 +-
lib/copy-sync/copy-sync.js | 32 +-
lib/copy/__tests__/async/copy-gh-89.test.js | 40 +--
lib/copy/__tests__/copy-dev-null.test.js | 34 +-
lib/copy/__tests__/copy-permissions.test.js | 66 ++--
lib/copy/__tests__/copy-preserve-time.test.js | 54 ++--
lib/copy/__tests__/copy.test.js | 319 +++++++++++++------
lib/copy/__tests__/ncp/broken-symlink.test.js | 45 +--
lib/copy/__tests__/ncp/ncp-error-perm.test.js | 38 +--
lib/copy/__tests__/ncp/ncp.test.js | 145 ++++-----
lib/copy/__tests__/ncp/symlink.test.js | 54 ++--
lib/copy/copy.js | 30 +-
lib/copy/ncp.js | 2 +-
lib/empty/__tests__/empty-dir-sync.test.js | 34 +-
lib/empty/__tests__/empty-dir.test.js | 40 +--
lib/empty/index.js | 28 +-
lib/ensure/__tests__/create.test.js | 54 ++--
lib/ensure/__tests__/ensure.test.js | 86 ++---
lib/ensure/__tests__/link.test.js | 184 +++++------
lib/ensure/__tests__/symlink-paths.test.js | 66 ++--
lib/ensure/__tests__/symlink-type.test.js | 64 ++--
lib/ensure/__tests__/symlink.test.js | 354 ++++++++++-----------
lib/ensure/file.js | 24 +-
lib/ensure/index.js | 8 +-
lib/ensure/link.js | 30 +-
lib/ensure/symlink-paths.js | 27 +-
lib/ensure/symlink-type.js | 14 +-
lib/ensure/symlink.js | 48 +--
lib/index.js | 21 +-
lib/json/__tests__/jsonfile-integration.test.js | 34 +-
lib/json/__tests__/output-json-sync.test.js | 45 +--
lib/json/__tests__/output-json.test.js | 47 +--
lib/json/__tests__/read.test.js | 39 ++-
lib/json/__tests__/spaces.test.js | 30 +-
lib/json/index.js | 4 +-
lib/json/jsonfile.js | 4 +-
lib/json/output-json-sync.js | 12 +-
lib/json/output-json.js | 16 +-
lib/mkdirs/__tests__/chmod.test.js | 62 ++--
lib/mkdirs/__tests__/clobber.test.js | 40 +--
lib/mkdirs/__tests__/issue-209.test.js | 26 +-
lib/mkdirs/__tests__/issue-93.test.js | 24 +-
lib/mkdirs/__tests__/mkdir.test.js | 50 +--
lib/mkdirs/__tests__/mkdirp.test.js | 44 +--
lib/mkdirs/__tests__/opts-undef.test.js | 26 +-
lib/mkdirs/__tests__/perm.test.js | 42 +--
lib/mkdirs/__tests__/perm_sync.test.js | 44 +--
lib/mkdirs/__tests__/race.test.js | 55 ++--
lib/mkdirs/__tests__/rel.test.js | 50 +--
lib/mkdirs/__tests__/return.test.js | 36 +--
lib/mkdirs/__tests__/return_sync.test.js | 34 +-
lib/mkdirs/__tests__/root.test.js | 22 +-
lib/mkdirs/__tests__/sync.test.js | 43 +--
lib/mkdirs/__tests__/umask.test.js | 54 ++--
lib/mkdirs/mkdirs-sync.js | 18 +-
lib/mkdirs/mkdirs.js | 22 +-
lib/mkdirs/win32.js | 13 +-
.../move-sync-prevent-moving-into-itself.test.js | 189 +++++++++++
lib/move-sync/__tests__/move-sync.test.js | 352 ++++++++++++++++++++
lib/move-sync/index.js | 117 +++++++
lib/move/__tests__/move.test.js | 230 +++++++------
lib/move/index.js | 74 +++--
lib/output/__tests__/output.test.js | 54 ++--
lib/output/index.js | 20 +-
lib/remove/__tests__/remove-dir.test.js | 26 +-
lib/remove/__tests__/remove-sync-dir.test.js | 24 +-
lib/remove/__tests__/remove-sync-file.test.js | 24 +-
lib/remove/__tests__/remove.test.js | 72 ++---
lib/remove/index.js | 10 +-
lib/remove/rimraf.js | 89 +++---
lib/util/__tests__/utimes.test.js | 94 +++---
lib/util/assign.js | 10 +-
lib/util/utimes.js | 42 +--
package.json | 4 +-
test.js | 31 +-
107 files changed, 3325 insertions(+), 2110 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7f810c8..99b4bd8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,14 +1,35 @@
-Unreleased
-----------
+2.1.2 / 2017-03-16
+------------------
-### Added
+### Fixed
-### Removed
+- Weird windows bug that resulted in `ensureDir()`'s callback being called twice in some cases. This bug may have also affected `remove()`. See [#392](https://github.com/jprichardson/node-fs-extra/issues/392), [#393](https://github.com/jprichardson/node-fs-extra/pull/393)
+
+2.1.1 / 2017-03-15
+------------------
+
+### Fixed
+
+- Reverted [`5597bd`](https://github.com/jprichardson/node-fs-extra/commit/5597bd5b67f7d060f5f5bf26e9635be48330f5d7), this broke compatibility with Node.js versions v4+ but less than `v4.5.0`.
+- Remove `Buffer.alloc()` usage in `moveSync()`.
+
+2.1.0 / 2017-03-15
+------------------
+
+Thanks to [Mani Maghsoudlou (@manidlou)](https://github.com/manidlou) & [Jan Peer Stöcklmair (@JPeer264)](https://github.com/JPeer264) for their extraordinary help with this release!
+
+### Added
+- `moveSync()` See [#309], [#381](https://github.com/jprichardson/node-fs-extra/pull/381). ([@manidlou](https://github.com/manidlou))
+- `copy()` and `copySync()`'s `filter` option now gets the destination path passed as the second parameter. [#366](https://github.com/jprichardson/node-fs-extra/pull/366) ([@manidlou](https://github.com/manidlou))
### Changed
+- Use `Buffer.alloc()` instead of deprecated `new Buffer()` in `copySync()`. [#380](https://github.com/jprichardson/node-fs-extra/pull/380) ([@manidlou](https://github.com/manidlou))
+- Refactored entire codebase to use ES6 features supported by Node.js v4+ [#355](https://github.com/jprichardson/node-fs-extra/issues/355). [(@JPeer264)](https://github.com/JPeer264)
+- Refactored docs. ([@manidlou](https://github.com/manidlou))
### Fixed
+- `move()` shouldn't error out when source and dest are the same. [#377](https://github.com/jprichardson/node-fs-extra/issues/377), [#378](https://github.com/jprichardson/node-fs-extra/pull/378) ([@jdalton](https://github.com/jdalton))
2.0.0 / 2017-01-16
------------------
diff --git a/README.md b/README.md
index 4805844..cee4399 100644
--- a/README.md
+++ b/README.md
@@ -35,27 +35,27 @@ Usage
You don't ever need to include the original `fs` module again:
```js
-var fs = require('fs') // this is no longer necessary
+const fs = require('fs') // this is no longer necessary
```
you can now do this:
```js
-var fs = require('fs-extra')
+const fs = require('fs-extra')
```
or if you prefer to make it clear that you're using `fs-extra` and not `fs`, you may want
to name your `fs` variable `fse` like so:
```js
-var fse = require('fs-extra')
+const fse = require('fs-extra')
```
you can also keep both, but it's redundant:
```js
-var fs = require('fs')
-var fse = require('fs-extra')
+const fs = require('fs')
+const fse = require('fs-extra')
```
Sync vs Async
@@ -67,9 +67,9 @@ Sync methods on the other hand will throw if an error occurs.
Example:
```js
-var fs = require('fs-extra')
+const fs = require('fs-extra')
-fs.copy('/tmp/myfile', '/tmp/mynewfile', function (err) {
+fs.copy('/tmp/myfile', '/tmp/mynewfile', err => {
if (err) return console.error(err)
console.log("success!")
});
@@ -85,38 +85,45 @@ try {
Methods
-------
+
+### Async
+
- [copy](docs/copy.md)
-- [copySync](docs/copy.md)
- [emptyDir](docs/emptyDir.md)
-- [emptyDirSync](docs/emptyDir.md)
- [ensureFile](docs/ensureFile.md)
-- [ensureFileSync](docs/ensureFile.md)
- [ensureDir](docs/ensureDir.md)
-- [ensureDirSync](docs/ensureDir.md)
- [ensureLink](docs/ensureLink.md)
-- [ensureLinkSync](docs/ensureLink.md)
- [ensureSymlink](docs/ensureSymlink.md)
-- [ensureSymlinkSync](docs/ensureSymlink.md)
- [mkdirs](docs/ensureDir.md)
-- [mkdirsSync](docs/ensureDir.md)
- [move](docs/move.md)
- [outputFile](docs/outputFile.md)
-- [outputFileSync](docs/outputFile.md)
- [outputJson](docs/outputJson.md)
-- [outputJsonSync](docs/outputJson.md)
- [readJson](docs/readJson.md)
-- [readJsonSync](docs/readJson.md)
- [remove](docs/remove.md)
-- [removeSync](docs/remove.md)
- [writeJson](docs/writeJson.md)
-- [writeJsonSync](docs/writeJson.md)
+
+### Sync
+
+- [copySync](docs/copy-sync.md)
+- [emptyDirSync](docs/emptyDir-sync.md)
+- [ensureFileSync](docs/ensureFile-sync.md)
+- [ensureDirSync](docs/ensureDir-sync.md)
+- [ensureLinkSync](docs/ensureLink-sync.md)
+- [ensureSymlinkSync](docs/ensureSymlink-sync.md)
+- [mkdirsSync](docs/ensureDir-sync.md)
+- [moveSync](docs/move-sync.md)
+- [outputFileSync](docs/outputFile-sync.md)
+- [outputJsonSync](docs/outputJson-sync.md)
+- [readJsonSync](docs/readJson-sync.md)
+- [removeSync](docs/remove-sync.md)
+- [writeJsonSync](docs/writeJson-sync.md)
**NOTE:** You can still use the native Node.js methods. They are copied over to `fs-extra`.
### What happened to `walk()` and `walkSync()`?
-They were removed from `fs-extra` in v2.0.0. If you need the functionality, `walk` and `walkSync` are available as separate packages, [`klaw`](https://github.com/jprichardson/node-klaw) and [`klaw-sync`](https://github.com/mawni/node-klaw-sync).
+They were removed from `fs-extra` in v2.0.0. If you need the functionality, `walk` and `walkSync` are available as separate packages, [`klaw`](https://github.com/jprichardson/node-klaw) and [`klaw-sync`](https://github.com/manidlou/node-klaw-sync).
Third Party
@@ -128,8 +135,8 @@ Use [Bluebird](https://github.com/petkaantonov/bluebird). See https://github.com
explicitly listed as supported.
```js
-var Promise = require('bluebird')
-var fs = Promise.promisifyAll(require('fs-extra'))
+const Promise = require('bluebird')
+const fs = Promise.promisifyAll(require('fs-extra'))
```
Or you can use a dedicated package:
@@ -138,6 +145,7 @@ Or you can use a dedicated package:
- [`fs-promise`](https://github.com/kevinbeaty/fs-promise) uses
[Any Promise](https://github.com/kevinbeaty/any-promise) and also covers
[`mz/fs`](https://github.com/normalize/mz/blob/master/fs.js).
+- [`fs-p`](https://github.com/grammarly/fs-p) - TypeScript-friendly promises implementation
### TypeScript
diff --git a/docs/copy-sync.md b/docs/copy-sync.md
new file mode 100644
index 0000000..8e61c2b
--- /dev/null
+++ b/docs/copy-sync.md
@@ -0,0 +1,37 @@
+# copySync(src, dest, [options])
+
+Copy a file or directory. The directory can have contents. Like `cp -r`.
+
+- `src` `<String>`
+- `dest` `<String>`
+- `options` `<Object>`
+ - `overwrite` `<boolean>`: overwrite existing file or directory, default is `true`. _Note that the copy operation will silently fail if you set this to `false` and the destination exists._ Use the `errorOnExist` option to change this behavior.
+ - `errorOnExist` `<boolean>`: when `overwrite` is `false` and the destination exists, throw an error. Default is `false`.
+ - `dereference` `<boolean>`: dereference symlinks, default is `false`.
+ - `preserveTimestamps` `<boolean>`: will set last modification and access times to the ones of the original source files, default is `false`.
+ - `filter` `<Function>`: Function to filter copied files. Return `true` to include, `false` to exclude. This can also be a RegExp, however this is deprecated (See [issue #239](https://github.com/jprichardson/node-fs-extra/issues/239) for background).
+
+## Example:
+
+```js
+const fs = require('fs-extra')
+
+// copy file
+fs.copySync('/tmp/myfile', '/tmp/mynewfile')
+
+// copy directory, even if it has subdirectories or files
+fs.copySync('/tmp/mydir', '/tmp/mynewdir')
+```
+
+**Using filter function**
+
+```js
+const fs = require('fs-extra')
+
+const filterFunc = (src, dest) => {
+ // your logic here
+ // it will be copied if return true
+}
+
+fs.copySync('/tmp/mydir', '/tmp/mynewdir', { filter: filterFunc })
+```
diff --git a/docs/copy.md b/docs/copy.md
index 617510f..a4f75e9 100644
--- a/docs/copy.md
+++ b/docs/copy.md
@@ -2,27 +2,47 @@
Copy a file or directory. The directory can have contents. Like `cp -r`.
-**Sync:** `copySync()`
-
-## Options:
-- overwrite (boolean): overwrite existing file or directory, default is `true`. _Note that the copy operation will silently fail if you set this to `false` and the destination exists._ Use the `errorOnExist` option to change this behavior.
-- errorOnExist (boolean): when `overwrite` is `false` and the destination exists, throw an error. Default is `false`.
-- dereference (boolean): dereference symlinks, default is `false`.
-- preserveTimestamps (boolean): will set last modification and access times to the ones of the original source files, default is `false`.
-- filter: Function to filter copied files. Return `true` to include, `false` to exclude. This can also be a RegExp, however this is deprecated (See [issue #239](https://github.com/jprichardson/node-fs-extra/issues/239) for background).
+- `src` `<String>`
+- `dest` `<String>`
+- `options` `<Object>`
+ - `overwrite` `<boolean>`: overwrite existing file or directory, default is `true`. _Note that the copy operation will silently fail if you set this to `false` and the destination exists._ Use the `errorOnExist` option to change this behavior.
+ - `errorOnExist` `<boolean>`: when `overwrite` is `false` and the destination exists, throw an error. Default is `false`.
+ - `dereference` `<boolean>`: dereference symlinks, default is `false`.
+ - `preserveTimestamps` `<boolean>`: will set last modification and access times to the ones of the original source files, default is `false`.
+ - `filter` `<Function>`: Function to filter copied files. Return `true` to include, `false` to exclude. This can also be a RegExp, however this is deprecated (See [issue #239](https://github.com/jprichardson/node-fs-extra/issues/239) for background).
+- `callback` `<Function>`
## Example:
```js
-var fs = require('fs-extra')
+const fs = require('fs-extra')
-fs.copy('/tmp/myfile', '/tmp/mynewfile', function (err) {
+fs.copy('/tmp/myfile', '/tmp/mynewfile', err => {
if (err) return console.error(err)
- console.log("success!")
+
+ console.log('success!')
}) // copies file
-fs.copy('/tmp/mydir', '/tmp/mynewdir', function (err) {
+fs.copy('/tmp/mydir', '/tmp/mynewdir', err => {
if (err) return console.error(err)
+
console.log('success!')
}) // copies directory, even if it has subdirectories or files
```
+
+**Using filter function**
+
+```js
+const fs = require('fs-extra')
+
+const filterFunc = (src, dest) => {
+ // your logic here
+ // it will be copied if return true
+}
+
+fs.copy('/tmp/mydir', '/tmp/mynewdir', { filter: filterFunc }, err => {
+ if (err) return console.error(err)
+
+ console.log('success!')
+})
+```
diff --git a/docs/emptyDir.md b/docs/emptyDir-sync.md
similarity index 55%
copy from docs/emptyDir.md
copy to docs/emptyDir-sync.md
index d57c151..7decdbc 100644
--- a/docs/emptyDir.md
+++ b/docs/emptyDir-sync.md
@@ -1,18 +1,16 @@
-# emptyDir(dir, [callback])
+# emptyDirSync(dir)
Ensures that a directory is empty. Deletes directory contents if the directory is not empty. If the directory does not exist, it is created. The directory itself is not deleted.
-**Alias:** `emptydir()`
+**Alias:** `emptydirSync()`
-**Sync:** `emptyDirSync()`, `emptydirSync()`
+- `dir` `<String>`
## Example:
```js
-var fs = require('fs-extra')
+const fs = require('fs-extra')
// assume this directory has a lot of files and folders
-fs.emptyDir('/tmp/some/dir', function (err) {
- if (!err) console.log('success!')
-})
+fs.emptyDirSync('/tmp/some/dir')
```
diff --git a/docs/emptyDir.md b/docs/emptyDir.md
index d57c151..43292e3 100644
--- a/docs/emptyDir.md
+++ b/docs/emptyDir.md
@@ -4,15 +4,18 @@ Ensures that a directory is empty. Deletes directory contents if the directory i
**Alias:** `emptydir()`
-**Sync:** `emptyDirSync()`, `emptydirSync()`
+- `dir` `<String>`
+- `callback` `<Function>`
## Example:
```js
-var fs = require('fs-extra')
+const fs = require('fs-extra')
// assume this directory has a lot of files and folders
-fs.emptyDir('/tmp/some/dir', function (err) {
- if (!err) console.log('success!')
+fs.emptyDir('/tmp/some/dir', err => {
+ if (err) return console.error(err)
+
+ console.log('success!')
})
```
diff --git a/docs/ensureDir-sync.md b/docs/ensureDir-sync.md
new file mode 100644
index 0000000..8f083d2
--- /dev/null
+++ b/docs/ensureDir-sync.md
@@ -0,0 +1,17 @@
+# ensureDirSync(dir)
+
+Ensures that the directory exists. If the directory structure does not exist, it is created. Like `mkdir -p`.
+
+**Aliases:** `mkdirsSync()`, `mkdirpSync()`
+
+- `dir` `<String>`
+
+## Example:
+
+```js
+const fs = require('fs-extra')
+
+const dir = '/tmp/this/path/does/not/exist'
+fs.ensureDirSync(dir)
+// dir has now been created, including the directory it is to be placed in
+```
diff --git a/docs/ensureDir.md b/docs/ensureDir.md
index ac704e0..1f8acfb 100644
--- a/docs/ensureDir.md
+++ b/docs/ensureDir.md
@@ -4,16 +4,16 @@ Ensures that the directory exists. If the directory structure does not exist, it
**Aliases:** `mkdirs()`, `mkdirp()`
-**Sync:** `ensureDirSync()`, `mkdirsSync()`, `mkdirpSync()`
-
+- `dir` `<String>`
+- `callback` `<Function>`
## Example:
```js
-var fs = require('fs-extra')
+const fs = require('fs-extra')
-var dir = '/tmp/this/path/does/not/exist'
-fs.ensureDir(dir, function (err) {
+const dir = '/tmp/this/path/does/not/exist'
+fs.ensureDir(dir, err => {
console.log(err) // => null
// dir has now been created, including the directory it is to be placed in
})
diff --git a/docs/ensureFile-sync.md b/docs/ensureFile-sync.md
new file mode 100644
index 0000000..25ac39d
--- /dev/null
+++ b/docs/ensureFile-sync.md
@@ -0,0 +1,17 @@
+# ensureFileSync(file)
+
+Ensures that the file exists. If the file that is requested to be created is in directories that do not exist, these directories are created. If the file already exists, it is **NOT MODIFIED**.
+
+**Alias:** `createFileSync()`
+
+- `file` `<String>`
+
+## Example:
+
+```js
+const fs = require('fs-extra')
+
+const file = '/tmp/this/path/does/not/exist/file.txt'
+fs.ensureFileSync(file)
+// file has now been created, including the directory it is to be placed in
+```
diff --git a/docs/ensureFile.md b/docs/ensureFile.md
index 21fcc8e..1567e60 100644
--- a/docs/ensureFile.md
+++ b/docs/ensureFile.md
@@ -4,16 +4,16 @@ Ensures that the file exists. If the file that is requested to be created is in
**Alias:** `createFile()`
-**Sync:** `createFileSync()`,`ensureFileSync()`
-
+- `file` `<String>`
+- `callback` `<Function>`
## Example:
```js
-var fs = require('fs-extra')
+const fs = require('fs-extra')
-var file = '/tmp/this/path/does/not/exist/file.txt'
-fs.ensureFile(file, function (err) {
+const file = '/tmp/this/path/does/not/exist/file.txt'
+fs.ensureFile(file, err => {
console.log(err) // => null
// file has now been created, including the directory it is to be placed in
})
diff --git a/docs/ensureLink-sync.md b/docs/ensureLink-sync.md
new file mode 100644
index 0000000..74769d3
--- /dev/null
+++ b/docs/ensureLink-sync.md
@@ -0,0 +1,17 @@
+# ensureLinkSync(srcpath, dstpath)
+
+Ensures that the link exists. If the directory structure does not exist, it is created.
+
+- `srcpath` `<String>`
+- `dstpath` `<String>`
+
+## Example:
+
+```js
+const fs = require('fs-extra')
+
+const srcpath = '/tmp/file.txt'
+const dstpath = '/tmp/this/path/does/not/exist/file.txt'
+fs.ensureLinkSync(srcpath, dstpath)
+// link has now been created, including the directory it is to be placed in
+```
diff --git a/docs/ensureLink.md b/docs/ensureLink.md
index 25df997..3cf38c5 100644
--- a/docs/ensureLink.md
+++ b/docs/ensureLink.md
@@ -2,17 +2,18 @@
Ensures that the link exists. If the directory structure does not exist, it is created.
-**Sync:** `ensureLinkSync()`
-
+- `srcpath` `<String>`
+- `dstpath` `<String>`
+- `callback` `<Function>`
## Example:
```js
-var fs = require('fs-extra')
+const fs = require('fs-extra')
-var srcpath = '/tmp/file.txt'
-var dstpath = '/tmp/this/path/does/not/exist/file.txt'
-fs.ensureLink(srcpath, dstpath, function (err) {
+const srcpath = '/tmp/file.txt'
+const dstpath = '/tmp/this/path/does/not/exist/file.txt'
+fs.ensureLink(srcpath, dstpath, err => {
console.log(err) // => null
// link has now been created, including the directory it is to be placed in
})
diff --git a/docs/ensureSymlink-sync.md b/docs/ensureSymlink-sync.md
new file mode 100644
index 0000000..328d4c4
--- /dev/null
+++ b/docs/ensureSymlink-sync.md
@@ -0,0 +1,18 @@
+# ensureSymlinkSync(srcpath, dstpath, [type])
+
+Ensures that the symlink exists. If the directory structure does not exist, it is created.
+
+- `srcpath` `<String>`
+- `dstpath` `<String>`
+- `type` `<String>`
+
+## Example:
+
+```js
+const fs = require('fs-extra')
+
+const srcpath = '/tmp/file.txt'
+const dstpath = '/tmp/this/path/does/not/exist/file.txt'
+fs.ensureSymlinkSync(srcpath, dstpath)
+// symlink has now been created, including the directory it is to be placed in
+```
diff --git a/docs/ensureSymlink.md b/docs/ensureSymlink.md
index a29754b..b6c9695 100644
--- a/docs/ensureSymlink.md
+++ b/docs/ensureSymlink.md
@@ -2,17 +2,19 @@
Ensures that the symlink exists. If the directory structure does not exist, it is created.
-**Sync:** `ensureSymlinkSync()`
-
+- `srcpath` `<String>`
+- `dstpath` `<String>`
+- `type` `<String>`
+- `callback` `<Function>`
## Example:
```js
-var fs = require('fs-extra')
+const fs = require('fs-extra')
-var srcpath = '/tmp/file.txt'
-var dstpath = '/tmp/this/path/does/not/exist/file.txt'
-fs.ensureSymlink(srcpath, dstpath, function (err) {
+const srcpath = '/tmp/file.txt'
+const dstpath = '/tmp/this/path/does/not/exist/file.txt'
+fs.ensureSymlink(srcpath, dstpath, err => {
console.log(err) // => null
// symlink has now been created, including the directory it is to be placed in
})
diff --git a/docs/move-sync.md b/docs/move-sync.md
new file mode 100644
index 0000000..cd701fe
--- /dev/null
+++ b/docs/move-sync.md
@@ -0,0 +1,24 @@
+# moveSync(src, dest, [options])
+
+Moves a file or directory, even across devices.
+
+- `src` `<String>`
+- `dest` `<String>`
+- `options` `<Object>`
+ - `overwrite` `<boolean>`: overwrite existing file or directory, default is `false`.
+
+## Example:
+
+```js
+const fs = require('fs-extra')
+
+fs.moveSync('/tmp/somefile', '/tmp/does/not/exist/yet/somefile')
+```
+
+**Using `overwrite` option**
+
+```js
+const fs = require('fs-extra')
+
+fs.moveSync('/tmp/somedir', '/tmp/may/already/existed/somedir', { overwrite: true })
+```
diff --git a/docs/move.md b/docs/move.md
index 5ff9153..547af7c 100644
--- a/docs/move.md
+++ b/docs/move.md
@@ -2,16 +2,32 @@
Moves a file or directory, even across devices.
-## Options:
-- overwrite (boolean): overwrite existing file or directory, default is `false`
+- `src` `<String>`
+- `dest` `<String>`
+- `options` `<Object>`
+ - `overwrite` `<boolean>`: overwrite existing file or directory, default is `false`.
+- `callback` `<Function>`
## Example:
```js
-var fs = require('fs-extra')
+const fs = require('fs-extra')
-fs.move('/tmp/somefile', '/tmp/does/not/exist/yet/somefile', function (err) {
+fs.move('/tmp/somefile', '/tmp/does/not/exist/yet/somefile', err => {
if (err) return console.error(err)
- console.log("success!")
+
+ console.log('success!')
+})
+```
+
+**Using `overwrite` option**
+
+```js
+const fs = require('fs-extra')
+
+fs.move('/tmp/somedir', '/tmp/may/already/existed/somedir', { overwrite: true }, err => {
+ if (err) return console.error(err)
+
+ console.log('success!')
})
```
diff --git a/docs/outputFile-sync.md b/docs/outputFile-sync.md
new file mode 100644
index 0000000..38eee8b
--- /dev/null
+++ b/docs/outputFile-sync.md
@@ -0,0 +1,19 @@
+# outputFileSync(file, data, [options])
+
+Almost the same as `writeFileSync` (i.e. it [overwrites](http://pages.citebite.com/v2o5n8l2f5reb)), except that if the parent directory does not exist, it's created. `file` must be a file path (a buffer or a file descriptor is not allowed). `options` are what you'd pass to [`fs.writeFileSync()`](https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options).
+
+- `file` `<String>`
+- `data` `<String> | <Buffer> | <Uint8Array>`
+- `options` `<Object> | <String>`
+
+## Example:
+
+```js
+const fs = require('fs-extra')
+
+const file = '/tmp/this/path/does/not/exist/file.txt'
+fs.outputFileSync(file, 'hello!')
+
+const data = fs.readFileSync(file, 'utf8')
+console.log(data) // => hello!
+```
diff --git a/docs/outputFile.md b/docs/outputFile.md
index 16a96c6..494cba3 100644
--- a/docs/outputFile.md
+++ b/docs/outputFile.md
@@ -1,20 +1,22 @@
# outputFile(file, data, [options], callback)
-Almost the same as `writeFile` (i.e. it [overwrites](http://pages.citebite.com/v2o5n8l2f5reb)), except that if the parent directory does not exist, it's created. `options` are what you'd pass to [`fs.writeFile()`](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback).
-
-**Sync:** `outputFileSync()`
+Almost the same as `writeFile` (i.e. it [overwrites](http://pages.citebite.com/v2o5n8l2f5reb)), except that if the parent directory does not exist, it's created. `file` must be a file path (a buffer or a file descriptor is not allowed). `options` are what you'd pass to [`fs.writeFile()`](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback).
+- `file` `<String>`
+- `data` `<String> | <Buffer> | <Uint8Array>`
+- `options` `<Object> | <String>`
+- `callback` `<Function>`
## Example:
```js
-var fs = require('fs-extra')
-var file = '/tmp/this/path/does/not/exist/file.txt'
+const fs = require('fs-extra')
-fs.outputFile(file, 'hello!', function (err) {
+const file = '/tmp/this/path/does/not/exist/file.txt'
+fs.outputFile(file, 'hello!', err => {
console.log(err) // => null
- fs.readFile(file, 'utf8', function (err, data) {
+ fs.readFile(file, 'utf8', (err, data) => {
console.log(data) // => hello!
})
})
diff --git a/docs/outputJson-sync.md b/docs/outputJson-sync.md
new file mode 100644
index 0000000..cab6316
--- /dev/null
+++ b/docs/outputJson-sync.md
@@ -0,0 +1,22 @@
+# outputJsonSync(file, object, [options])
+
+Almost the same as [`writeJsonSync`](writeJson-sync.md), except that if the directory does not exist, it's created.
+`options` are what you'd pass to [`jsonFile.writeFileSync()`](https://github.com/jprichardson/node-jsonfile#writefilesyncfilename-obj-options).
+
+**Alias:** `outputJSONSync()`
+
+- `file` `<String>`
+- `object` `<Object>`
+- `options` `<Object>`
+
+## Example:
+
+```js
+const fs = require('fs-extra')
+
+const file = '/tmp/this/path/does/not/exist/file.json'
+fs.outputJsonSync(file, {name: 'JP'})
+
+const data = fs.readJsonSync(file)
+console.log(data.name) // => JP
+```
diff --git a/docs/outputJson.md b/docs/outputJson.md
index 4ef0727..58917b8 100644
--- a/docs/outputJson.md
+++ b/docs/outputJson.md
@@ -1,23 +1,25 @@
-# outputJson(file, data, [options], callback)
+# outputJson(file, object, [options], callback)
Almost the same as [`writeJson`](writeJson.md), except that if the directory does not exist, it's created.
`options` are what you'd pass to [`jsonFile.writeFile()`](https://github.com/jprichardson/node-jsonfile#writefilefilename-options-callback).
**Alias:** `outputJSON()`
-**Sync:** `outputJsonSync()`, `outputJSONSync()`
-
+- `file` `<String>`
+- `object` `<Object>`
+- `options` `<Object>`
+- `callback` `<Function>`
## Example:
```js
-var fs = require('fs-extra')
-var file = '/tmp/this/path/does/not/exist/file.txt'
+const fs = require('fs-extra')
-fs.outputJson(file, {name: 'JP'}, function (err) {
+const file = '/tmp/this/path/does/not/exist/file.json'
+fs.outputJson(file, {name: 'JP'}, err => {
console.log(err) // => null
- fs.readJson(file, function(err, data) {
+ fs.readJson(file, (err, data) => {
console.log(data.name) // => JP
})
})
diff --git a/docs/readJson-sync.md b/docs/readJson-sync.md
new file mode 100644
index 0000000..a135637
--- /dev/null
+++ b/docs/readJson-sync.md
@@ -0,0 +1,33 @@
+# readJsonSync(file, [options])
+
+Reads a JSON file and then parses it into an object. `options` are the same
+that you'd pass to [`jsonFile.readFileSync`](https://github.com/jprichardson/node-jsonfile#readfilesyncfilename-options).
+
+**Alias:** `readJSONSync()`
+
+- `file` `<String>`
+- `options` `<Object>`
+
+## Example:
+
+```js
+const fs = require('fs-extra')
+
+const packageObj = fs.readJsonSync('./package.json')
+console.log(packageObj.version) // => 2.0.0
+```
+
+---
+
+`readJsonSync()` can take a `throws` option set to `false` and it won't throw if the JSON is invalid. Example:
+
+```js
+const fs = require('fs-extra')
+
+const file = '/tmp/some-invalid.json'
+const data = '{not valid JSON'
+fs.writeFileSync(file, data)
+
+const obj = fs.readJsonSync(file, { throws: false })
+console.log(obj) // => null
+```
diff --git a/docs/readJson.md b/docs/readJson.md
index 8d5e28c..acaf19c 100644
--- a/docs/readJson.md
+++ b/docs/readJson.md
@@ -5,29 +5,36 @@ that you'd pass to [`jsonFile.readFile`](https://github.com/jprichardson/node-js
**Alias:** `readJSON()`
-**Sync:** `readJsonSync()`, `readJSONSync()`
-
+- `file` `<String>`
+- `options` `<Object>`
+- `callback` `<Function>`
## Example:
```js
-var fs = require('fs-extra')
+const fs = require('fs-extra')
-fs.readJson('./package.json', function (err, packageObj) {
+fs.readJson('./package.json', (err, packageObj) => {
+ if (err) console.error(err)
+
console.log(packageObj.version) // => 0.1.3
})
```
---
-`readJsonSync()` can take a `throws` option set to `false` and it won't throw if the JSON is invalid. Example:
+`readJson()` can take a `throws` option set to `false` and it won't throw if the JSON is invalid. Example:
```js
-var fs = require('fs-extra')
-var file = path.join('/tmp/some-invalid.json')
-var data = '{not valid JSON'
+const fs = require('fs-extra')
+
+const file = '/tmp/some-invalid.json'
+const data = '{not valid JSON'
fs.writeFileSync(file, data)
-var obj = fs.readJsonSync(file, {throws: false})
-console.log(obj) // => null
+fs.readJson(file, { throws: false }, (err, obj) => {
+ if (err) console.error(err)
+
+ console.log(obj) // => null
+})
```
diff --git a/docs/remove-sync.md b/docs/remove-sync.md
new file mode 100644
index 0000000..fb01fe8
--- /dev/null
+++ b/docs/remove-sync.md
@@ -0,0 +1,16 @@
+# removeSync(path)
+
+Removes a file or directory. The directory can have contents. Like `rm -rf`.
+
+- `path` `<String>`
+
+## Example:
+
+```js
+const fs = require('fs-extra')
+
+// remove file
+fs.removeSync('/tmp/myfile')
+
+fs.removeSync('/home/jprichardson') // I just deleted my entire HOME directory.
+```
diff --git a/docs/remove.md b/docs/remove.md
index 785e808..68f9162 100644
--- a/docs/remove.md
+++ b/docs/remove.md
@@ -1,20 +1,25 @@
-# remove(dir, callback)
+# remove(path, callback)
Removes a file or directory. The directory can have contents. Like `rm -rf`.
-**Sync:** `removeSync()`
-
+- `path` `<String>`
+- `callback` `<Function>`
## Example:
```js
-var fs = require('fs-extra')
+const fs = require('fs-extra')
-fs.remove('/tmp/myfile', function (err) {
+// remove file
+fs.remove('/tmp/myfile', err => {
if (err) return console.error(err)
console.log('success!')
})
-fs.removeSync('/home/jprichardson') //I just deleted my entire HOME directory.
+fs.remove('/home/jprichardson', err => {
+ if (err) return console.error(err)
+
+ console.log('success!') // I just deleted my entire HOME directory.
+})
```
diff --git a/docs/writeJson-sync.md b/docs/writeJson-sync.md
new file mode 100644
index 0000000..9e649bb
--- /dev/null
+++ b/docs/writeJson-sync.md
@@ -0,0 +1,21 @@
+# writeJsonSync(file, object, [options])
+
+Writes an object to a JSON file. `options` are the same that
+you'd pass to [`jsonFile.writeFileSync()`](https://github.com/jprichardson/node-jsonfile#writefilesyncfilename-obj-options).
+
+**Alias:** `writeJSONSync()`
+
+- `file` `<String>`
+- `object` `<Object>`
+- `options` `<Object>`
+
+## Example:
+
+```js
+const fs = require('fs-extra')
+
+fs.writeJsonSync('./package.json', {name: 'fs-extra'})
+```
+---
+
+**See also:** [`outputJsonSync()`](outputJson-sync.md)
diff --git a/docs/writeJson.md b/docs/writeJson.md
index 04b9345..3f8f042 100644
--- a/docs/writeJson.md
+++ b/docs/writeJson.md
@@ -5,14 +5,20 @@ you'd pass to [`jsonFile.writeFile()`](https://github.com/jprichardson/node-json
**Alias:** `writeJSON()`
-**Sync:** `writeJsonSync()`, `writeJSONSync()`
+- `file` `<String>`
+- `object` `<Object>`
+- `options` `<Object>`
+- `callback` `<Function>`
## Example:
```js
-var fs = require('fs-extra')
-fs.writeJson('./package.json', {name: 'fs-extra'}, function (err) {
- console.log(err)
+const fs = require('fs-extra')
+
+fs.writeJson('./package.json', {name: 'fs-extra'}, err => {
+ if (err) return console.error(err)
+
+ console.log('success!')
})
```
diff --git a/lib/__tests__/fs-integration.test.js b/lib/__tests__/fs-integration.test.js
index efd7ba9..4ff52c5 100644
--- a/lib/__tests__/fs-integration.test.js
+++ b/lib/__tests__/fs-integration.test.js
@@ -1,31 +1,31 @@
-var assert = require('assert')
-var path = require('path')
-var os = require('os')
-var fs = require('fs')
-var fse = require('../')
+'use strict'
+
+const os = require('os')
+const fs = require('fs')
+const fse = require('../')
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-describe('native fs', function () {
- var TEST_DIR
+describe('native fs', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'native-fs')
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- it('should use native fs methods', function () {
- var file = path.join(TEST_DIR, 'write.txt')
+ it('should use native fs methods', () => {
+ const file = path.join(TEST_DIR, 'write.txt')
fse.writeFileSync(file, 'hello')
- var data = fse.readFileSync(file, 'utf8')
+ const data = fse.readFileSync(file, 'utf8')
assert.equal(data, 'hello')
})
- it('should have native fs constants', function () {
+ it('should have native fs constants', () => {
// Node.js v0.12 / IO.js
if ('F_OK' in fs) {
assert.equal(fse.F_OK, fs.F_OK)
diff --git a/lib/copy-sync/__tests__/broken-symlink.test.js b/lib/copy-sync/__tests__/broken-symlink.test.js
index 7ba184b..c5772ed 100644
--- a/lib/copy-sync/__tests__/broken-symlink.test.js
+++ b/lib/copy-sync/__tests__/broken-symlink.test.js
@@ -1,52 +1,48 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
-var copySync = require('../copy-sync')
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
+const copySync = require('../copy-sync')
/* global afterEach, beforeEach, describe, it */
-describe('copy-sync / broken symlink', function () {
- var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-broken-symlinks')
- var src = path.join(TEST_DIR, 'src')
- var out = path.join(TEST_DIR, 'out')
+describe('copy-sync / broken symlink', () => {
+ const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-broken-symlinks')
+ const src = path.join(TEST_DIR, 'src')
+ const out = path.join(TEST_DIR, 'out')
- beforeEach(function (done) {
- fse.emptyDir(TEST_DIR, function (err) {
+ beforeEach(done => {
+ fse.emptyDir(TEST_DIR, err => {
assert.ifError(err)
createFixtures(src, done)
})
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
-
- it('should copy broken symlinks by default', function () {
- assert.doesNotThrow(function () {
- copySync(src, out)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
+ it('should copy broken symlinks by default', () => {
+ assert.doesNotThrow(() => copySync(src, out))
assert.equal(fs.readlinkSync(path.join(out, 'broken-symlink')), path.join(src, 'does-not-exist'))
})
- it('should throw an error when dereference=true', function () {
- assert.throws(function () {
- copySync(src, out, {dereference: true})
- }, function (err) {
- return err.code === 'ENOENT'
- })
+ it('should throw an error when dereference=true', () => {
+ assert.throws(() => copySync(src, out, {dereference: true}), err => err.code === 'ENOENT')
})
})
function createFixtures (srcDir, callback) {
- fs.mkdir(srcDir, function (err) {
+ fs.mkdir(srcDir, err => {
+ let brokenFile
+ let brokenFileLink
+
if (err) return callback(err)
try {
- var brokenFile = path.join(srcDir, 'does-not-exist')
- var brokenFileLink = path.join(srcDir, 'broken-symlink')
+ brokenFile = path.join(srcDir, 'does-not-exist')
+ brokenFileLink = path.join(srcDir, 'broken-symlink')
fs.writeFileSync(brokenFile, 'does not matter')
fs.symlinkSync(brokenFile, brokenFileLink, 'file')
} catch (err) {
diff --git a/lib/copy-sync/__tests__/copy-sync-dir.test.js b/lib/copy-sync/__tests__/copy-sync-dir.test.js
index d457982..82c360e 100644
--- a/lib/copy-sync/__tests__/copy-sync-dir.test.js
+++ b/lib/copy-sync/__tests__/copy-sync-dir.test.js
@@ -1,84 +1,109 @@
-var assert = require('assert')
-var crypto = require('crypto')
-var os = require('os')
-var path = require('path')
-var fs = require(process.cwd())
+'use strict'
+
+const fs = require(process.cwd())
+const os = require('os')
+const path = require('path')
+const assert = require('assert')
+const crypto = require('crypto')
/* global beforeEach, describe, it */
-describe('+ copySync()', function () {
- var TEST_DIR
- var SIZE = 16 * 64 * 1024 + 7
- var src, dest
+describe('+ copySync()', () => {
+ const SIZE = 16 * 64 * 1024 + 7
+ let TEST_DIR
+ let src, dest
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-dir')
src = path.join(TEST_DIR, 'src')
dest = path.join(TEST_DIR, 'dest')
fs.emptyDir(TEST_DIR, done)
})
- describe('> when the source is a directory', function () {
- it('should copy the directory synchronously', function () {
- var FILES = 2
- var i, j
- var src = path.join(TEST_DIR, 'src')
- var dest = path.join(TEST_DIR, 'dest')
+ describe('> when the source is a directory', () => {
+ it('should copy the directory synchronously', () => {
+ const FILES = 2
+
+ src = path.join(TEST_DIR, 'src')
+ dest = path.join(TEST_DIR, 'dest')
- fs.mkdirsSync(src)
+ fs.mkdirSync(src)
- for (i = 0; i < FILES; ++i) {
+ for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(src, i.toString()), crypto.randomBytes(SIZE))
}
- var subdir = path.join(src, 'subdir')
+ const subdir = path.join(src, 'subdir')
- fs.mkdirsSync(subdir)
+ fs.mkdirSync(subdir)
- for (i = 0; i < FILES; ++i) {
+ for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(subdir, i.toString()), crypto.randomBytes(SIZE))
}
fs.copySync(src, dest)
assert(fs.existsSync(dest))
- for (i = 0; i < FILES; ++i) {
+ for (let i = 0; i < FILES; ++i) {
assert(fs.existsSync(path.join(dest, i.toString())))
}
- var destSub = path.join(dest, 'subdir')
- for (j = 0; j < FILES; ++j) {
+ const destSub = path.join(dest, 'subdir')
+ for (let j = 0; j < FILES; ++j) {
assert(fs.existsSync(path.join(destSub, j.toString())))
}
})
- it('should preserve symbolic links', function () {
- fs.mkdirsSync(src)
+ it('should preserve symbolic links', () => {
+ fs.mkdirSync(src)
fs.symlinkSync('destination', path.join(src, 'symlink'))
fs.copySync(src, dest)
- var link = fs.readlinkSync(path.join(dest, 'symlink'))
+ const link = fs.readlinkSync(path.join(dest, 'symlink'))
assert.strictEqual(link, 'destination')
})
- it('should should apply filter recursively', function () {
- var FILES = 2
- var filter = function (s) {
- // Don't match anything that ends with a digit higher than 0:
- return /(0|\D)$/i.test(s)
- }
+ describe('> when the destination dir does not exist', () => {
+ it('should create the destination directory and copy the file', () => {
+ const src = path.join(TEST_DIR, 'data/')
+ fs.mkdirSync(src)
+
+ const d1 = 'file1'
+ const d2 = 'file2'
+
+ fs.writeFileSync(path.join(src, 'f1.txt'), d1)
+ fs.writeFileSync(path.join(src, 'f2.txt'), d2)
+
+ const dest = path.join(TEST_DIR, 'this/path/does/not/exist/outputDir')
- fs.mkdirsSync(src)
+ fs.copySync(src, dest)
+
+ const o1 = fs.readFileSync(path.join(dest, 'f1.txt'), 'utf8')
+ const o2 = fs.readFileSync(path.join(dest, 'f2.txt'), 'utf8')
+
+ assert.strictEqual(d1, o1)
+ assert.strictEqual(d2, o2)
+ })
+ })
+ })
- for (var i = 0; i < FILES; ++i) {
+ describe('> when filter is used', () => {
+ it('should should apply filter recursively', () => {
+ const FILES = 2
+ // Don't match anything that ends with a digit higher than 0:
+ const filter = s => /(0|\D)$/i.test(s)
+
+ fs.mkdirSync(src)
+
+ for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(src, i.toString()), crypto.randomBytes(SIZE))
}
- var subdir = path.join(src, 'subdir')
- fs.mkdirsSync(subdir)
+ const subdir = path.join(src, 'subdir')
+ fs.mkdirSync(subdir)
- for (i = 0; i < FILES; ++i) {
+ for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(subdir, i.toString()), crypto.randomBytes(SIZE))
}
@@ -87,7 +112,7 @@ describe('+ copySync()', function () {
assert(fs.existsSync(dest))
assert(FILES > 1)
- for (i = 0; i < FILES; ++i) {
+ for (let i = 0; i < FILES; ++i) {
if (i === 0) {
assert(fs.existsSync(path.join(dest, i.toString())))
} else {
@@ -95,9 +120,9 @@ describe('+ copySync()', function () {
}
}
- var destSub = path.join(dest, 'subdir')
+ const destSub = path.join(dest, 'subdir')
- for (var j = 0; j < FILES; ++j) {
+ for (let j = 0; j < FILES; ++j) {
if (j === 0) {
assert(fs.existsSync(path.join(destSub, j.toString())))
} else {
@@ -106,16 +131,14 @@ describe('+ copySync()', function () {
}
})
- it('should apply the filter to directory names', function () {
- var IGNORE = 'ignore'
- var filter = function (p) {
- return !~p.indexOf(IGNORE)
- }
+ it('should apply the filter to directory names', () => {
+ const IGNORE = 'ignore'
+ const filter = p => !~p.indexOf(IGNORE)
- fs.mkdirsSync(src)
+ fs.mkdirSync(src)
- var ignoreDir = path.join(src, IGNORE)
- fs.mkdirsSync(ignoreDir)
+ const ignoreDir = path.join(src, IGNORE)
+ fs.mkdirSync(ignoreDir)
fs.writeFileSync(path.join(ignoreDir, 'file'), crypto.randomBytes(SIZE))
@@ -125,27 +148,57 @@ describe('+ copySync()', function () {
assert(!fs.existsSync(path.join(dest, IGNORE, 'file')), 'file was not ignored')
})
- describe('> when the destination dir does not exist', function () {
- it('should create the destination directory and copy the file', function () {
- var src = path.join(TEST_DIR, 'data/')
+ it('should apply filter when it is applied only to dest', done => {
+ const timeCond = new Date().getTime()
+
+ const filter = (s, d) => fs.statSync(d).birthtime.getTime() < timeCond
+
+ const dest = path.join(TEST_DIR, 'dest')
+
+ setTimeout(() => {
fs.mkdirSync(src)
+ fs.writeFileSync(path.join(src, 'somefile.html'), 'some data')
+ fs.mkdirSync(dest)
+ try {
+ fs.copySync(src, dest, filter)
+ } catch (err) {
+ assert.ifError(err)
+ }
+ assert(!fs.existsSync(path.join(dest, 'somefile.html')))
+ done()
+ }, 1000)
+ })
- var d1 = 'file1'
- var d2 = 'file2'
+ it('should apply filter when it is applied to both src and dest', done => {
+ const timeCond = new Date().getTime()
+ const filter = (s, d) => s.split('.').pop() !== 'css' && fs.statSync(path.dirname(d)).birthtime.getTime() > timeCond
- fs.writeFileSync(path.join(src, 'f1.txt'), d1)
- fs.writeFileSync(path.join(src, 'f2.txt'), d2)
+ const dest = path.join(TEST_DIR, 'dest')
- var dest = path.join(TEST_DIR, 'this/path/does/not/exist/outputDir')
+ setTimeout(() => {
+ const srcFile1 = path.join(TEST_DIR, '1.html')
+ const srcFile2 = path.join(TEST_DIR, '2.css')
+ const srcFile3 = path.join(TEST_DIR, '3.jade')
- fs.copySync(src, dest)
+ fs.writeFileSync(srcFile1, '')
+ fs.writeFileSync(srcFile2, '')
+ fs.writeFileSync(srcFile3, '')
- var o1 = fs.readFileSync(path.join(dest, 'f1.txt'), 'utf8')
- var o2 = fs.readFileSync(path.join(dest, 'f2.txt'), 'utf8')
+ const destFile1 = path.join(dest, 'dest1.html')
+ const destFile2 = path.join(dest, 'dest2.css')
+ const destFile3 = path.join(dest, 'dest3.jade')
- assert.strictEqual(d1, o1)
- assert.strictEqual(d2, o2)
- })
+ fs.mkdirSync(dest)
+
+ fs.copySync(srcFile1, destFile1, filter)
+ fs.copySync(srcFile2, destFile2, filter)
+ fs.copySync(srcFile3, destFile3, filter)
+
+ assert(fs.existsSync(destFile1))
+ assert(!fs.existsSync(destFile2))
+ assert(fs.existsSync(destFile3))
+ done()
+ }, 1000)
})
})
})
diff --git a/lib/copy-sync/__tests__/copy-sync-file.test.js b/lib/copy-sync/__tests__/copy-sync-file.test.js
index 70e2e5c..2874c12 100644
--- a/lib/copy-sync/__tests__/copy-sync-file.test.js
+++ b/lib/copy-sync/__tests__/copy-sync-file.test.js
@@ -1,34 +1,34 @@
-var assert = require('assert')
-var crypto = require('crypto')
-var os = require('os')
-var path = require('path')
-var fs = require(process.cwd())
+'use strict'
+
+const fs = require(process.cwd())
+const os = require('os')
+const path = require('path')
+const assert = require('assert')
+const crypto = require('crypto')
/* global afterEach, beforeEach, describe, it */
-var SIZE = 16 * 64 * 1024 + 7
+const SIZE = 16 * 64 * 1024 + 7
-describe('+ copySync()', function () {
- var TEST_DIR
+describe('+ copySync()', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync')
fs.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fs.remove(TEST_DIR, done)
- })
+ afterEach(done => fs.remove(TEST_DIR, done))
- describe('> when the source is a file', function () {
- it('should copy the file synchronously', function () {
- var fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src')
- var fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
+ describe('> when the source is a file', () => {
+ it('should copy the file synchronously', () => {
+ const fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src')
+ const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
fs.writeFileSync(fileSrc, crypto.randomBytes(SIZE))
- var srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest('hex')
- var destMd5 = ''
+ const srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest('hex')
+ let destMd5 = ''
fs.copySync(fileSrc, fileDest)
@@ -36,15 +36,15 @@ describe('+ copySync()', function () {
assert.strictEqual(srcMd5, destMd5)
})
- it('should follow symlinks', function () {
- var fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src')
- var fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
- var linkSrc = path.join(TEST_DIR, 'TEST_fs-extra_copy_link')
+ it('should follow symlinks', () => {
+ const fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src')
+ const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
+ const linkSrc = path.join(TEST_DIR, 'TEST_fs-extra_copy_link')
fs.writeFileSync(fileSrc, crypto.randomBytes(SIZE))
- var srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest('hex')
- var destMd5 = ''
+ const srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest('hex')
+ let destMd5 = ''
fs.symlinkSync(fileSrc, linkSrc)
fs.copySync(linkSrc, fileDest)
@@ -52,33 +52,33 @@ describe('+ copySync()', function () {
assert.strictEqual(srcMd5, destMd5)
})
- it('should maintain file mode', function () {
- var fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src')
- var fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
+ it('should maintain file mode', () => {
+ const fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src')
+ const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
fs.writeFileSync(fileSrc, crypto.randomBytes(SIZE))
fs.chmodSync(fileSrc, parseInt('750', 8))
fs.copySync(fileSrc, fileDest)
- var statSrc = fs.statSync(fileSrc)
- var statDest = fs.statSync(fileDest)
+ const statSrc = fs.statSync(fileSrc)
+ const statDest = fs.statSync(fileDest)
assert.strictEqual(statSrc.mode, statDest.mode)
})
- it('should only copy files allowed by filter fn', function () {
- var srcFile1 = path.join(TEST_DIR, '1.html')
- var srcFile2 = path.join(TEST_DIR, '2.css')
- var srcFile3 = path.join(TEST_DIR, '3.jade')
+ it('should only copy files allowed by filter fn', () => {
+ const srcFile1 = path.join(TEST_DIR, '1.html')
+ const srcFile2 = path.join(TEST_DIR, '2.css')
+ const srcFile3 = path.join(TEST_DIR, '3.jade')
fs.writeFileSync(srcFile1, '')
fs.writeFileSync(srcFile2, '')
fs.writeFileSync(srcFile3, '')
- var destFile1 = path.join(TEST_DIR, 'dest1.html')
- var destFile2 = path.join(TEST_DIR, 'dest2.css')
- var destFile3 = path.join(TEST_DIR, 'dest3.jade')
+ const destFile1 = path.join(TEST_DIR, 'dest1.html')
+ const destFile2 = path.join(TEST_DIR, 'dest2.css')
+ const destFile3 = path.join(TEST_DIR, 'dest3.jade')
- var filter = function (s) { return s.split('.').pop() !== 'css' }
+ const filter = s => s.split('.').pop() !== 'css'
fs.copySync(srcFile1, destFile1, filter)
fs.copySync(srcFile2, destFile2, filter)
@@ -89,43 +89,43 @@ describe('+ copySync()', function () {
assert(fs.existsSync(destFile3))
})
- describe('> when the destination dir does not exist', function () {
- it('should create the destination directory and copy the file', function () {
- var src = path.join(TEST_DIR, 'file.txt')
- var dest = path.join(TEST_DIR, 'this/path/does/not/exist/copied.txt')
- var data = 'did it copy?\n'
+ describe('> when the destination dir does not exist', () => {
+ it('should create the destination directory and copy the file', () => {
+ const src = path.join(TEST_DIR, 'file.txt')
+ const dest = path.join(TEST_DIR, 'this/path/does/not/exist/copied.txt')
+ const data = 'did it copy?\n'
fs.writeFileSync(src, data, 'utf8')
fs.copySync(src, dest)
- var data2 = fs.readFileSync(dest, 'utf8')
+ const data2 = fs.readFileSync(dest, 'utf8')
assert.strictEqual(data, data2)
})
})
- describe('> when the source file does not have write permissions', function () {
- it('should be able to copy contents of file', function () {
- var fileSrc = path.join(TEST_DIR, 'file.txt')
- var fileDest = path.join(TEST_DIR, 'file-copy.txt')
- var data = 'did it copy?'
+ describe('> when the source file does not have write permissions', () => {
+ it('should be able to copy contents of file', () => {
+ const fileSrc = path.join(TEST_DIR, 'file.txt')
+ const fileDest = path.join(TEST_DIR, 'file-copy.txt')
+ const data = 'did it copy?'
fs.writeFileSync(fileSrc, data, 'utf8')
fs.chmodSync(fileSrc, '0444')
fs.copySync(fileSrc, fileDest)
- var data2 = fs.readFileSync(fileDest, 'utf8')
+ const data2 = fs.readFileSync(fileDest, 'utf8')
assert.strictEqual(data, data2)
})
})
- describe('> when overwrite option is passed', function () {
- var src, dest
- var srcData = 'some src data'
+ describe('> when overwrite option is passed', () => {
+ const srcData = 'some src data'
+ let src, dest
- beforeEach(function () {
+ beforeEach(() => {
src = path.join(TEST_DIR, 'src-file')
dest = path.join(TEST_DIR, 'des-file')
@@ -133,60 +133,59 @@ describe('+ copySync()', function () {
fs.writeFileSync(src, srcData)
})
- describe('> when destination file does NOT exist', function () {
- describe('> when overwrite is true', function () {
- it('should copy the file and not throw an error', function () {
+ describe('> when destination file does NOT exist', () => {
+ describe('> when overwrite is true', () => {
+ it('should copy the file and not throw an error', () => {
fs.copySync(src, dest, {overwrite: true})
- var destData = fs.readFileSync(dest, 'utf8')
+ const destData = fs.readFileSync(dest, 'utf8')
assert.strictEqual(srcData, destData)
})
})
- describe('> when overwrite is false', function () {
- it('should copy the file and not throw an error', function () {
+ describe('> when overwrite is false', () => {
+ it('should copy the file and not throw an error', () => {
fs.copySync(src, dest, {overwrite: false})
- var destData = fs.readFileSync(dest, 'utf8')
+ const destData = fs.readFileSync(dest, 'utf8')
assert.strictEqual(srcData, destData)
})
})
})
- describe('when destination file does exist', function () {
- var destData
- beforeEach(function () {
+ describe('when destination file does exist', () => {
+ let destData
+
+ beforeEach(() => {
destData = 'some dest data'
fs.writeFileSync(dest, destData)
})
- describe('> when overwrite is true', function () {
- it('should copy the file and not throw an error', function () {
+ describe('> when overwrite is true', () => {
+ it('should copy the file and not throw an error', () => {
fs.copySync(src, dest, {overwrite: true})
destData = fs.readFileSync(dest, 'utf8')
assert.strictEqual(srcData, destData)
})
})
- describe('> when overwrite is false', function () {
- it('should not throw an error', function () {
+ describe('> when overwrite is false', () => {
+ it('should not throw an error', () => {
fs.copySync(src, dest, {overwrite: false})
// copy never happened
- var destDataNew = fs.readFileSync(dest, 'utf8')
+ const destDataNew = fs.readFileSync(dest, 'utf8')
assert.strictEqual(destData, destDataNew)
})
- it('should throw an error when errorOnExist is true', function () {
- assert.throws(function () {
- fs.copySync(src, dest, {overwrite: false, errorOnExist: true})
- })
+ it('should throw an error when errorOnExist is true', () => {
+ assert.throws(() => fs.copySync(src, dest, {overwrite: false, errorOnExist: true}))
// copy never happened
- var destDataNew = fs.readFileSync(dest, 'utf8')
+ const destDataNew = fs.readFileSync(dest, 'utf8')
assert.strictEqual(destData, destDataNew)
})
})
- describe('> when overwrite is true and dest is readonly', function () {
- it('should copy the file and not throw an error', function () {
+ describe('> when overwrite is true and dest is readonly', () => {
+ it('should copy the file and not throw an error', () => {
try {
fs.chmodSync(dest, parseInt('444', 8))
fs.copySync(src, dest, {overwrite: true})
@@ -200,10 +199,10 @@ describe('+ copySync()', function () {
})
})
})
- describe('clobber', function () {
- var src, dest, srcData, destData
+ describe('clobber', () => {
+ let src, dest, srcData, destData
- beforeEach(function () {
+ beforeEach(() => {
src = path.join(TEST_DIR, 'src-file')
dest = path.join(TEST_DIR, 'des-file')
srcData = 'some src data'
@@ -212,11 +211,11 @@ describe('+ copySync()', function () {
fs.writeFileSync(dest, destData)
})
- it('is an alias for overwrite', function () {
+ it('is an alias for overwrite', () => {
fs.copySync(src, dest, {clobber: false})
// copy never happened
- var destDataNew = fs.readFileSync(dest, 'utf8')
+ const destDataNew = fs.readFileSync(dest, 'utf8')
assert.strictEqual(destData, destDataNew)
})
})
diff --git a/lib/copy-sync/__tests__/copy-sync-preserve-time.test.js b/lib/copy-sync/__tests__/copy-sync-preserve-time.test.js
index 8028b73..8c4bfa9 100644
--- a/lib/copy-sync/__tests__/copy-sync-preserve-time.test.js
+++ b/lib/copy-sync/__tests__/copy-sync-preserve-time.test.js
@@ -1,35 +1,37 @@
-var assert = require('assert')
-var fs = require('fs')
-var os = require('os')
-var path = require('path')
-var copySync = require('../copy-sync')
-var utimes = require('../../util/utimes')
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const path = require('path')
+const utimes = require('../../util/utimes')
+const assert = require('assert')
+const copySync = require('../copy-sync')
/* global beforeEach, describe, it */
-describe('copy', function () {
- var TEST_DIR
+describe('copy', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-preserve-time')
require(process.cwd()).emptyDir(TEST_DIR, done)
})
- describe('> modification option', function () {
- var SRC_FIXTURES_DIR = path.join(__dirname, './fixtures')
- var FILES = ['a-file', path.join('a-folder', 'another-file'), path.join('a-folder', 'another-folder', 'file3')]
+ describe('> modification option', () => {
+ const SRC_FIXTURES_DIR = path.join(__dirname, './fixtures')
+ const FILES = ['a-file', path.join('a-folder', 'another-file'), path.join('a-folder', 'another-folder', 'file3')]
- describe('> when modified option is turned off', function () {
- it('should have different timestamps on copy', function () {
- var from = path.join(SRC_FIXTURES_DIR)
+ describe('> when modified option is turned off', () => {
+ it('should have different timestamps on copy', () => {
+ const from = path.join(SRC_FIXTURES_DIR)
copySync(from, TEST_DIR, {preserveTimestamps: false})
FILES.forEach(testFile({preserveTimestamps: false}))
})
})
- describe('> when modified option is turned on', function () {
- it('should have the same timestamps on copy', function () {
- var from = path.join(SRC_FIXTURES_DIR)
+ describe('> when modified option is turned on', () => {
+ it('should have the same timestamps on copy', () => {
+ const from = path.join(SRC_FIXTURES_DIR)
copySync(from, TEST_DIR, {preserveTimestamps: true})
FILES.forEach(testFile({preserveTimestamps: true}))
})
@@ -37,10 +39,10 @@ describe('copy', function () {
function testFile (options) {
return function (file) {
- var a = path.join(SRC_FIXTURES_DIR, file)
- var b = path.join(TEST_DIR, file)
- var fromStat = fs.statSync(a)
- var toStat = fs.statSync(b)
+ const a = path.join(SRC_FIXTURES_DIR, file)
+ const b = path.join(TEST_DIR, file)
+ const fromStat = fs.statSync(a)
+ const toStat = fs.statSync(b)
if (options.preserveTimestamps) {
// https://github.com/nodejs/io.js/issues/2069
if (process.platform !== 'win32') {
diff --git a/lib/copy-sync/__tests__/symlink.test.js b/lib/copy-sync/__tests__/symlink.test.js
index 2890583..3764e59 100644
--- a/lib/copy-sync/__tests__/symlink.test.js
+++ b/lib/copy-sync/__tests__/symlink.test.js
@@ -1,30 +1,32 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
-var copySync = require('../copy-sync')
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
+const copySync = require('../copy-sync')
/* global afterEach, beforeEach, describe, it */
-describe('copy-sync / symlink', function () {
- var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-symlinks')
- var src = path.join(TEST_DIR, 'src')
- var out = path.join(TEST_DIR, 'out')
+describe('copy-sync / symlink', () => {
+ const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-symlinks')
+ const src = path.join(TEST_DIR, 'src')
+ const out = path.join(TEST_DIR, 'out')
- beforeEach(function (done) {
- fse.emptyDir(TEST_DIR, function (err) {
+ beforeEach(done => {
+ fse.emptyDir(TEST_DIR, err => {
assert.ifError(err)
createFixtures(src, done)
})
})
- afterEach(function (done) {
+ afterEach(done => {
fse.remove(TEST_DIR, done)
})
- it('copies symlinks by default', function () {
- assert.doesNotThrow(function () {
+ it('copies symlinks by default', () => {
+ assert.doesNotThrow(() => {
copySync(src, out)
})
@@ -32,38 +34,38 @@ describe('copy-sync / symlink', function () {
assert.equal(fs.readlinkSync(path.join(out, 'dir-symlink')), path.join(src, 'dir'))
})
- it('copies file contents when dereference=true', function () {
+ it('copies file contents when dereference=true', () => {
try {
copySync(src, out, {dereference: true})
} catch (err) {
assert.ifError(err)
}
- var fileSymlinkPath = path.join(out, 'file-symlink')
+ const fileSymlinkPath = path.join(out, 'file-symlink')
assert.ok(fs.lstatSync(fileSymlinkPath).isFile())
assert.equal(fs.readFileSync(fileSymlinkPath), 'foo contents')
- var dirSymlinkPath = path.join(out, 'dir-symlink')
+ const dirSymlinkPath = path.join(out, 'dir-symlink')
assert.ok(fs.lstatSync(dirSymlinkPath).isDirectory())
assert.deepEqual(fs.readdirSync(dirSymlinkPath), ['bar'])
})
})
function createFixtures (srcDir, callback) {
- fs.mkdir(srcDir, function (err) {
+ fs.mkdir(srcDir, err => {
if (err) return callback(err)
// note: third parameter in symlinkSync is type e.g. 'file' or 'dir'
// https://nodejs.org/api/fs.html#fs_fs_symlink_srcpath_dstpath_type_callback
try {
- var fooFile = path.join(srcDir, 'foo')
- var fooFileLink = path.join(srcDir, 'file-symlink')
+ const fooFile = path.join(srcDir, 'foo')
+ const fooFileLink = path.join(srcDir, 'file-symlink')
fs.writeFileSync(fooFile, 'foo contents')
fs.symlinkSync(fooFile, fooFileLink, 'file')
- var dir = path.join(srcDir, 'dir')
- var dirFile = path.join(dir, 'bar')
- var dirLink = path.join(srcDir, 'dir-symlink')
+ const dir = path.join(srcDir, 'dir')
+ const dirFile = path.join(dir, 'bar')
+ const dirLink = path.join(srcDir, 'dir-symlink')
fs.mkdirSync(dir)
fs.writeFileSync(dirFile, 'bar contents')
fs.symlinkSync(dir, dirLink, 'dir')
diff --git a/lib/copy-sync/copy-file-sync.js b/lib/copy-sync/copy-file-sync.js
index fb9fad6..4e4cd3f 100644
--- a/lib/copy-sync/copy-file-sync.js
+++ b/lib/copy-sync/copy-file-sync.js
@@ -1,26 +1,28 @@
-var fs = require('graceful-fs')
+'use strict'
-var BUF_LENGTH = 64 * 1024
-var _buff = new Buffer(BUF_LENGTH)
+const fs = require('graceful-fs')
+
+const BUF_LENGTH = 64 * 1024
+const _buff = new Buffer(BUF_LENGTH)
function copyFileSync (srcFile, destFile, options) {
- var overwrite = options.overwrite
- var errorOnExist = options.errorOnExist
- var preserveTimestamps = options.preserveTimestamps
+ const overwrite = options.overwrite
+ const errorOnExist = options.errorOnExist
+ const preserveTimestamps = options.preserveTimestamps
if (fs.existsSync(destFile)) {
if (overwrite) {
fs.unlinkSync(destFile)
} else if (errorOnExist) {
- throw new Error(destFile + ' already exists')
+ throw new Error(`${destFile} already exists`)
} else return
}
- var fdr = fs.openSync(srcFile, 'r')
- var stat = fs.fstatSync(fdr)
- var fdw = fs.openSync(destFile, 'w', stat.mode)
- var bytesRead = 1
- var pos = 0
+ const fdr = fs.openSync(srcFile, 'r')
+ const stat = fs.fstatSync(fdr)
+ const fdw = fs.openSync(destFile, 'w', stat.mode)
+ let bytesRead = 1
+ let pos = 0
while (bytesRead > 0) {
bytesRead = fs.readSync(fdr, _buff, 0, BUF_LENGTH, pos)
diff --git a/lib/copy-sync/copy-sync.js b/lib/copy-sync/copy-sync.js
index 29812f8..9d5639c 100644
--- a/lib/copy-sync/copy-sync.js
+++ b/lib/copy-sync/copy-sync.js
@@ -1,7 +1,9 @@
-var fs = require('graceful-fs')
-var path = require('path')
-var copyFileSync = require('./copy-file-sync')
-var mkdir = require('../mkdirs')
+'use strict'
+
+const fs = require('graceful-fs')
+const path = require('path')
+const copyFileSync = require('./copy-file-sync')
+const mkdir = require('../mkdirs')
function copySync (src, dest, options) {
if (typeof options === 'function' || options instanceof RegExp) {
@@ -22,19 +24,19 @@ function copySync (src, dest, options) {
// Warn about using preserveTimestamps on 32-bit node:
if (options.preserveTimestamps && process.arch === 'ia32') {
- console.warn('fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n' +
- 'see https://github.com/jprichardson/node-fs-extra/issues/269')
+ console.warn(`fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n
+ see https://github.com/jprichardson/node-fs-extra/issues/269`)
}
- var stats = (options.recursive && !options.dereference) ? fs.lstatSync(src) : fs.statSync(src)
- var destFolder = path.dirname(dest)
- var destFolderExists = fs.existsSync(destFolder)
- var performCopy = false
+ const stats = (options.recursive && !options.dereference) ? fs.lstatSync(src) : fs.statSync(src)
+ const destFolder = path.dirname(dest)
+ const destFolderExists = fs.existsSync(destFolder)
+ let performCopy = false
if (options.filter instanceof RegExp) {
console.warn('Warning: fs-extra: Passing a RegExp filter is deprecated, use a function')
performCopy = options.filter.test(src)
- } else if (typeof options.filter === 'function') performCopy = options.filter(src)
+ } else if (typeof options.filter === 'function') performCopy = options.filter(src, dest)
if (stats.isFile() && performCopy) {
if (!destFolderExists) mkdir.mkdirsSync(destFolder)
@@ -45,14 +47,14 @@ function copySync (src, dest, options) {
})
} else if (stats.isDirectory() && performCopy) {
if (!fs.existsSync(dest)) mkdir.mkdirsSync(dest)
- var contents = fs.readdirSync(src)
- contents.forEach(function (content) {
- var opts = options
+ const contents = fs.readdirSync(src)
+ contents.forEach(content => {
+ const opts = options
opts.recursive = true
copySync(path.join(src, content), path.join(dest, content), opts)
})
} else if (options.recursive && stats.isSymbolicLink() && performCopy) {
- var srcPath = fs.readlinkSync(src)
+ const srcPath = fs.readlinkSync(src)
fs.symlinkSync(srcPath, dest)
}
}
diff --git a/lib/copy/__tests__/async/copy-gh-89.test.js b/lib/copy/__tests__/async/copy-gh-89.test.js
index 4ff9269..4cfe191 100644
--- a/lib/copy/__tests__/async/copy-gh-89.test.js
+++ b/lib/copy/__tests__/async/copy-gh-89.test.js
@@ -1,46 +1,48 @@
+'use strict'
+
// relevant: https://github.com/jprichardson/node-fs-extra/issues/89
// come up with better file name
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-describe('copy / gh #89', function () {
- var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-gh-89')
+describe('copy / gh #89', () => {
+ const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-gh-89')
- beforeEach(function (done) {
+ beforeEach(done => {
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
+ afterEach(done => {
fse.remove(TEST_DIR, done)
})
- it('should...', function (done) {
- var A = path.join(TEST_DIR, 'A')
- var B = path.join(TEST_DIR, 'B')
+ it('should...', done => {
+ const A = path.join(TEST_DIR, 'A')
+ const B = path.join(TEST_DIR, 'B')
fs.mkdirSync(A)
fs.mkdirSync(B)
- var one = path.join(A, 'one.txt')
- var two = path.join(A, 'two.txt')
- var three = path.join(B, 'three.txt')
- var four = path.join(B, 'four.txt')
+ const one = path.join(A, 'one.txt')
+ const two = path.join(A, 'two.txt')
+ const three = path.join(B, 'three.txt')
+ const four = path.join(B, 'four.txt')
fs.writeFileSync(one, '1')
fs.writeFileSync(two, '2')
fs.writeFileSync(three, '3')
fs.writeFileSync(four, '4')
- var C = path.join(TEST_DIR, 'C')
- fse.copy(A, C, function (err) {
+ const C = path.join(TEST_DIR, 'C')
+ fse.copy(A, C, err => {
if (err) return done(err)
- fse.copy(B, C, function (err) {
+ fse.copy(B, C, err => {
if (err) return done(err)
assert(fs.existsSync(path.join(C, 'one.txt')))
diff --git a/lib/copy/__tests__/copy-dev-null.test.js b/lib/copy/__tests__/copy-dev-null.test.js
index dfdfea5..77482f0 100644
--- a/lib/copy/__tests__/copy-dev-null.test.js
+++ b/lib/copy/__tests__/copy-dev-null.test.js
@@ -1,31 +1,33 @@
-var assert = require('assert')
-var fs = require('fs')
-var os = require('os')
-var path = require('path')
-var fse = require('../../')
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require('../../')
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-var TEST_DIR = ''
+let TEST_DIR = ''
-describe('fs-extra', function () {
- beforeEach(function (done) {
+describe('+ copy() - copy /dev/null', () => {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'test', 'fs-extra', 'copy-dev-null')
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- describe('+ copy()', function () {
- it('should error', function (done) {
+ describe('> when src is /dev/null', () => {
+ it('should copy successfully', done => {
// no /dev/null on windows
if (process.platform === 'win32') return done()
- var tmpFile = path.join(TEST_DIR, 'foo')
- fse.copy('/dev/null', tmpFile, function (err) {
+
+ const tmpFile = path.join(TEST_DIR, 'foo')
+
+ fse.copy('/dev/null', tmpFile, err => {
assert.ifError(err)
- var stats = fs.lstatSync(tmpFile)
+ const stats = fs.lstatSync(tmpFile)
assert.strictEqual(stats.size, 0)
done()
})
diff --git a/lib/copy/__tests__/copy-permissions.test.js b/lib/copy/__tests__/copy-permissions.test.js
index 16516d6..ccc89cd 100644
--- a/lib/copy/__tests__/copy-permissions.test.js
+++ b/lib/copy/__tests__/copy-permissions.test.js
@@ -1,36 +1,38 @@
-var assert = require('assert')
-var fs = require('fs')
-var os = require('os')
-var path = require('path')
-var fse = require('../../')
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require('../../')
+const path = require('path')
+const assert = require('assert')
/* global beforeEach, describe, it */
-var o777 = parseInt('777', 8)
-var o666 = parseInt('666', 8)
-var o444 = parseInt('444', 8)
+const o777 = parseInt('777', 8)
+const o666 = parseInt('666', 8)
+const o444 = parseInt('444', 8)
-describe('copy', function () {
- var TEST_DIR
+describe('copy', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy')
fse.emptyDir(TEST_DIR, done)
})
// pretty UNIX specific, may not pass on windows... only tested on Mac OS X 10.9
- it('should maintain file permissions and ownership', function (done) {
+ it('should maintain file permissions and ownership', done => {
if (process.platform === 'win32') return done()
// var userid = require('userid')
// http://man7.org/linux/man-pages/man2/stat.2.html
- var S_IFREG = parseInt('0100000', 8) // regular file
- var S_IFDIR = parseInt('0040000', 8) // directory
+ const S_IFREG = parseInt('0100000', 8) // regular file
+ const S_IFDIR = parseInt('0040000', 8) // directory
// these are Mac specific I think (at least staff), should find Linux equivalent
- var gidWheel
- var gidStaff
+ let gidWheel
+ let gidStaff
try {
gidWheel = process.getgid() // userid.gid('wheel')
@@ -44,48 +46,48 @@ describe('copy', function () {
gidStaff = process.getgid()
}
- var permDir = path.join(TEST_DIR, 'perms')
+ const permDir = path.join(TEST_DIR, 'perms')
fs.mkdirSync(permDir)
- var srcDir = path.join(permDir, 'src')
+ const srcDir = path.join(permDir, 'src')
fs.mkdirSync(srcDir)
- var f1 = path.join(srcDir, 'f1.txt')
+ const f1 = path.join(srcDir, 'f1.txt')
fs.writeFileSync(f1, '')
fs.chmodSync(f1, o666)
fs.chownSync(f1, process.getuid(), gidWheel)
- var f1stats = fs.lstatSync(f1)
+ const f1stats = fs.lstatSync(f1)
assert.strictEqual(f1stats.mode - S_IFREG, o666)
- var d1 = path.join(srcDir, 'somedir')
+ const d1 = path.join(srcDir, 'somedir')
fs.mkdirSync(d1)
fs.chmodSync(d1, o777)
fs.chownSync(d1, process.getuid(), gidStaff)
- var d1stats = fs.lstatSync(d1)
+ const d1stats = fs.lstatSync(d1)
assert.strictEqual(d1stats.mode - S_IFDIR, o777)
- var f2 = path.join(d1, 'f2.bin')
+ const f2 = path.join(d1, 'f2.bin')
fs.writeFileSync(f2, '')
fs.chmodSync(f2, o777)
fs.chownSync(f2, process.getuid(), gidStaff)
- var f2stats = fs.lstatSync(f2)
+ const f2stats = fs.lstatSync(f2)
assert.strictEqual(f2stats.mode - S_IFREG, o777)
- var d2 = path.join(srcDir, 'crazydir')
+ const d2 = path.join(srcDir, 'crazydir')
fs.mkdirSync(d2)
fs.chmodSync(d2, o444)
fs.chownSync(d2, process.getuid(), gidWheel)
- var d2stats = fs.lstatSync(d2)
+ const d2stats = fs.lstatSync(d2)
assert.strictEqual(d2stats.mode - S_IFDIR, o444)
- var destDir = path.join(permDir, 'dest')
- fse.copy(srcDir, destDir, function (err) {
+ const destDir = path.join(permDir, 'dest')
+ fse.copy(srcDir, destDir, err => {
assert.ifError(err)
- var newf1stats = fs.lstatSync(path.join(permDir, 'dest/f1.txt'))
- var newd1stats = fs.lstatSync(path.join(permDir, 'dest/somedir'))
- var newf2stats = fs.lstatSync(path.join(permDir, 'dest/somedir/f2.bin'))
- var newd2stats = fs.lstatSync(path.join(permDir, 'dest/crazydir'))
+ const newf1stats = fs.lstatSync(path.join(permDir, 'dest/f1.txt'))
+ const newd1stats = fs.lstatSync(path.join(permDir, 'dest/somedir'))
+ const newf2stats = fs.lstatSync(path.join(permDir, 'dest/somedir/f2.bin'))
+ const newd2stats = fs.lstatSync(path.join(permDir, 'dest/crazydir'))
assert.strictEqual(newf1stats.mode, f1stats.mode)
assert.strictEqual(newd1stats.mode, d1stats.mode)
diff --git a/lib/copy/__tests__/copy-preserve-time.test.js b/lib/copy/__tests__/copy-preserve-time.test.js
index 9de5e0c..1c9fcb0 100644
--- a/lib/copy/__tests__/copy-preserve-time.test.js
+++ b/lib/copy/__tests__/copy-preserve-time.test.js
@@ -1,42 +1,44 @@
-var assert = require('assert')
-var fs = require('fs')
-var os = require('os')
-var path = require('path')
-var copy = require('../copy')
-var utimes = require('../../util/utimes')
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const path = require('path')
+const copy = require('../copy')
+const utimes = require('../../util/utimes')
+const assert = require('assert')
/* global beforeEach, describe, it */
-describe('copy', function () {
- var TEST_DIR
+describe('copy', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy')
require(process.cwd()).emptyDir(TEST_DIR, done)
})
- describe('> modification option', function () {
- var SRC_FIXTURES_DIR = path.join(__dirname, '/fixtures')
- var FILES = ['a-file', path.join('a-folder', 'another-file'), path.join('a-folder', 'another-folder', 'file3')]
+ describe('> modification option', () => {
+ const SRC_FIXTURES_DIR = path.join(__dirname, '/fixtures')
+ const FILES = ['a-file', path.join('a-folder', 'another-file'), path.join('a-folder', 'another-folder', 'file3')]
- describe('> when modified option is turned off', function () {
- it('should have different timestamps on copy', function (done) {
- var from = path.join(SRC_FIXTURES_DIR)
- var to = path.join(TEST_DIR)
+ describe('> when modified option is turned off', () => {
+ it('should have different timestamps on copy', done => {
+ const from = path.join(SRC_FIXTURES_DIR)
+ const to = path.join(TEST_DIR)
- copy(from, to, {preserveTimestamps: false}, function () {
+ copy(from, to, {preserveTimestamps: false}, () => {
FILES.forEach(testFile({preserveTimestamps: false}))
done()
})
})
})
- describe('> when modified option is turned on', function () {
- it('should have the same timestamps on copy', function (done) {
- var from = path.join(SRC_FIXTURES_DIR)
- var to = path.join(TEST_DIR)
+ describe('> when modified option is turned on', () => {
+ it('should have the same timestamps on copy', done => {
+ const from = path.join(SRC_FIXTURES_DIR)
+ const to = path.join(TEST_DIR)
- copy(from, to, {preserveTimestamps: true}, function () {
+ copy(from, to, {preserveTimestamps: true}, () => {
FILES.forEach(testFile({preserveTimestamps: true}))
done()
})
@@ -45,10 +47,10 @@ describe('copy', function () {
function testFile (options) {
return function (file) {
- var a = path.join(SRC_FIXTURES_DIR, file)
- var b = path.join(TEST_DIR, file)
- var fromStat = fs.statSync(a)
- var toStat = fs.statSync(b)
+ const a = path.join(SRC_FIXTURES_DIR, file)
+ const b = path.join(TEST_DIR, file)
+ const fromStat = fs.statSync(a)
+ const toStat = fs.statSync(b)
if (options.preserveTimestamps) {
// https://github.com/nodejs/io.js/issues/2069
if (process.platform !== 'win32') {
diff --git a/lib/copy/__tests__/copy.test.js b/lib/copy/__tests__/copy.test.js
index 5c1efbc..6ed64ff 100644
--- a/lib/copy/__tests__/copy.test.js
+++ b/lib/copy/__tests__/copy.test.js
@@ -1,44 +1,44 @@
-var assert = require('assert')
-var crypto = require('crypto')
-var fs = require('fs')
-var os = require('os')
-var path = require('path')
-var fse = require('../../')
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require('../../')
+const path = require('path')
+const assert = require('assert')
+const crypto = require('crypto')
/* global afterEach, beforeEach, describe, it */
-var SIZE = 16 * 64 * 1024 + 7
-var TEST_DIR = ''
+const SIZE = 16 * 64 * 1024 + 7
+let TEST_DIR = ''
-describe('fs-extra', function () {
- beforeEach(function (done) {
+describe('fs-extra', () => {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy')
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- describe('+ copy()', function () {
- it('should return an error if src and dest are the same', function (done) {
- var fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_copy')
- var fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
- fse.copy(fileSrc, fileDest, function (err) {
+ describe('+ copy()', () => {
+ it('should return an error if src and dest are the same', done => {
+ const fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_copy')
+ const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
+ fse.copy(fileSrc, fileDest, err => {
assert.equal(err.message, 'Source and destination must not be the same.')
done()
})
})
- describe('> when the source is a file', function () {
- it('should copy the file asynchronously', function (done) {
- var fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src')
- var fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
+ describe('> when the source is a file', () => {
+ it('should copy the file asynchronously', done => {
+ const fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src')
+ const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
fs.writeFileSync(fileSrc, crypto.randomBytes(SIZE))
- var srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest('hex')
- var destMd5 = ''
+ const srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest('hex')
+ let destMd5 = ''
- fse.copy(fileSrc, fileDest, function (err) {
+ fse.copy(fileSrc, fileDest, err => {
assert(!err)
destMd5 = crypto.createHash('md5').update(fs.readFileSync(fileDest)).digest('hex')
assert.strictEqual(srcMd5, destMd5)
@@ -46,60 +46,39 @@ describe('fs-extra', function () {
})
})
- it('should return an error if the source file does not exist', function (done) {
- var fileSrc = 'we-simply-assume-this-file-does-not-exist.bin'
- var fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
+ it('should return an error if the source file does not exist', done => {
+ const fileSrc = 'we-simply-assume-this-file-does-not-exist.bin'
+ const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
- fse.copy(fileSrc, fileDest, function (err) {
+ fse.copy(fileSrc, fileDest, err => {
assert(err)
done()
})
})
- it('should only copy files allowed by filter fn', function (done) {
- var srcFile1 = path.join(TEST_DIR, '1.css')
- fs.writeFileSync(srcFile1, '')
- var destFile1 = path.join(TEST_DIR, 'dest1.css')
- var filter = function (s) { return s.split('.').pop() !== 'css' }
- fse.copy(srcFile1, destFile1, filter, function () {
- assert(!fs.existsSync(destFile1))
- done()
- })
- })
-
- it('accepts options object in place of filter', function (done) {
- var srcFile1 = path.join(TEST_DIR, '1.jade')
- fs.writeFileSync(srcFile1, '')
- var destFile1 = path.join(TEST_DIR, 'dest1.jade')
- var options = {filter: function (s) { return /.html$|.css$/i.test(s) }}
- fse.copy(srcFile1, destFile1, options, function () {
- assert(!fs.existsSync(destFile1))
- done()
- })
- })
+ it('should copy to a destination file with two \'$\' characters in name (eg: TEST_fs-extra_$$_copy)', done => {
+ const fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src')
+ const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_$$_copy')
- it('should copy to a destination file with two \'$\' characters in name (eg: TEST_fs-extra_$$_copy)', function (done) {
- var fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src')
- var fileDest = path.join(TEST_DIR, 'TEST_fs-extra_$$_copy')
fs.writeFileSync(fileSrc, '')
- fse.copy(fileSrc, fileDest, function (err) {
+ fse.copy(fileSrc, fileDest, err => {
assert(!err)
fs.statSync(fileDest)
done()
})
})
- describe('> when the destination dir does not exist', function () {
- it('should create the destination directory and copy the file', function (done) {
- var src = path.join(TEST_DIR, 'file.txt')
- var dest = path.join(TEST_DIR, 'this/path/does/not/exist/copied.txt')
- var data = 'did it copy?\n'
+ describe('> when the destination dir does not exist', () => {
+ it('should create the destination directory and copy the file', done => {
+ const src = path.join(TEST_DIR, 'file.txt')
+ const dest = path.join(TEST_DIR, 'this/path/does/not/exist/copied.txt')
+ const data = 'did it copy?\n'
fs.writeFileSync(src, data, 'utf8')
- fse.copy(src, dest, function (err) {
- var data2 = fs.readFileSync(dest, 'utf8')
+ fse.copy(src, dest, err => {
+ const data2 = fs.readFileSync(dest, 'utf8')
assert.strictEqual(data, data2)
done(err)
})
@@ -107,46 +86,46 @@ describe('fs-extra', function () {
})
})
- describe('> when the source is a directory', function () {
- describe('> when the source directory does not exist', function () {
- it('should return an error', function (done) {
- var ts = path.join(TEST_DIR, 'this_dir_does_not_exist')
- var td = path.join(TEST_DIR, 'this_dir_really_does_not_matter')
- fse.copy(ts, td, function (err) {
+ describe('> when the source is a directory', () => {
+ describe('> when the source directory does not exist', () => {
+ it('should return an error', done => {
+ const ts = path.join(TEST_DIR, 'this_dir_does_not_exist')
+ const td = path.join(TEST_DIR, 'this_dir_really_does_not_matter')
+ fse.copy(ts, td, err => {
assert(err)
done()
})
})
})
- it('should copy the directory asynchronously', function (done) {
- var FILES = 2
- var src = path.join(TEST_DIR, 'src')
- var dest = path.join(TEST_DIR, 'dest')
+ it('should copy the directory asynchronously', done => {
+ const FILES = 2
+ const src = path.join(TEST_DIR, 'src')
+ const dest = path.join(TEST_DIR, 'dest')
- fse.mkdirs(src, function (err) {
+ fse.mkdirs(src, err => {
assert(!err)
- for (var i = 0; i < FILES; ++i) {
+ for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(src, i.toString()), crypto.randomBytes(SIZE))
}
- var subdir = path.join(src, 'subdir')
- fse.mkdirs(subdir, function (err) {
+ const subdir = path.join(src, 'subdir')
+ fse.mkdirs(subdir, err => {
assert(!err)
- for (var i = 0; i < FILES; ++i) {
+ for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(subdir, i.toString()), crypto.randomBytes(SIZE))
}
- fse.copy(src, dest, function (err) {
+ fse.copy(src, dest, err => {
assert.ifError(err)
assert(fs.existsSync(dest))
- for (var i = 0; i < FILES; ++i) {
+ for (let i = 0; i < FILES; ++i) {
assert(fs.existsSync(path.join(dest, i.toString())))
}
- var destSub = path.join(dest, 'subdir')
- for (var j = 0; j < FILES; ++j) {
+ const destSub = path.join(dest, 'subdir')
+ for (let j = 0; j < FILES; ++j) {
assert(fs.existsSync(path.join(destSub, j.toString())))
}
@@ -156,21 +135,21 @@ describe('fs-extra', function () {
})
})
- describe('> when the destination dir does not exist', function () {
- it('should create the destination directory and copy the file', function (done) {
- var src = path.join(TEST_DIR, 'data/')
+ describe('> when the destination dir does not exist', () => {
+ it('should create the destination directory and copy the file', done => {
+ const src = path.join(TEST_DIR, 'data/')
fse.mkdirsSync(src)
- var d1 = 'file1'
- var d2 = 'file2'
+ const d1 = 'file1'
+ const d2 = 'file2'
fs.writeFileSync(path.join(src, 'f1.txt'), d1)
fs.writeFileSync(path.join(src, 'f2.txt'), d2)
- var dest = path.join(TEST_DIR, 'this/path/does/not/exist/outputDir')
+ const dest = path.join(TEST_DIR, 'this/path/does/not/exist/outputDir')
- fse.copy(src, dest, function (err) {
- var o1 = fs.readFileSync(path.join(dest, 'f1.txt'), 'utf8')
- var o2 = fs.readFileSync(path.join(dest, 'f2.txt'), 'utf8')
+ fse.copy(src, dest, err => {
+ const o1 = fs.readFileSync(path.join(dest, 'f1.txt'), 'utf8')
+ const o2 = fs.readFileSync(path.join(dest, 'f2.txt'), 'utf8')
assert.strictEqual(d1, o1)
assert.strictEqual(d2, o2)
@@ -180,14 +159,170 @@ describe('fs-extra', function () {
})
})
- describe('> when src dir does not exist', function () {
- it('should return an error', function (done) {
- fse.copy('/does/not/exist', '/something/else', function (err) {
+ describe('> when src dir does not exist', () => {
+ it('should return an error', done => {
+ fse.copy('/does/not/exist', '/something/else', err => {
assert(err instanceof Error)
done()
})
})
})
})
+
+ describe('> when filter is used', () => {
+ it('should only copy files allowed by filter fn', done => {
+ const srcFile1 = path.join(TEST_DIR, '1.css')
+ fs.writeFileSync(srcFile1, '')
+ const destFile1 = path.join(TEST_DIR, 'dest1.css')
+ const filter = s => s.split('.').pop() !== 'css'
+
+ fse.copy(srcFile1, destFile1, filter, err => {
+ assert(!err)
+ assert(!fs.existsSync(destFile1))
+ done()
+ })
+ })
+
+ it('accepts options object in place of filter', done => {
+ const srcFile1 = path.join(TEST_DIR, '1.jade')
+ fs.writeFileSync(srcFile1, '')
+ const destFile1 = path.join(TEST_DIR, 'dest1.jade')
+ const options = { filter: s => /.html$|.css$/i.test(s) }
+
+ fse.copy(srcFile1, destFile1, options, (err) => {
+ assert(!err)
+ assert(!fs.existsSync(destFile1))
+ done()
+ })
+ })
+
+ it('should should apply filter recursively', done => {
+ const FILES = 2
+ // Don't match anything that ends with a digit higher than 0:
+ const filter = s => /(0|\D)$/i.test(s)
+
+ const src = path.join(TEST_DIR, 'src')
+ fse.mkdirsSync(src)
+
+ for (let i = 0; i < FILES; ++i) {
+ fs.writeFileSync(path.join(src, i.toString()), crypto.randomBytes(SIZE))
+ }
+
+ const subdir = path.join(src, 'subdir')
+ fse.mkdirsSync(subdir)
+
+ for (let i = 0; i < FILES; ++i) {
+ fs.writeFileSync(path.join(subdir, i.toString()), crypto.randomBytes(SIZE))
+ }
+ const dest = path.join(TEST_DIR, 'dest')
+ fse.copy(src, dest, filter, err => {
+ assert(!err)
+
+ assert(fs.existsSync(dest))
+ assert(FILES > 1)
+
+ for (let i = 0; i < FILES; ++i) {
+ if (i === 0) {
+ assert(fs.existsSync(path.join(dest, i.toString())))
+ } else {
+ assert(!fs.existsSync(path.join(dest, i.toString())))
+ }
+ }
+
+ const destSub = path.join(dest, 'subdir')
+
+ for (let j = 0; j < FILES; ++j) {
+ if (j === 0) {
+ assert(fs.existsSync(path.join(destSub, j.toString())))
+ } else {
+ assert(!fs.existsSync(path.join(destSub, j.toString())))
+ }
+ }
+ done()
+ })
+ })
+
+ it('should apply the filter to directory names', done => {
+ const IGNORE = 'ignore'
+ const filter = p => !~p.indexOf(IGNORE)
+
+ const src = path.join(TEST_DIR, 'src')
+ fse.mkdirsSync(src)
+
+ const ignoreDir = path.join(src, IGNORE)
+ fse.mkdirsSync(ignoreDir)
+
+ fse.writeFileSync(path.join(ignoreDir, 'file'), crypto.randomBytes(SIZE))
+
+ const dest = path.join(TEST_DIR, 'dest')
+
+ fse.copySync(src, dest, filter)
+
+ assert(!fs.existsSync(path.join(dest, IGNORE)), 'directory was not ignored')
+ assert(!fs.existsSync(path.join(dest, IGNORE, 'file')), 'file was not ignored')
+ done()
+ })
+
+ it('should apply filter when it is applied only to dest', done => {
+ const timeCond = new Date().getTime()
+
+ const filter = (s, d) => fs.statSync(d).birthtime.getTime() < timeCond
+
+ const src = path.join(TEST_DIR, 'src')
+ fse.mkdirsSync(src)
+ const subdir = path.join(src, 'subdir')
+ fse.mkdirsSync(subdir)
+
+ const dest = path.join(TEST_DIR, 'dest')
+
+ setTimeout(() => {
+ fse.mkdirsSync(dest)
+
+ fse.copy(src, dest, filter, err => {
+ assert(!err)
+ assert(!fs.existsSync(path.join(dest, 'subdir')))
+ done()
+ })
+ }, 1000)
+ })
+
+ it('should apply filter when it is applied to both src and dest', done => {
+ const timeCond = new Date().getTime()
+ const filter = (s, d) => s.split('.').pop() !== 'css' && fs.statSync(path.dirname(d)).birthtime.getTime() > timeCond
+
+ const dest = path.join(TEST_DIR, 'dest')
+ setTimeout(() => {
+ fse.mkdirsSync(dest)
+
+ const srcFile1 = path.join(TEST_DIR, '1.html')
+ const srcFile2 = path.join(TEST_DIR, '2.css')
+ const srcFile3 = path.join(TEST_DIR, '3.jade')
+
+ fse.writeFileSync(srcFile1, '')
+ fse.writeFileSync(srcFile2, '')
+ fse.writeFileSync(srcFile3, '')
+
+ const destFile1 = path.join(dest, 'dest1.html')
+ const destFile2 = path.join(dest, 'dest2.css')
+ const destFile3 = path.join(dest, 'dest3.jade')
+
+ fse.copy(srcFile1, destFile1, filter, err => {
+ assert(!err)
+ assert(fs.existsSync(destFile1))
+
+ fse.copy(srcFile2, destFile2, filter, err => {
+ assert(!err)
+ assert(!fs.existsSync(destFile2))
+
+ fse.copy(srcFile3, destFile3, filter, err => {
+ assert(!err)
+ assert(fs.existsSync(destFile3))
+ done()
+ })
+ })
+ })
+ }, 1000)
+ })
+ })
})
})
diff --git a/lib/copy/__tests__/ncp/broken-symlink.test.js b/lib/copy/__tests__/ncp/broken-symlink.test.js
index 33b0d43..781ac6d 100644
--- a/lib/copy/__tests__/ncp/broken-symlink.test.js
+++ b/lib/copy/__tests__/ncp/broken-symlink.test.js
@@ -1,38 +1,38 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
-var ncp = require('../../ncp')
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const ncp = require('../../ncp')
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
describe('ncp broken symlink', function () {
- var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ncp-broken-symlinks')
- var src = path.join(TEST_DIR, 'src')
- var out = path.join(TEST_DIR, 'out')
+ const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ncp-broken-symlinks')
+ const src = path.join(TEST_DIR, 'src')
+ const out = path.join(TEST_DIR, 'out')
- beforeEach(function (done) {
- fse.emptyDir(TEST_DIR, function (err) {
+ beforeEach(done => {
+ fse.emptyDir(TEST_DIR, err => {
assert.ifError(err)
createFixtures(src, done)
})
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- it('should copy broken symlinks by default', function (done) {
- ncp(src, out, function (err) {
+ it('should copy broken symlinks by default', done => {
+ ncp(src, out, err => {
if (err) return done(err)
assert.equal(fs.readlinkSync(path.join(out, 'broken-symlink')), path.join(src, 'does-not-exist'))
done()
})
})
- it('should return an error when dereference=true', function (done) {
- ncp(src, out, {dereference: true}, function (err) {
+ it('should return an error when dereference=true', done => {
+ ncp(src, out, {dereference: true}, err => {
assert.equal(err.code, 'ENOENT')
done()
})
@@ -40,12 +40,15 @@ describe('ncp broken symlink', function () {
})
function createFixtures (srcDir, callback) {
- fs.mkdir(srcDir, function (err) {
+ fs.mkdir(srcDir, err => {
+ let brokenFile
+ let brokenFileLink
+
if (err) return callback(err)
try {
- var brokenFile = path.join(srcDir, 'does-not-exist')
- var brokenFileLink = path.join(srcDir, 'broken-symlink')
+ brokenFile = path.join(srcDir, 'does-not-exist')
+ brokenFileLink = path.join(srcDir, 'broken-symlink')
fs.writeFileSync(brokenFile, 'does not matter')
fs.symlinkSync(brokenFile, brokenFileLink, 'file')
} catch (err) {
diff --git a/lib/copy/__tests__/ncp/ncp-error-perm.test.js b/lib/copy/__tests__/ncp/ncp-error-perm.test.js
index 5038959..451bbdf 100644
--- a/lib/copy/__tests__/ncp/ncp-error-perm.test.js
+++ b/lib/copy/__tests__/ncp/ncp-error-perm.test.js
@@ -1,11 +1,13 @@
+'use strict'
+
// file in reference: https://github.com/jprichardson/node-fs-extra/issues/56
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
-var ncp = require('../../ncp')
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const ncp = require('../../ncp')
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
@@ -14,34 +16,32 @@ var ncp = require('../../ncp')
// if (os.platform().indexOf('win') === 0) return
// eslint-enable globalReturn */
-describe('ncp / error / dest-permission', function () {
- var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ncp-error-dest-perm')
- var src = path.join(TEST_DIR, 'src')
- var dest = path.join(TEST_DIR, 'dest')
+describe('ncp / error / dest-permission', () => {
+ const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ncp-error-dest-perm')
+ const src = path.join(TEST_DIR, 'src')
+ const dest = path.join(TEST_DIR, 'dest')
if (os.platform().indexOf('win') === 0) return
- beforeEach(function (done) {
- fse.emptyDir(TEST_DIR, function (err) {
+ beforeEach(done => {
+ fse.emptyDir(TEST_DIR, err => {
assert.ifError(err)
done()
})
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- it('should return an error', function (done) {
- var someFile = path.join(src, 'some-file')
+ it('should return an error', done => {
+ const someFile = path.join(src, 'some-file')
fse.outputFileSync(someFile, 'hello')
fse.mkdirsSync(dest)
fs.chmodSync(dest, parseInt('444', 8))
- var subdest = path.join(dest, 'another-dir')
+ const subdest = path.join(dest, 'another-dir')
- ncp(src, subdest, function (err) {
+ ncp(src, subdest, err => {
assert(err)
assert.equal(err.code, 'EACCES')
done()
diff --git a/lib/copy/__tests__/ncp/ncp.test.js b/lib/copy/__tests__/ncp/ncp.test.js
index acb0f42..23ad3fc 100644
--- a/lib/copy/__tests__/ncp/ncp.test.js
+++ b/lib/copy/__tests__/ncp/ncp.test.js
@@ -1,30 +1,28 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var rimraf = require('rimraf')
-var readDirFiles = require('read-dir-files').read // temporary, will remove
-var ncp = require('../../ncp')
+'use strict'
+
+const fs = require('fs')
+const ncp = require('../../ncp')
+const path = require('path')
+const rimraf = require('rimraf')
+const assert = require('assert')
+const readDirFiles = require('read-dir-files').read // temporary, will remove
/* eslint-env mocha */
-var fixturesDir = path.join(__dirname, 'fixtures')
+const fixturesDir = path.join(__dirname, 'fixtures')
-describe('ncp', function () {
- describe('regular files and directories', function () {
- var fixtures = path.join(fixturesDir, 'regular-fixtures')
- var src = path.join(fixtures, 'src')
- var out = path.join(fixtures, 'out')
+describe('ncp', () => {
+ describe('regular files and directories', () => {
+ const fixtures = path.join(fixturesDir, 'regular-fixtures')
+ const src = path.join(fixtures, 'src')
+ const out = path.join(fixtures, 'out')
- before(function (cb) {
- rimraf(out, function () {
- ncp(src, out, cb)
- })
- })
+ before(cb => rimraf(out, () => ncp(src, out, cb)))
- describe('when copying a directory of files', function () {
- it('files are copied correctly', function (cb) {
- readDirFiles(src, 'utf8', function (srcErr, srcFiles) {
- readDirFiles(out, 'utf8', function (outErr, outFiles) {
+ describe('when copying a directory of files', () => {
+ it('files are copied correctly', cb => {
+ readDirFiles(src, 'utf8', (srcErr, srcFiles) => {
+ readDirFiles(out, 'utf8', (outErr, outFiles) => {
assert.ifError(srcErr)
assert.deepEqual(srcFiles, outFiles)
cb()
@@ -33,21 +31,18 @@ describe('ncp', function () {
})
})
- describe('when copying files using filter', function () {
- before(function (cb) {
- var filter = function (name) {
- return name.substr(name.length - 1) !== 'a'
- }
- rimraf(out, function () {
- ncp(src, out, {filter: filter}, cb)
- })
+ describe('when copying files using filter', () => {
+ before(cb => {
+ const filter = name => name.substr(name.length - 1) !== 'a'
+
+ rimraf(out, () => ncp(src, out, { filter }, cb))
})
- it('files are copied correctly', function (cb) {
- readDirFiles(src, 'utf8', function (srcErr, srcFiles) {
+ it('files are copied correctly', cb => {
+ readDirFiles(src, 'utf8', (srcErr, srcFiles) => {
function filter (files) {
- for (var fileName in files) {
- var curFile = files[fileName]
+ for (let fileName in files) {
+ const curFile = files[fileName]
if (curFile instanceof Object) {
return filter(curFile)
}
@@ -58,7 +53,7 @@ describe('ncp', function () {
}
}
filter(srcFiles)
- readDirFiles(out, 'utf8', function (outErr, outFiles) {
+ readDirFiles(out, 'utf8', (outErr, outFiles) => {
assert.ifError(outErr)
assert.deepEqual(srcFiles, outFiles)
cb()
@@ -67,7 +62,7 @@ describe('ncp', function () {
})
})
- describe('when using overwrite=true', function () {
+ describe('when using overwrite=true', () => {
before(function () {
this.originalCreateReadStream = fs.createReadStream
})
@@ -76,41 +71,41 @@ describe('ncp', function () {
fs.createReadStream = this.originalCreateReadStream
})
- it('the copy is complete after callback', function (done) {
- ncp(src, out, {overwrite: true}, function (err) {
- fs.createReadStream = function () {
- done(new Error('createReadStream after callback'))
- }
+ it('the copy is complete after callback', done => {
+ ncp(src, out, {overwrite: true}, err => {
+ fs.createReadStream = () => done(new Error('createReadStream after callback'))
+
assert.ifError(err)
process.nextTick(done)
})
})
})
- describe('when using overwrite=false', function () {
- beforeEach(function (done) {
- rimraf(out, done)
- })
- it('works', function (cb) {
- ncp(src, out, {overwrite: false}, function (err) {
+ describe('when using overwrite=false', () => {
+ beforeEach(done => rimraf(out, done))
+
+ it('works', cb => {
+ ncp(src, out, {overwrite: false}, err => {
assert.ifError(err)
cb()
})
})
- it('should not error if files exist', function (cb) {
- ncp(src, out, function () {
- ncp(src, out, {overwrite: false}, function (err) {
+
+ it('should not error if files exist', cb => {
+ ncp(src, out, () => {
+ ncp(src, out, {overwrite: false}, err => {
assert.ifError(err)
cb()
})
})
})
- it('should error if errorOnExist and file exists', function (cb) {
- ncp(src, out, function () {
+
+ it('should error if errorOnExist and file exists', cb => {
+ ncp(src, out, () => {
ncp(src, out, {
overwrite: false,
errorOnExist: true
- }, function (err) {
+ }, err => {
assert(err)
cb()
})
@@ -118,17 +113,15 @@ describe('ncp', function () {
})
})
- describe('clobber', function () {
- beforeEach(function (done) {
- rimraf(out, done)
- })
+ describe('clobber', () => {
+ beforeEach(done => rimraf(out, done))
- it('is an alias for overwrite', function (cb) {
- ncp(src, out, function () {
+ it('is an alias for overwrite', cb => {
+ ncp(src, out, () => {
ncp(src, out, {
clobber: false,
errorOnExist: true
- }, function (err) {
+ }, err => {
assert(err)
cb()
})
@@ -136,10 +129,10 @@ describe('ncp', function () {
})
})
- describe('when using transform', function () {
- it('file descriptors are passed correctly', function (cb) {
+ describe('when using transform', () => {
+ it('file descriptors are passed correctly', cb => {
ncp(src, out, {
- transform: function (read, write, file) {
+ transform: (read, write, file) => {
assert.notEqual(file.name, undefined)
assert.strictEqual(typeof file.mode, 'number')
read.pipe(write)
@@ -150,20 +143,20 @@ describe('ncp', function () {
})
// see https://github.com/AvianFlu/ncp/issues/71
- describe('Issue 71: Odd Async Behaviors', function (cb) {
- var fixtures = path.join(__dirname, 'fixtures', 'regular-fixtures')
- var src = path.join(fixtures, 'src')
- var out = path.join(fixtures, 'out')
+ describe('Issue 71: Odd Async Behaviors', cb => {
+ const fixtures = path.join(__dirname, 'fixtures', 'regular-fixtures')
+ const src = path.join(fixtures, 'src')
+ const out = path.join(fixtures, 'out')
- var totalCallbacks = 0
+ let totalCallbacks = 0
function copyAssertAndCount (callback) {
// rimraf(out, function() {
- ncp(src, out, function (err) {
+ ncp(src, out, err => {
assert(!err)
totalCallbacks += 1
- readDirFiles(src, 'utf8', function (srcErr, srcFiles) {
- readDirFiles(out, 'utf8', function (outErr, outFiles) {
+ readDirFiles(src, 'utf8', (srcErr, srcFiles) => {
+ readDirFiles(out, 'utf8', (outErr, outFiles) => {
assert.ifError(srcErr)
assert.deepEqual(srcFiles, outFiles)
callback()
@@ -173,15 +166,15 @@ describe('ncp', function () {
// })
}
- describe('when copying a directory of files without cleaning the destination', function () {
- it('callback fires once per run and directories are equal', function (done) {
- var expected = 10
- var count = 10
+ describe('when copying a directory of files without cleaning the destination', () => {
+ it('callback fires once per run and directories are equal', done => {
+ const expected = 10
+ let count = 10
function next () {
if (count > 0) {
- setTimeout(function () {
- copyAssertAndCount(function () {
+ setTimeout(() => {
+ copyAssertAndCount(() => {
count -= 1
next()
})
diff --git a/lib/copy/__tests__/ncp/symlink.test.js b/lib/copy/__tests__/ncp/symlink.test.js
index 3315a1c..1b8816b 100644
--- a/lib/copy/__tests__/ncp/symlink.test.js
+++ b/lib/copy/__tests__/ncp/symlink.test.js
@@ -1,30 +1,30 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
-var ncp = require('../../ncp')
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const ncp = require('../../ncp')
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-describe('ncp / symlink', function () {
- var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ncp-symlinks')
- var src = path.join(TEST_DIR, 'src')
- var out = path.join(TEST_DIR, 'out')
+describe('ncp / symlink', () => {
+ const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ncp-symlinks')
+ const src = path.join(TEST_DIR, 'src')
+ const out = path.join(TEST_DIR, 'out')
- beforeEach(function (done) {
- fse.emptyDir(TEST_DIR, function (err) {
+ beforeEach(done => {
+ fse.emptyDir(TEST_DIR, err => {
assert.ifError(err)
createFixtures(src, done)
})
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- it('copies symlinks by default', function (done) {
- ncp(src, out, function (err) {
+ it('copies symlinks by default', done => {
+ ncp(src, out, err => {
assert.ifError(err)
assert.equal(fs.readlinkSync(path.join(out, 'file-symlink')), path.join(src, 'foo'))
@@ -34,15 +34,15 @@ describe('ncp / symlink', function () {
})
})
- it('copies file contents when dereference=true', function (done) {
- ncp(src, out, {dereference: true}, function (err) {
+ it('copies file contents when dereference=true', done => {
+ ncp(src, out, {dereference: true}, err => {
assert.ifError(err)
- var fileSymlinkPath = path.join(out, 'file-symlink')
+ const fileSymlinkPath = path.join(out, 'file-symlink')
assert.ok(fs.lstatSync(fileSymlinkPath).isFile())
assert.equal(fs.readFileSync(fileSymlinkPath), 'foo contents')
- var dirSymlinkPath = path.join(out, 'dir-symlink')
+ const dirSymlinkPath = path.join(out, 'dir-symlink')
assert.ok(fs.lstatSync(dirSymlinkPath).isDirectory())
assert.deepEqual(fs.readdirSync(dirSymlinkPath), ['bar'])
@@ -52,20 +52,20 @@ describe('ncp / symlink', function () {
})
function createFixtures (srcDir, callback) {
- fs.mkdir(srcDir, function (err) {
+ fs.mkdir(srcDir, err => {
if (err) return callback(err)
// note: third parameter in symlinkSync is type e.g. 'file' or 'dir'
// https://nodejs.org/api/fs.html#fs_fs_symlink_srcpath_dstpath_type_callback
try {
- var fooFile = path.join(srcDir, 'foo')
- var fooFileLink = path.join(srcDir, 'file-symlink')
+ const fooFile = path.join(srcDir, 'foo')
+ const fooFileLink = path.join(srcDir, 'file-symlink')
fs.writeFileSync(fooFile, 'foo contents')
fs.symlinkSync(fooFile, fooFileLink, 'file')
- var dir = path.join(srcDir, 'dir')
- var dirFile = path.join(dir, 'bar')
- var dirLink = path.join(srcDir, 'dir-symlink')
+ const dir = path.join(srcDir, 'dir')
+ const dirFile = path.join(dir, 'bar')
+ const dirLink = path.join(srcDir, 'dir-symlink')
fs.mkdirSync(dir)
fs.writeFileSync(dirFile, 'bar contents')
fs.symlinkSync(dir, dirLink, 'dir')
diff --git a/lib/copy/copy.js b/lib/copy/copy.js
index 6c5e224..d66c898 100644
--- a/lib/copy/copy.js
+++ b/lib/copy/copy.js
@@ -1,7 +1,9 @@
-var fs = require('graceful-fs')
-var path = require('path')
-var ncp = require('./ncp')
-var mkdir = require('../mkdirs')
+'use strict'
+
+const fs = require('graceful-fs')
+const path = require('path')
+const ncp = require('./ncp')
+const mkdir = require('../mkdirs')
function copy (src, dest, options, callback) {
if (typeof options === 'function' && !callback) {
@@ -15,31 +17,31 @@ function copy (src, dest, options, callback) {
// Warn about using preserveTimestamps on 32-bit node:
if (options.preserveTimestamps && process.arch === 'ia32') {
- console.warn('fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n' +
- 'see https://github.com/jprichardson/node-fs-extra/issues/269')
+ console.warn(`fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n
+ see https://github.com/jprichardson/node-fs-extra/issues/269`)
}
// don't allow src and dest to be the same
- var basePath = process.cwd()
- var currentPath = path.resolve(basePath, src)
- var targetPath = path.resolve(basePath, dest)
+ const basePath = process.cwd()
+ const currentPath = path.resolve(basePath, src)
+ const targetPath = path.resolve(basePath, dest)
if (currentPath === targetPath) return callback(new Error('Source and destination must not be the same.'))
- fs.lstat(src, function (err, stats) {
+ fs.lstat(src, (err, stats) => {
if (err) return callback(err)
- var dir = null
+ let dir = null
if (stats.isDirectory()) {
- var parts = dest.split(path.sep)
+ const parts = dest.split(path.sep)
parts.pop()
dir = parts.join(path.sep)
} else {
dir = path.dirname(dest)
}
- fs.exists(dir, function (dirExists) {
+ fs.exists(dir, dirExists => {
if (dirExists) return ncp(src, dest, options, callback)
- mkdir.mkdirs(dir, function (err) {
+ mkdir.mkdirs(dir, err => {
if (err) return callback(err)
ncp(src, dest, options, callback)
})
diff --git a/lib/copy/ncp.js b/lib/copy/ncp.js
index 7137e16..9670ee0 100644
--- a/lib/copy/ncp.js
+++ b/lib/copy/ncp.js
@@ -41,7 +41,7 @@ function ncp (source, dest, options, callback) {
return doneOne(true)
}
} else if (typeof filter === 'function') {
- if (!filter(source)) {
+ if (!filter(source, dest)) {
return doneOne(true)
}
}
diff --git a/lib/empty/__tests__/empty-dir-sync.test.js b/lib/empty/__tests__/empty-dir-sync.test.js
index 3587472..b8a648c 100644
--- a/lib/empty/__tests__/empty-dir-sync.test.js
+++ b/lib/empty/__tests__/empty-dir-sync.test.js
@@ -1,15 +1,17 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-describe('+ emptyDir()', function () {
- var TEST_DIR
+describe('+ emptyDir()', () => {
+ let TEST_DIR
- beforeEach(function () {
+ beforeEach(() => {
TEST_DIR = path.join(os.tmpdir(), 'test-fs-extra', 'empty-dir')
if (fs.existsSync(TEST_DIR)) {
fse.removeSync(TEST_DIR)
@@ -17,12 +19,10 @@ describe('+ emptyDir()', function () {
fse.ensureDirSync(TEST_DIR)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- describe('> when directory exists and contains items', function () {
- it('should delete all of the items', function () {
+ describe('> when directory exists and contains items', () => {
+ it('should delete all of the items', () => {
// verify nothing
assert.equal(fs.readdirSync(TEST_DIR).length, 0)
fse.ensureFileSync(path.join(TEST_DIR, 'some-file'))
@@ -35,16 +35,16 @@ describe('+ emptyDir()', function () {
})
})
- describe('> when directory exists and contains no items', function () {
- it('should do nothing', function () {
+ describe('> when directory exists and contains no items', () => {
+ it('should do nothing', () => {
assert.equal(fs.readdirSync(TEST_DIR).length, 0)
fse.emptyDirSync(TEST_DIR)
assert.equal(fs.readdirSync(TEST_DIR).length, 0)
})
})
- describe('> when directory does not exist', function () {
- it('should create it', function () {
+ describe('> when directory does not exist', () => {
+ it('should create it', () => {
fse.removeSync(TEST_DIR)
assert(!fs.existsSync(TEST_DIR))
fse.emptyDirSync(TEST_DIR)
diff --git a/lib/empty/__tests__/empty-dir.test.js b/lib/empty/__tests__/empty-dir.test.js
index 8ef1304..4f50917 100644
--- a/lib/empty/__tests__/empty-dir.test.js
+++ b/lib/empty/__tests__/empty-dir.test.js
@@ -1,15 +1,17 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-describe('+ emptyDir()', function () {
- var TEST_DIR
+describe('+ emptyDir()', () => {
+ let TEST_DIR
- beforeEach(function () {
+ beforeEach(() => {
TEST_DIR = path.join(os.tmpdir(), 'test-fs-extra', 'empty-dir')
if (fs.existsSync(TEST_DIR)) {
fse.removeSync(TEST_DIR)
@@ -17,12 +19,10 @@ describe('+ emptyDir()', function () {
fse.ensureDirSync(TEST_DIR)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- describe('> when directory exists and contains items', function () {
- it('should delete all of the items', function (done) {
+ describe('> when directory exists and contains items', () => {
+ it('should delete all of the items', done => {
// verify nothing
assert.equal(fs.readdirSync(TEST_DIR).length, 0)
fse.ensureFileSync(path.join(TEST_DIR, 'some-file'))
@@ -30,7 +30,7 @@ describe('+ emptyDir()', function () {
fse.ensureDirSync(path.join(TEST_DIR, 'some-dir'))
assert.equal(fs.readdirSync(TEST_DIR).length, 3)
- fse.emptyDir(TEST_DIR, function (err) {
+ fse.emptyDir(TEST_DIR, err => {
assert.ifError(err)
assert.equal(fs.readdirSync(TEST_DIR).length, 0)
done()
@@ -38,10 +38,10 @@ describe('+ emptyDir()', function () {
})
})
- describe('> when directory exists and contains no items', function () {
- it('should do nothing', function (done) {
+ describe('> when directory exists and contains no items', () => {
+ it('should do nothing', done => {
assert.equal(fs.readdirSync(TEST_DIR).length, 0)
- fse.emptyDir(TEST_DIR, function (err) {
+ fse.emptyDir(TEST_DIR, err => {
assert.ifError(err)
assert.equal(fs.readdirSync(TEST_DIR).length, 0)
done()
@@ -49,11 +49,11 @@ describe('+ emptyDir()', function () {
})
})
- describe('> when directory does not exist', function () {
- it('should create it', function (done) {
+ describe('> when directory does not exist', () => {
+ it('should create it', done => {
fse.removeSync(TEST_DIR)
assert(!fs.existsSync(TEST_DIR))
- fse.emptyDir(TEST_DIR, function (err) {
+ fse.emptyDir(TEST_DIR, err => {
assert.ifError(err)
assert.equal(fs.readdirSync(TEST_DIR).length, 0)
done()
diff --git a/lib/empty/index.js b/lib/empty/index.js
index a17cbae..180c3f7 100644
--- a/lib/empty/index.js
+++ b/lib/empty/index.js
@@ -1,23 +1,23 @@
-var fs = require('fs')
-var path = require('path')
-var mkdir = require('../mkdirs')
-var remove = require('../remove')
+'use strict'
+
+const fs = require('fs')
+const path = require('path')
+const mkdir = require('../mkdirs')
+const remove = require('../remove')
function emptyDir (dir, callback) {
callback = callback || function () {}
- fs.readdir(dir, function (err, items) {
+ fs.readdir(dir, (err, items) => {
if (err) return mkdir.mkdirs(dir, callback)
- items = items.map(function (item) {
- return path.join(dir, item)
- })
+ items = items.map(item => path.join(dir, item))
deleteItem()
function deleteItem () {
- var item = items.pop()
+ const item = items.pop()
if (!item) return callback()
- remove.remove(item, function (err) {
+ remove.remove(item, err => {
if (err) return callback(err)
deleteItem()
})
@@ -26,22 +26,22 @@ function emptyDir (dir, callback) {
}
function emptyDirSync (dir) {
- var items
+ let items
try {
items = fs.readdirSync(dir)
} catch (err) {
return mkdir.mkdirsSync(dir)
}
- items.forEach(function (item) {
+ items.forEach(item => {
item = path.join(dir, item)
remove.removeSync(item)
})
}
module.exports = {
- emptyDirSync: emptyDirSync,
+ emptyDirSync,
emptydirSync: emptyDirSync,
- emptyDir: emptyDir,
+ emptyDir,
emptydir: emptyDir
}
diff --git a/lib/ensure/__tests__/create.test.js b/lib/ensure/__tests__/create.test.js
index 94ce3c1..bed2966 100644
--- a/lib/ensure/__tests__/create.test.js
+++ b/lib/ensure/__tests__/create.test.js
@@ -1,29 +1,29 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-describe('fs-extra', function () {
- var TEST_DIR
+describe('fs-extra', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'create')
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- describe('+ createFile', function () {
- describe('> when the file and directory does not exist', function () {
- it('should create the file', function (done) {
- var file = path.join(TEST_DIR, Math.random() + 't-ne', Math.random() + '.txt')
+ describe('+ createFile', () => {
+ describe('> when the file and directory does not exist', () => {
+ it('should create the file', done => {
+ const file = path.join(TEST_DIR, Math.random() + 't-ne', Math.random() + '.txt')
assert(!fs.existsSync(file))
- fse.createFile(file, function (err) {
+ fse.createFile(file, err => {
assert.ifError(err)
assert(fs.existsSync(file))
done()
@@ -31,12 +31,12 @@ describe('fs-extra', function () {
})
})
- describe('> when the file does exist', function () {
- it('should not modify the file', function (done) {
- var file = path.join(TEST_DIR, Math.random() + 't-e', Math.random() + '.txt')
+ describe('> when the file does exist', () => {
+ it('should not modify the file', done => {
+ const file = path.join(TEST_DIR, Math.random() + 't-e', Math.random() + '.txt')
fse.mkdirsSync(path.dirname(file))
fs.writeFileSync(file, 'hello world')
- fse.createFile(file, function (err) {
+ fse.createFile(file, err => {
assert.ifError(err)
assert.equal(fs.readFileSync(file, 'utf8'), 'hello world')
done()
@@ -45,19 +45,19 @@ describe('fs-extra', function () {
})
})
- describe('+ createFileSync', function () {
- describe('> when the file and directory does not exist', function () {
- it('should create the file', function () {
- var file = path.join(TEST_DIR, Math.random() + 'ts-ne', Math.random() + '.txt')
+ describe('+ createFileSync', () => {
+ describe('> when the file and directory does not exist', () => {
+ it('should create the file', () => {
+ const file = path.join(TEST_DIR, Math.random() + 'ts-ne', Math.random() + '.txt')
assert(!fs.existsSync(file))
fse.createFileSync(file)
assert(fs.existsSync(file))
})
})
- describe('> when the file does exist', function () {
- it('should not modify the file', function () {
- var file = path.join(TEST_DIR, Math.random() + 'ts-e', Math.random() + '.txt')
+ describe('> when the file does exist', () => {
+ it('should not modify the file', () => {
+ const file = path.join(TEST_DIR, Math.random() + 'ts-e', Math.random() + '.txt')
fse.mkdirsSync(path.dirname(file))
fs.writeFileSync(file, 'hello world')
fse.createFileSync(file)
diff --git a/lib/ensure/__tests__/ensure.test.js b/lib/ensure/__tests__/ensure.test.js
index 5ff340f..b7b519e 100644
--- a/lib/ensure/__tests__/ensure.test.js
+++ b/lib/ensure/__tests__/ensure.test.js
@@ -1,31 +1,31 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-describe('fs-extra', function () {
- var TEST_DIR
+describe('fs-extra', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure')
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- describe('+ ensureFile()', function () {
- describe('> when file exists', function () {
- it('should not do anything', function (done) {
- var file = path.join(TEST_DIR, 'file.txt')
+ describe('+ ensureFile()', () => {
+ describe('> when file exists', () => {
+ it('should not do anything', done => {
+ const file = path.join(TEST_DIR, 'file.txt')
fs.writeFileSync(file, 'blah')
assert(fs.existsSync(file))
- fse.ensureFile(file, function (err) {
+ fse.ensureFile(file, err => {
assert.ifError(err)
assert(fs.existsSync(file))
done()
@@ -33,12 +33,12 @@ describe('fs-extra', function () {
})
})
- describe('> when file does not exist', function () {
- it('should create the file', function (done) {
- var file = path.join(TEST_DIR, 'dir/that/does/not/exist', 'file.txt')
+ describe('> when file does not exist', () => {
+ it('should create the file', done => {
+ const file = path.join(TEST_DIR, 'dir/that/does/not/exist', 'file.txt')
assert(!fs.existsSync(file))
- fse.ensureFile(file, function (err) {
+ fse.ensureFile(file, err => {
assert.ifError(err)
assert(fs.existsSync(file))
done()
@@ -47,10 +47,10 @@ describe('fs-extra', function () {
})
})
- describe('+ ensureFileSync()', function () {
- describe('> when file exists', function () {
- it('should not do anything', function () {
- var file = path.join(TEST_DIR, 'file.txt')
+ describe('+ ensureFileSync()', () => {
+ describe('> when file exists', () => {
+ it('should not do anything', () => {
+ const file = path.join(TEST_DIR, 'file.txt')
fs.writeFileSync(file, 'blah')
assert(fs.existsSync(file))
@@ -59,9 +59,9 @@ describe('fs-extra', function () {
})
})
- describe('> when file does not exist', function () {
- it('should create the file', function () {
- var file = path.join(TEST_DIR, 'dir/that/does/not/exist', 'file.txt')
+ describe('> when file does not exist', () => {
+ it('should create the file', () => {
+ const file = path.join(TEST_DIR, 'dir/that/does/not/exist', 'file.txt')
assert(!fs.existsSync(file))
fse.ensureFileSync(file)
@@ -70,14 +70,14 @@ describe('fs-extra', function () {
})
})
- describe('+ ensureDir()', function () {
- describe('> when dir exists', function () {
- it('should not do anything', function (done) {
- var dir = path.join(TEST_DIR, 'dir/does/not/exist')
+ describe('+ ensureDir()', () => {
+ describe('> when dir exists', () => {
+ it('should not do anything', done => {
+ const dir = path.join(TEST_DIR, 'dir/does/not/exist')
fse.mkdirpSync(dir)
assert(fs.existsSync(dir))
- fse.ensureDir(dir, function (err) {
+ fse.ensureDir(dir, err => {
assert.ifError(err)
assert(fs.existsSync(dir))
done()
@@ -85,12 +85,12 @@ describe('fs-extra', function () {
})
})
- describe('> when dir does not exist', function () {
- it('should create the dir', function (done) {
- var dir = path.join(TEST_DIR, 'dir/that/does/not/exist')
+ describe('> when dir does not exist', () => {
+ it('should create the dir', done => {
+ const dir = path.join(TEST_DIR, 'dir/that/does/not/exist')
assert(!fs.existsSync(dir))
- fse.ensureDir(dir, function (err) {
+ fse.ensureDir(dir, err => {
assert.ifError(err)
assert(fs.existsSync(dir))
done()
@@ -99,10 +99,10 @@ describe('fs-extra', function () {
})
})
- describe('+ ensureDirSync()', function () {
- describe('> when dir exists', function () {
- it('should not do anything', function () {
- var dir = path.join(TEST_DIR, 'dir/does/not/exist')
+ describe('+ ensureDirSync()', () => {
+ describe('> when dir exists', () => {
+ it('should not do anything', () => {
+ const dir = path.join(TEST_DIR, 'dir/does/not/exist')
fse.mkdirpSync(dir)
assert(fs.existsSync(dir))
@@ -111,9 +111,9 @@ describe('fs-extra', function () {
})
})
- describe('> when dir does not exist', function () {
- it('should create the dir', function () {
- var dir = path.join(TEST_DIR, 'dir/that/does/not/exist')
+ describe('> when dir does not exist', () => {
+ it('should create the dir', () => {
+ const dir = path.join(TEST_DIR, 'dir/that/does/not/exist')
assert(!fs.existsSync(dir))
fse.ensureDirSync(dir)
diff --git a/lib/ensure/__tests__/link.test.js b/lib/ensure/__tests__/link.test.js
index f06f4e8..8e27b6f 100644
--- a/lib/ensure/__tests__/link.test.js
+++ b/lib/ensure/__tests__/link.test.js
@@ -1,21 +1,21 @@
-var assert = require('assert')
-var util = require('util')
-var path = require('path')
-var os = require('os')
-var fs = require('graceful-fs')
-var CWD = process.cwd()
-var fse = require(CWD)
-var ensureLink = fse.ensureLink
-var ensureLinkSync = fse.ensureLinkSync
+'use strict'
-/* global afterEach, beforeEach, describe, it, after, before */
+const CWD = process.cwd()
+
+const fs = require('graceful-fs')
+const os = require('os')
+const fse = require(CWD)
+const path = require('path')
+const assert = require('assert')
+const ensureLink = fse.ensureLink
+const ensureLinkSync = fse.ensureLinkSync
-var TEST_DIR
+/* global afterEach, beforeEach, describe, it, after, before */
-describe('fse-ensure-link', function () {
- TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink')
+describe('fse-ensure-link', () => {
+ const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink')
- var tests = [
+ const tests = [
// [[srcpath, dstpath], fs.link expect, ensureLink expect]
[['./foo.txt', './link.txt'], 'file-success', 'file-success'],
[['./foo.txt', './dir-foo/link.txt'], 'file-success', 'file-success'],
@@ -42,12 +42,12 @@ describe('fse-ensure-link', function () {
[[path.resolve(path.join(TEST_DIR, '../dir-foo/foo.txt')), './link.txt'], 'file-error', 'file-error']
]
- before(function () {
+ before(() => {
fse.emptyDirSync(TEST_DIR)
process.chdir(TEST_DIR)
})
- beforeEach(function () {
+ beforeEach(() => {
fs.writeFileSync('./foo.txt', 'foo\n')
fse.mkdirsSync('empty-dir')
fse.mkdirsSync('dir-foo')
@@ -57,28 +57,28 @@ describe('fse-ensure-link', function () {
fse.mkdirsSync('real-alpha/real-beta/real-gamma')
})
- afterEach(function (done) {
- fse.emptyDir(TEST_DIR, done)
- })
+ afterEach(done => fse.emptyDir(TEST_DIR, done))
- after(function () {
+ after(() => {
process.chdir(CWD)
fse.removeSync(TEST_DIR)
})
function fileSuccess (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should create link file using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function (done) {
- var callback = function (err) {
+ const srcpath = args[0]
+ const dstpath = args[1]
+
+ it(`should create link file using src ${srcpath} and dst ${dstpath}`, done => {
+ const callback = err => {
if (err) return done(err)
- var srcContent = fs.readFileSync(srcpath, 'utf8')
- var dstDir = path.dirname(dstpath)
- var dstBasename = path.basename(dstpath)
- var isSymlink = fs.lstatSync(dstpath).isFile()
- var dstContent = fs.readFileSync(dstpath, 'utf8')
- var dstDirContents = fs.readdirSync(dstDir)
+
+ const srcContent = fs.readFileSync(srcpath, 'utf8')
+ const dstDir = path.dirname(dstpath)
+ const dstBasename = path.basename(dstpath)
+ const isSymlink = fs.lstatSync(dstpath).isFile()
+ const dstContent = fs.readFileSync(dstpath, 'utf8')
+ const dstDirContents = fs.readdirSync(dstDir)
+
assert.equal(isSymlink, true)
assert.equal(srcContent, dstContent)
assert(dstDirContents.indexOf(dstBasename) >= 0)
@@ -90,15 +90,15 @@ describe('fse-ensure-link', function () {
}
function fileError (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should return error when creating link file using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function (done) {
- var dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
- function callback (err) {
+ const srcpath = args[0]
+ const dstpath = args[1]
+
+ it(`should return error when creating link file using src ${srcpath} and dst ${dstpath}`, done => {
+ const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
+ const callback = err => {
assert.equal(err instanceof Error, true)
// ensure that directories aren't created if there's an error
- var dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
+ const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
assert.equal(dstdirExistsBefore, dstdirExistsAfter)
return done()
}
@@ -108,14 +108,14 @@ describe('fse-ensure-link', function () {
}
function fileDestExists (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function (done) {
- var destinationContentBefore = fs.readFileSync(dstpath, 'utf8')
- var callback = function (err) {
+ const srcpath = args[0]
+ const dstpath = args[1]
+
+ it(`should do nothing using src ${srcpath} and dst ${dstpath}`, done => {
+ const destinationContentBefore = fs.readFileSync(dstpath, 'utf8')
+ const callback = err => {
if (err) return done(err)
- var destinationContentAfter = fs.readFileSync(dstpath, 'utf8')
+ const destinationContentAfter = fs.readFileSync(dstpath, 'utf8')
assert.equal(destinationContentBefore, destinationContentAfter)
return done()
}
@@ -125,17 +125,17 @@ describe('fse-ensure-link', function () {
}
function fileSuccessSync (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should create link file using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function () {
+ const srcpath = args[0]
+ const dstpath = args[1]
+
+ it(`should create link file using src ${srcpath} and dst ${dstpath}`, () => {
fn.apply(null, args)
- var srcContent = fs.readFileSync(srcpath, 'utf8')
- var dstDir = path.dirname(dstpath)
- var dstBasename = path.basename(dstpath)
- var isSymlink = fs.lstatSync(dstpath).isFile()
- var dstContent = fs.readFileSync(dstpath, 'utf8')
- var dstDirContents = fs.readdirSync(dstDir)
+ const srcContent = fs.readFileSync(srcpath, 'utf8')
+ const dstDir = path.dirname(dstpath)
+ const dstBasename = path.basename(dstpath)
+ const isSymlink = fs.lstatSync(dstpath).isFile()
+ const dstContent = fs.readFileSync(dstpath, 'utf8')
+ const dstDirContents = fs.readdirSync(dstDir)
assert.equal(isSymlink, true)
assert.equal(srcContent, dstContent)
assert(dstDirContents.indexOf(dstBasename) >= 0)
@@ -143,78 +143,78 @@ describe('fse-ensure-link', function () {
}
function fileErrorSync (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should throw error using` src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function () {
+ const srcpath = args[0]
+ const dstpath = args[1]
+
+ it(`should throw error using src ${srcpath} and dst ${dstpath}`, () => {
// will fail if dstdir is created and there's an error
- var dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
- var err = null
+ const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
+ let err = null
try {
fn.apply(null, args)
} catch (e) {
err = e
}
assert.equal(err instanceof Error, true)
- var dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
+ const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
assert.equal(dstdirExistsBefore, dstdirExistsAfter)
})
}
function fileDestExistsSync (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function () {
- var destinationContentBefore = fs.readFileSync(dstpath, 'utf8')
+ const srcpath = args[0]
+ const dstpath = args[1]
+
+ it(`should do nothing using src ${srcpath} and dst ${dstpath}`, () => {
+ const destinationContentBefore = fs.readFileSync(dstpath, 'utf8')
fn.apply(null, args)
- var destinationContentAfter = fs.readFileSync(dstpath, 'utf8')
+ const destinationContentAfter = fs.readFileSync(dstpath, 'utf8')
assert.equal(destinationContentBefore, destinationContentAfter)
})
}
- describe('fs.link()', function () {
- var fn = fs.link
- tests.forEach(function (test) {
- var args = test[0].slice(0)
- var nativeBehavior = test[1]
- // var newBehavior = test[2]
+ describe('fs.link()', () => {
+ const fn = fs.link
+ tests.forEach(test => {
+ const args = test[0].slice(0)
+ const nativeBehavior = test[1]
+ // const newBehavior = test[2]
if (nativeBehavior === 'file-success') fileSuccess(args, fn)
if (nativeBehavior === 'file-error') fileError(args, fn)
if (nativeBehavior === 'file-dest-exists') fileDestExists(args, fn)
})
})
- describe('ensureLink()', function () {
- var fn = ensureLink
- tests.forEach(function (test) {
- var args = test[0].slice(0)
- // var nativeBehavior = test[1]
- var newBehavior = test[2]
+ describe('ensureLink()', () => {
+ const fn = ensureLink
+ tests.forEach(test => {
+ const args = test[0].slice(0)
+ // const nativeBehavior = test[1]
+ const newBehavior = test[2]
if (newBehavior === 'file-success') fileSuccess(args, fn)
if (newBehavior === 'file-error') fileError(args, fn)
if (newBehavior === 'file-dest-exists') fileDestExists(args, fn)
})
})
- describe('fs.linkSync()', function () {
- var fn = fs.linkSync
- tests.forEach(function (test) {
- var args = test[0].slice(0)
- var nativeBehavior = test[1]
- // var newBehavior = test[2]
+ describe('fs.linkSync()', () => {
+ const fn = fs.linkSync
+ tests.forEach(test => {
+ const args = test[0].slice(0)
+ const nativeBehavior = test[1]
+ // const newBehavior = test[2]
if (nativeBehavior === 'file-success') fileSuccessSync(args, fn)
if (nativeBehavior === 'file-error') fileErrorSync(args, fn)
if (nativeBehavior === 'file-dest-exists') fileDestExists(args, fn)
})
})
- describe('ensureLinkSync()', function () {
- var fn = ensureLinkSync
- tests.forEach(function (test) {
- var args = test[0].slice(0)
- // var nativeBehavior = test[1]
- var newBehavior = test[2]
+ describe('ensureLinkSync()', () => {
+ const fn = ensureLinkSync
+ tests.forEach(test => {
+ const args = test[0].slice(0)
+ // const nativeBehavior = test[1]
+ const newBehavior = test[2]
if (newBehavior === 'file-success') fileSuccessSync(args, fn)
if (newBehavior === 'file-error') fileErrorSync(args, fn)
if (newBehavior === 'file-dest-exists') fileDestExistsSync(args, fn)
diff --git a/lib/ensure/__tests__/symlink-paths.test.js b/lib/ensure/__tests__/symlink-paths.test.js
index 8bfaa38..46262b0 100644
--- a/lib/ensure/__tests__/symlink-paths.test.js
+++ b/lib/ensure/__tests__/symlink-paths.test.js
@@ -1,24 +1,26 @@
-var assert = require('assert')
-var util = require('util')
-var path = require('path')
-var os = require('os')
-var fs = require('graceful-fs')
-var CWD = process.cwd()
-var fse = require(CWD)
-var _symlinkPaths = require('../symlink-paths')
-var symlinkPaths = _symlinkPaths.symlinkPaths
-var symlinkPathsSync = _symlinkPaths.symlinkPathsSync
-var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink')
+'use strict'
+
+const CWD = process.cwd()
+
+const fs = require('graceful-fs')
+const os = require('os')
+const fse = require(CWD)
+const path = require('path')
+const assert = require('assert')
+const _symlinkPaths = require('../symlink-paths')
+const symlinkPaths = _symlinkPaths.symlinkPaths
+const symlinkPathsSync = _symlinkPaths.symlinkPathsSync
+const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink')
/* global afterEach, beforeEach, describe, it, after, before */
-describe('symlink-type', function () {
- before(function () {
+describe('symlink-type', () => {
+ before(() => {
fse.emptyDirSync(TEST_DIR)
process.chdir(TEST_DIR)
})
- beforeEach(function () {
+ beforeEach(() => {
fs.writeFileSync('./foo.txt', 'foo\n')
fse.mkdirsSync('./empty-dir')
fse.mkdirsSync('./dir-foo')
@@ -28,16 +30,14 @@ describe('symlink-type', function () {
fse.mkdirsSync('./real-alpha/real-beta/real-gamma')
})
- afterEach(function (done) {
- fse.emptyDir(TEST_DIR, done)
- })
+ afterEach(done => fse.emptyDir(TEST_DIR, done))
- after(function () {
+ after(() => {
process.chdir(CWD)
fse.removeSync(TEST_DIR)
})
- var tests = [
+ const tests = [
[['foo.txt', 'symlink.txt'], {toCwd: 'foo.txt', toDst: 'foo.txt'}], // smart && nodestyle
[['foo.txt', 'empty-dir/symlink.txt'], {toCwd: 'foo.txt', toDst: '../foo.txt'}], // smart
[['../foo.txt', 'empty-dir/symlink.txt'], {toCwd: 'foo.txt', toDst: '../foo.txt'}], // nodestyle
@@ -51,7 +51,7 @@ describe('symlink-type', function () {
]
// formats paths to pass on multiple operating systems
- tests.forEach(function (test) {
+ tests.forEach(test => {
test[0][0] = path.join(test[0][0])
test[0][1] = path.join(test[0][1])
test[1] = {
@@ -60,13 +60,12 @@ describe('symlink-type', function () {
}
})
- describe('symlinkPaths()', function () {
- tests.forEach(function (test) {
- var args = test[0].slice(0)
- var expectedRelativePaths = test[1]
- var should = util.format('should return \'%s\' when src \'%s\' and dst is \'%s\'', JSON.stringify(expectedRelativePaths), args[0], args[1])
- it(should, function (done) {
- var callback = function (err, relativePaths) {
+ describe('symlinkPaths()', () => {
+ tests.forEach(test => {
+ const args = test[0].slice(0)
+ const expectedRelativePaths = test[1]
+ it(`should return '${JSON.stringify(expectedRelativePaths)}' when src '${args[0]}' and dst is '${args[1]}'`, done => {
+ const callback = (err, relativePaths) => {
if (err) done(err)
assert.deepEqual(relativePaths, expectedRelativePaths)
done()
@@ -77,13 +76,12 @@ describe('symlink-type', function () {
})
})
- describe('symlinkPathsSync()', function () {
- tests.forEach(function (test) {
- var args = test[0].slice(0)
- var expectedRelativePaths = test[1]
- var should = util.format('should return \'%s\' when src \'%s\' and dst is \'%s\'', JSON.stringify(expectedRelativePaths), args[0], args[1])
- it(should, function () {
- var relativePaths = symlinkPathsSync.apply(null, args)
+ describe('symlinkPathsSync()', () => {
+ tests.forEach(test => {
+ const args = test[0].slice(0)
+ const expectedRelativePaths = test[1]
+ it(`should return '${JSON.stringify(expectedRelativePaths)}' when src '${args[0]}' and dst is '${args[1]}'`, () => {
+ const relativePaths = symlinkPathsSync.apply(null, args)
assert.deepEqual(relativePaths, expectedRelativePaths)
})
})
diff --git a/lib/ensure/__tests__/symlink-type.test.js b/lib/ensure/__tests__/symlink-type.test.js
index e520863..2fc2936 100644
--- a/lib/ensure/__tests__/symlink-type.test.js
+++ b/lib/ensure/__tests__/symlink-type.test.js
@@ -1,24 +1,26 @@
-var assert = require('assert')
-var util = require('util')
-var path = require('path')
-var os = require('os')
-var fs = require('graceful-fs')
-var CWD = process.cwd()
-var fse = require(CWD)
-var _symlinkType = require('../symlink-type')
-var symlinkType = _symlinkType.symlinkType
-var symlinkTypeSync = _symlinkType.symlinkTypeSync
-var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink')
+'use strict'
+
+const CWD = process.cwd()
+
+const fs = require('graceful-fs')
+const os = require('os')
+const fse = require(CWD)
+const path = require('path')
+const assert = require('assert')
+const _symlinkType = require('../symlink-type')
+const symlinkType = _symlinkType.symlinkType
+const symlinkTypeSync = _symlinkType.symlinkTypeSync
+const TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink')
/* global afterEach, beforeEach, describe, it, after, before */
-describe('symlink-type', function () {
- before(function () {
+describe('symlink-type', () => {
+ before(() => {
fse.emptyDirSync(TEST_DIR)
process.chdir(TEST_DIR)
})
- beforeEach(function () {
+ beforeEach(() => {
fs.writeFileSync('./foo.txt', 'foo\n')
fse.mkdirsSync('./empty-dir')
fse.mkdirsSync('./dir-foo')
@@ -28,16 +30,14 @@ describe('symlink-type', function () {
fse.mkdirsSync('./real-alpha/real-beta/real-gamma')
})
- afterEach(function (done) {
- fse.emptyDir(TEST_DIR, done)
- })
+ afterEach(done => fse.emptyDir(TEST_DIR, done))
- after(function () {
+ after(() => {
process.chdir(CWD)
fse.removeSync(TEST_DIR)
})
- var tests = {
+ const tests = {
success: [
// [{arguments} [srcpath, dirpath, [type] , result]
// smart file type checking
@@ -91,13 +91,12 @@ describe('symlink-type', function () {
]
}
- describe('symlinkType()', function () {
- tests.success.forEach(function (test) {
- var args = test[0].slice(0)
- var expectedType = test[1]
- var should = util.format('should return \'%s\' when src \'%s\'', expectedType, args[0])
- it(should, function (done) {
- var callback = function (err, type) {
+ describe('symlinkType()', () => {
+ tests.success.forEach(test => {
+ const args = test[0].slice(0)
+ const expectedType = test[1]
+ it(`should return '${expectedType}' when src '${args[0]}'`, done => {
+ const callback = (err, type) => {
if (err) done(err)
assert.equal(type, expectedType)
done()
@@ -108,13 +107,12 @@ describe('symlink-type', function () {
})
})
- describe('symlinkTypeSync()', function () {
- tests.success.forEach(function (test) {
- var args = test[0]
- var expectedType = test[1]
- var should = util.format('should return \'%s\' when src \'%s\'', expectedType, args[0])
- it(should, function () {
- var type = symlinkTypeSync.apply(null, args)
+ describe('symlinkTypeSync()', () => {
+ tests.success.forEach(test => {
+ const args = test[0]
+ const expectedType = test[1]
+ it(`should return '${expectedType}' when src '${args[0]}'`, () => {
+ const type = symlinkTypeSync.apply(null, args)
assert.equal(type, expectedType)
})
})
diff --git a/lib/ensure/__tests__/symlink.test.js b/lib/ensure/__tests__/symlink.test.js
index 2e688ac..6f93958 100644
--- a/lib/ensure/__tests__/symlink.test.js
+++ b/lib/ensure/__tests__/symlink.test.js
@@ -1,25 +1,25 @@
-var assert = require('assert')
-var util = require('util')
-var path = require('path')
-var os = require('os')
-var fs = require('graceful-fs')
-var CWD = process.cwd()
-var fse = require(CWD)
+'use strict'
-var _symlinkPaths = require('../symlink-paths')
-var symlinkPathsSync = _symlinkPaths.symlinkPathsSync
+const CWD = process.cwd()
-var ensureSymlink = fse.ensureSymlink
-var ensureSymlinkSync = fse.ensureSymlinkSync
+const fs = require('graceful-fs')
+const os = require('os')
+const fse = require(CWD)
+const path = require('path')
+const assert = require('assert')
+const _symlinkPaths = require('../symlink-paths')
+const symlinkPathsSync = _symlinkPaths.symlinkPathsSync
+const ensureSymlink = fse.ensureSymlink
+const ensureSymlinkSync = fse.ensureSymlinkSync
/* global afterEach, beforeEach, describe, it, after, before */
-var TEST_DIR
+let TEST_DIR
-describe('fse-ensure-symlink', function () {
+describe('fse-ensure-symlink', () => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink')
- var tests = [
+ const tests = [
// [[srcpath, dstpath], fs.symlink expect, fse.ensureSymlink expect]
[['./foo.txt', './symlink.txt'], 'file-success', 'file-success'],
[['../foo.txt', './empty-dir/symlink.txt'], 'file-success', 'file-success'],
@@ -56,12 +56,12 @@ describe('fse-ensure-symlink', function () {
[[path.resolve(path.join(TEST_DIR, '../dir-foo/foo.txt')), './symlink.txt'], 'file-broken', 'file-error']
]
- before(function () {
+ before(() => {
fse.emptyDirSync(TEST_DIR)
process.chdir(TEST_DIR)
})
- beforeEach(function () {
+ beforeEach(() => {
fs.writeFileSync('./foo.txt', 'foo\n')
fse.mkdirsSync('empty-dir')
fse.mkdirsSync('dir-foo')
@@ -71,29 +71,26 @@ describe('fse-ensure-symlink', function () {
fse.mkdirsSync('real-alpha/real-beta/real-gamma')
})
- afterEach(function (done) {
- fse.emptyDir(TEST_DIR, done)
- })
+ afterEach(done => fse.emptyDir(TEST_DIR, done))
- after(function () {
+ after(() => {
process.chdir(CWD)
fse.removeSync(TEST_DIR)
})
function fileSuccess (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should create symlink file using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function (done) {
- var callback = function (err) {
+ const srcpath = args[0]
+ const dstpath = args[1]
+ it(`should create symlink file using src ${srcpath} and dst ${dstpath}`, done => {
+ const callback = err => {
if (err) return done(err)
- var relative = symlinkPathsSync(srcpath, dstpath)
- var srcContent = fs.readFileSync(relative.toCwd, 'utf8')
- var dstDir = path.dirname(dstpath)
- var dstBasename = path.basename(dstpath)
- var isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
- var dstContent = fs.readFileSync(dstpath, 'utf8')
- var dstDirContents = fs.readdirSync(dstDir)
+ const relative = symlinkPathsSync(srcpath, dstpath)
+ const srcContent = fs.readFileSync(relative.toCwd, 'utf8')
+ const dstDir = path.dirname(dstpath)
+ const dstBasename = path.basename(dstpath)
+ const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
+ const dstContent = fs.readFileSync(dstpath, 'utf8')
+ const dstDirContents = fs.readdirSync(dstDir)
assert.equal(isSymlink, true)
assert.equal(srcContent, dstContent)
assert(dstDirContents.indexOf(dstBasename) >= 0)
@@ -105,21 +102,18 @@ describe('fse-ensure-symlink', function () {
}
function fileBroken (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should create broken symlink file using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function (done) {
- var callback = function (err) {
+ const srcpath = args[0]
+ const dstpath = args[1]
+ it(`should create broken symlink file using src ${srcpath} and dst ${dstpath}`, done => {
+ const callback = err => {
if (err) return done(err)
- var dstDir = path.dirname(dstpath)
- var dstBasename = path.basename(dstpath)
- var isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
- var dstDirContents = fs.readdirSync(dstDir)
+ const dstDir = path.dirname(dstpath)
+ const dstBasename = path.basename(dstpath)
+ const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
+ const dstDirContents = fs.readdirSync(dstDir)
assert.equal(isSymlink, true)
assert(dstDirContents.indexOf(dstBasename) >= 0)
- assert.throws(function () {
- return fs.readFileSync(dstpath, 'utf8')
- }, Error)
+ assert.throws(() => fs.readFileSync(dstpath, 'utf8'), Error)
return done()
}
args.push(callback)
@@ -128,15 +122,14 @@ describe('fse-ensure-symlink', function () {
}
function fileError (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should return error when creating symlink file using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function (done) {
- var dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
- function callback (err) {
+ const srcpath = args[0]
+ const dstpath = args[1]
+ it(`should return error when creating symlink file using src ${srcpath} and dst ${dstpath}`, done => {
+ const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
+ const callback = err => {
assert.equal(err instanceof Error, true)
// ensure that directories aren't created if there's an error
- var dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
+ const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
assert.equal(dstdirExistsBefore, dstdirExistsAfter)
return done()
}
@@ -146,14 +139,13 @@ describe('fse-ensure-symlink', function () {
}
function fileDestExists (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function (done) {
- var destinationContentBefore = fs.readFileSync(dstpath, 'utf8')
- var callback = function (err) {
+ const srcpath = args[0]
+ const dstpath = args[1]
+ it(`should do nothing using src ${srcpath} and dst ${dstpath}`, done => {
+ const destinationContentBefore = fs.readFileSync(dstpath, 'utf8')
+ const callback = err => {
if (err) return done(err)
- var destinationContentAfter = fs.readFileSync(dstpath, 'utf8')
+ const destinationContentAfter = fs.readFileSync(dstpath, 'utf8')
assert.equal(destinationContentBefore, destinationContentAfter)
return done()
}
@@ -163,19 +155,18 @@ describe('fse-ensure-symlink', function () {
}
function dirSuccess (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should create symlink dir using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function (done) {
- var callback = function (err) {
+ const srcpath = args[0]
+ const dstpath = args[1]
+ it(`should create symlink dir using src ${srcpath} and dst ${dstpath}`, done => {
+ const callback = err => {
if (err) return done(err)
- var relative = symlinkPathsSync(srcpath, dstpath)
- var srcContents = fs.readdirSync(relative.toCwd)
- var dstDir = path.dirname(dstpath)
- var dstBasename = path.basename(dstpath)
- var isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
- var dstContents = fs.readdirSync(dstpath)
- var dstDirContents = fs.readdirSync(dstDir)
+ const relative = symlinkPathsSync(srcpath, dstpath)
+ const srcContents = fs.readdirSync(relative.toCwd)
+ const dstDir = path.dirname(dstpath)
+ const dstBasename = path.basename(dstpath)
+ const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
+ const dstContents = fs.readdirSync(dstpath)
+ const dstDirContents = fs.readdirSync(dstDir)
assert.equal(isSymlink, true)
assert.deepEqual(srcContents, dstContents)
assert(dstDirContents.indexOf(dstBasename) >= 0)
@@ -187,21 +178,18 @@ describe('fse-ensure-symlink', function () {
}
function dirBroken (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should create broken symlink dir using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function (done) {
- var callback = function (err) {
+ const srcpath = args[0]
+ const dstpath = args[1]
+ it(`should create broken symlink dir using src ${srcpath} and dst ${dstpath}`, done => {
+ const callback = err => {
if (err) return done(err)
- var dstDir = path.dirname(dstpath)
- var dstBasename = path.basename(dstpath)
- var isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
- var dstDirContents = fs.readdirSync(dstDir)
+ const dstDir = path.dirname(dstpath)
+ const dstBasename = path.basename(dstpath)
+ const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
+ const dstDirContents = fs.readdirSync(dstDir)
assert.equal(isSymlink, true)
assert(dstDirContents.indexOf(dstBasename) >= 0)
- assert.throws(function () {
- return fs.readdirSync(dstpath)
- }, Error)
+ assert.throws(() => fs.readdirSync(dstpath), Error)
return done()
}
args.push(callback)
@@ -210,15 +198,14 @@ describe('fse-ensure-symlink', function () {
}
function dirError (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should return error when creating symlink dir using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function (done) {
- var dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
- function callback (err) {
+ const srcpath = args[0]
+ const dstpath = args[1]
+ it(`should return error when creating symlink dir using src ${srcpath} and dst ${dstpath}`, done => {
+ const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
+ const callback = err => {
assert.equal(err instanceof Error, true)
// ensure that directories aren't created if there's an error
- var dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
+ const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
assert.equal(dstdirExistsBefore, dstdirExistsAfter)
return done()
}
@@ -228,14 +215,13 @@ describe('fse-ensure-symlink', function () {
}
function dirDestExists (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function (done) {
- var destinationContentBefore = fs.readdirSync(dstpath)
- var callback = function (err) {
+ const srcpath = args[0]
+ const dstpath = args[1]
+ it(`should do nothing using src ${srcpath} and dst ${dstpath}`, done => {
+ const destinationContentBefore = fs.readdirSync(dstpath)
+ const callback = err => {
if (err) return done(err)
- var destinationContentAfter = fs.readdirSync(dstpath)
+ const destinationContentAfter = fs.readdirSync(dstpath)
assert.deepEqual(destinationContentBefore, destinationContentAfter)
return done()
}
@@ -245,18 +231,17 @@ describe('fse-ensure-symlink', function () {
}
function fileSuccessSync (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should create symlink file using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function () {
+ const srcpath = args[0]
+ const dstpath = args[1]
+ it(`should create symlink file using src ${srcpath} and dst ${dstpath}`, () => {
fn.apply(null, args)
- var relative = symlinkPathsSync(srcpath, dstpath)
- var srcContent = fs.readFileSync(relative.toCwd, 'utf8')
- var dstDir = path.dirname(dstpath)
- var dstBasename = path.basename(dstpath)
- var isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
- var dstContent = fs.readFileSync(dstpath, 'utf8')
- var dstDirContents = fs.readdirSync(dstDir)
+ const relative = symlinkPathsSync(srcpath, dstpath)
+ const srcContent = fs.readFileSync(relative.toCwd, 'utf8')
+ const dstDir = path.dirname(dstpath)
+ const dstBasename = path.basename(dstpath)
+ const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
+ const dstContent = fs.readFileSync(dstpath, 'utf8')
+ const dstDirContents = fs.readdirSync(dstDir)
assert.equal(isSymlink, true)
assert.equal(srcContent, dstContent)
assert(dstDirContents.indexOf(dstBasename) >= 0)
@@ -264,66 +249,60 @@ describe('fse-ensure-symlink', function () {
}
function fileBrokenSync (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should create broken symlink file using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function () {
+ const srcpath = args[0]
+ const dstpath = args[1]
+ it(`should create broken symlink file using src ${srcpath} and dst ${dstpath}`, () => {
fn.apply(null, args)
- var dstDir = path.dirname(dstpath)
- var dstBasename = path.basename(dstpath)
- var isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
- var dstDirContents = fs.readdirSync(dstDir)
+ const dstDir = path.dirname(dstpath)
+ const dstBasename = path.basename(dstpath)
+ const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
+ const dstDirContents = fs.readdirSync(dstDir)
assert.equal(isSymlink, true)
assert(dstDirContents.indexOf(dstBasename) >= 0)
- assert.throws(function () {
- return fs.readFileSync(dstpath, 'utf8')
- }, Error)
+ assert.throws(() => fs.readFileSync(dstpath, 'utf8'), Error)
})
}
function fileErrorSync (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should throw error using` src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function () {
- var dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
- var err = null
+ const srcpath = args[0]
+ const dstpath = args[1]
+ it(`should throw error using src ${srcpath} and dst ${dstpath}`, () => {
+ const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
+ let err = null
try {
fn.apply(null, args)
} catch (e) {
err = e
}
assert.equal(err instanceof Error, true)
- var dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
+ const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
assert.equal(dstdirExistsBefore, dstdirExistsAfter)
})
}
function fileDestExistsSync (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function () {
- var destinationContentBefore = fs.readFileSync(dstpath, 'utf8')
+ const srcpath = args[0]
+ const dstpath = args[1]
+ it(`should do nothing using src ${srcpath} and dst ${dstpath}`, () => {
+ const destinationContentBefore = fs.readFileSync(dstpath, 'utf8')
fn.apply(null, args)
- var destinationContentAfter = fs.readFileSync(dstpath, 'utf8')
+ const destinationContentAfter = fs.readFileSync(dstpath, 'utf8')
assert.equal(destinationContentBefore, destinationContentAfter)
})
}
function dirSuccessSync (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should create symlink dir using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function () {
+ const srcpath = args[0]
+ const dstpath = args[1]
+ it(`should create symlink dir using src ${srcpath} and dst ${dstpath}`, () => {
fn.apply(null, args)
- var relative = symlinkPathsSync(srcpath, dstpath)
- var srcContents = fs.readdirSync(relative.toCwd)
- var dstDir = path.dirname(dstpath)
- var dstBasename = path.basename(dstpath)
- var isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
- var dstContents = fs.readdirSync(dstpath)
- var dstDirContents = fs.readdirSync(dstDir)
+ const relative = symlinkPathsSync(srcpath, dstpath)
+ const srcContents = fs.readdirSync(relative.toCwd)
+ const dstDir = path.dirname(dstpath)
+ const dstBasename = path.basename(dstpath)
+ const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
+ const dstContents = fs.readdirSync(dstpath)
+ const dstDirContents = fs.readdirSync(dstDir)
assert.equal(isSymlink, true)
assert.deepEqual(srcContents, dstContents)
assert(dstDirContents.indexOf(dstBasename) >= 0)
@@ -331,59 +310,54 @@ describe('fse-ensure-symlink', function () {
}
function dirBrokenSync (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should create broken symlink dir using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function () {
+ const srcpath = args[0]
+ const dstpath = args[1]
+ it(`should create broken symlink dir using src ${srcpath} and dst ${dstpath}`, () => {
fn.apply(null, args)
- var dstDir = path.dirname(dstpath)
- var dstBasename = path.basename(dstpath)
- var isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
- var dstDirContents = fs.readdirSync(dstDir)
+ const dstDir = path.dirname(dstpath)
+ const dstBasename = path.basename(dstpath)
+ const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
+ const dstDirContents = fs.readdirSync(dstDir)
assert.equal(isSymlink, true)
assert(dstDirContents.indexOf(dstBasename) >= 0)
- assert.throws(function () {
- return fs.readdirSync(dstpath)
- }, Error)
+ assert.throws(() => fs.readdirSync(dstpath), Error)
})
}
function dirErrorSync (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should throw error when creating symlink dir using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function () {
- var dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
- var err = null
+ const srcpath = args[0]
+ const dstpath = args[1]
+ it(`should throw error when creating symlink dir using src ${srcpath} and dst ${dstpath}`, () => {
+ const dstdirExistsBefore = fs.existsSync(path.dirname(dstpath))
+ let err = null
try {
fn.apply(null, args)
} catch (e) {
err = e
}
assert.equal(err instanceof Error, true)
- var dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
+ const dstdirExistsAfter = fs.existsSync(path.dirname(dstpath))
assert.equal(dstdirExistsBefore, dstdirExistsAfter)
})
}
function dirDestExistsSync (args, fn) {
- var srcpath = args[0]
- var dstpath = args[1]
- var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath)
- it(should, function () {
- var destinationContentBefore = fs.readdirSync(dstpath)
+ const srcpath = args[0]
+ const dstpath = args[1]
+ it(`should do nothing using src ${srcpath} and dst ${dstpath}`, () => {
+ const destinationContentBefore = fs.readdirSync(dstpath)
fn.apply(null, args)
- var destinationContentAfter = fs.readdirSync(dstpath)
+ const destinationContentAfter = fs.readdirSync(dstpath)
assert.deepEqual(destinationContentBefore, destinationContentAfter)
})
}
- describe('fs.symlink()', function () {
- var fn = fs.symlink
- tests.forEach(function (test) {
- var args = test[0].slice(0)
- var nativeBehavior = test[1]
- // var newBehavior = test[2]
+ describe('fs.symlink()', () => {
+ const fn = fs.symlink
+ tests.forEach(test => {
+ const args = test[0].slice(0)
+ const nativeBehavior = test[1]
+ // const newBehavior = test[2]
if (nativeBehavior === 'file-success') fileSuccess(args, fn)
if (nativeBehavior === 'file-broken') fileBroken(args, fn)
if (nativeBehavior === 'file-error') fileError(args, fn)
@@ -396,12 +370,12 @@ describe('fse-ensure-symlink', function () {
})
})
- describe('ensureSymlink()', function () {
- var fn = ensureSymlink
- tests.forEach(function (test) {
- var args = test[0]
- // var nativeBehavior = test[1]
- var newBehavior = test[2]
+ describe('ensureSymlink()', () => {
+ const fn = ensureSymlink
+ tests.forEach(test => {
+ const args = test[0]
+ // const nativeBehavior = test[1]
+ const newBehavior = test[2]
if (newBehavior === 'file-success') fileSuccess(args, fn)
if (newBehavior === 'file-broken') fileBroken(args, fn)
if (newBehavior === 'file-error') fileError(args, fn)
@@ -413,12 +387,12 @@ describe('fse-ensure-symlink', function () {
})
})
- describe('fs.symlinkSync()', function () {
- var fn = fs.symlinkSync
- tests.forEach(function (test) {
- var args = test[0].slice(0)
- var nativeBehavior = test[1]
- // var newBehavior = test[2]
+ describe('fs.symlinkSync()', () => {
+ const fn = fs.symlinkSync
+ tests.forEach(test => {
+ const args = test[0].slice(0)
+ const nativeBehavior = test[1]
+ // const newBehavior = test[2]
if (nativeBehavior === 'file-success') fileSuccessSync(args, fn)
if (nativeBehavior === 'file-broken') fileBrokenSync(args, fn)
if (nativeBehavior === 'file-error') fileErrorSync(args, fn)
@@ -431,12 +405,12 @@ describe('fse-ensure-symlink', function () {
})
})
- describe('ensureSymlinkSync()', function () {
- var fn = ensureSymlinkSync
- tests.forEach(function (test) {
- var args = test[0]
- // var nativeBehavior = test[1]
- var newBehavior = test[2]
+ describe('ensureSymlinkSync()', () => {
+ const fn = ensureSymlinkSync
+ tests.forEach(test => {
+ const args = test[0]
+ // const nativeBehavior = test[1]
+ const newBehavior = test[2]
if (newBehavior === 'file-success') fileSuccessSync(args, fn)
if (newBehavior === 'file-broken') fileBrokenSync(args, fn)
if (newBehavior === 'file-error') fileErrorSync(args, fn)
diff --git a/lib/ensure/file.js b/lib/ensure/file.js
index 1c9c2de..031d130 100644
--- a/lib/ensure/file.js
+++ b/lib/ensure/file.js
@@ -1,21 +1,23 @@
-var path = require('path')
-var fs = require('graceful-fs')
-var mkdir = require('../mkdirs')
+'use strict'
+
+const path = require('path')
+const fs = require('graceful-fs')
+const mkdir = require('../mkdirs')
function createFile (file, callback) {
function makeFile () {
- fs.writeFile(file, '', function (err) {
+ fs.writeFile(file, '', err => {
if (err) return callback(err)
callback()
})
}
- fs.exists(file, function (fileExists) {
+ fs.exists(file, fileExists => {
if (fileExists) return callback()
- var dir = path.dirname(file)
- fs.exists(dir, function (dirExists) {
+ const dir = path.dirname(file)
+ fs.exists(dir, dirExists => {
if (dirExists) return makeFile()
- mkdir.mkdirs(dir, function (err) {
+ mkdir.mkdirs(dir, err => {
if (err) return callback(err)
makeFile()
})
@@ -26,7 +28,7 @@ function createFile (file, callback) {
function createFileSync (file) {
if (fs.existsSync(file)) return
- var dir = path.dirname(file)
+ const dir = path.dirname(file)
if (!fs.existsSync(dir)) {
mkdir.mkdirsSync(dir)
}
@@ -35,8 +37,8 @@ function createFileSync (file) {
}
module.exports = {
- createFile: createFile,
- createFileSync: createFileSync,
+ createFile,
+ createFileSync,
// alias
ensureFile: createFile,
ensureFileSync: createFileSync
diff --git a/lib/ensure/index.js b/lib/ensure/index.js
index 26e8705..c1f67b7 100644
--- a/lib/ensure/index.js
+++ b/lib/ensure/index.js
@@ -1,6 +1,8 @@
-var file = require('./file')
-var link = require('./link')
-var symlink = require('./symlink')
+'use strict'
+
+const file = require('./file')
+const link = require('./link')
+const symlink = require('./symlink')
module.exports = {
// file
diff --git a/lib/ensure/link.js b/lib/ensure/link.js
index 4e4e283..8284fae 100644
--- a/lib/ensure/link.js
+++ b/lib/ensure/link.js
@@ -1,27 +1,29 @@
-var path = require('path')
-var fs = require('graceful-fs')
-var mkdir = require('../mkdirs')
+'use strict'
+
+const path = require('path')
+const fs = require('graceful-fs')
+const mkdir = require('../mkdirs')
function createLink (srcpath, dstpath, callback) {
function makeLink (srcpath, dstpath) {
- fs.link(srcpath, dstpath, function (err) {
+ fs.link(srcpath, dstpath, err => {
if (err) return callback(err)
callback(null)
})
}
- fs.exists(dstpath, function (destinationExists) {
+ fs.exists(dstpath, destinationExists => {
if (destinationExists) return callback(null)
- fs.lstat(srcpath, function (err, stat) {
+ fs.lstat(srcpath, (err, stat) => {
if (err) {
err.message = err.message.replace('lstat', 'ensureLink')
return callback(err)
}
- var dir = path.dirname(dstpath)
- fs.exists(dir, function (dirExists) {
+ const dir = path.dirname(dstpath)
+ fs.exists(dir, dirExists => {
if (dirExists) return makeLink(srcpath, dstpath)
- mkdir.mkdirs(dir, function (err) {
+ mkdir.mkdirs(dir, err => {
if (err) return callback(err)
makeLink(srcpath, dstpath)
})
@@ -31,7 +33,7 @@ function createLink (srcpath, dstpath, callback) {
}
function createLinkSync (srcpath, dstpath, callback) {
- var destinationExists = fs.existsSync(dstpath)
+ const destinationExists = fs.existsSync(dstpath)
if (destinationExists) return undefined
try {
@@ -41,8 +43,8 @@ function createLinkSync (srcpath, dstpath, callback) {
throw err
}
- var dir = path.dirname(dstpath)
- var dirExists = fs.existsSync(dir)
+ const dir = path.dirname(dstpath)
+ const dirExists = fs.existsSync(dir)
if (dirExists) return fs.linkSync(srcpath, dstpath)
mkdir.mkdirsSync(dir)
@@ -50,8 +52,8 @@ function createLinkSync (srcpath, dstpath, callback) {
}
module.exports = {
- createLink: createLink,
- createLinkSync: createLinkSync,
+ createLink,
+ createLinkSync,
// alias
ensureLink: createLink,
ensureLinkSync: createLinkSync
diff --git a/lib/ensure/symlink-paths.js b/lib/ensure/symlink-paths.js
index eebc12e..e52d039 100644
--- a/lib/ensure/symlink-paths.js
+++ b/lib/ensure/symlink-paths.js
@@ -1,6 +1,7 @@
-var path = require('path')
-// path.isAbsolute shim for Node.js 0.10 support
-var fs = require('graceful-fs')
+'use strict'
+
+const path = require('path')
+const fs = require('graceful-fs')
/**
* Function that returns two types of paths, one relative to symlink, and one
@@ -26,7 +27,7 @@ var fs = require('graceful-fs')
function symlinkPaths (srcpath, dstpath, callback) {
if (path.isAbsolute(srcpath)) {
- return fs.lstat(srcpath, function (err, stat) {
+ return fs.lstat(srcpath, (err, stat) => {
if (err) {
err.message = err.message.replace('lstat', 'ensureSymlink')
return callback(err)
@@ -37,16 +38,16 @@ function symlinkPaths (srcpath, dstpath, callback) {
})
})
} else {
- var dstdir = path.dirname(dstpath)
- var relativeToDst = path.join(dstdir, srcpath)
- return fs.exists(relativeToDst, function (exists) {
+ const dstdir = path.dirname(dstpath)
+ const relativeToDst = path.join(dstdir, srcpath)
+ return fs.exists(relativeToDst, exists => {
if (exists) {
return callback(null, {
'toCwd': relativeToDst,
'toDst': srcpath
})
} else {
- return fs.lstat(srcpath, function (err, stat) {
+ return fs.lstat(srcpath, (err, stat) => {
if (err) {
err.message = err.message.replace('lstat', 'ensureSymlink')
return callback(err)
@@ -62,7 +63,7 @@ function symlinkPaths (srcpath, dstpath, callback) {
}
function symlinkPathsSync (srcpath, dstpath) {
- var exists
+ let exists
if (path.isAbsolute(srcpath)) {
exists = fs.existsSync(srcpath)
if (!exists) throw new Error('absolute srcpath does not exist')
@@ -71,8 +72,8 @@ function symlinkPathsSync (srcpath, dstpath) {
'toDst': srcpath
}
} else {
- var dstdir = path.dirname(dstpath)
- var relativeToDst = path.join(dstdir, srcpath)
+ const dstdir = path.dirname(dstpath)
+ const relativeToDst = path.join(dstdir, srcpath)
exists = fs.existsSync(relativeToDst)
if (exists) {
return {
@@ -91,6 +92,6 @@ function symlinkPathsSync (srcpath, dstpath) {
}
module.exports = {
- 'symlinkPaths': symlinkPaths,
- 'symlinkPathsSync': symlinkPathsSync
+ symlinkPaths,
+ symlinkPathsSync
}
diff --git a/lib/ensure/symlink-type.js b/lib/ensure/symlink-type.js
index 81e3588..4f8787c 100644
--- a/lib/ensure/symlink-type.js
+++ b/lib/ensure/symlink-type.js
@@ -1,10 +1,12 @@
-var fs = require('graceful-fs')
+'use strict'
+
+const fs = require('graceful-fs')
function symlinkType (srcpath, type, callback) {
callback = (typeof type === 'function') ? type : callback
type = (typeof type === 'function') ? false : type
if (type) return callback(null, type)
- fs.lstat(srcpath, function (err, stats) {
+ fs.lstat(srcpath, (err, stats) => {
if (err) return callback(null, 'file')
type = (stats && stats.isDirectory()) ? 'dir' : 'file'
callback(null, type)
@@ -12,9 +14,11 @@ function symlinkType (srcpath, type, callback) {
}
function symlinkTypeSync (srcpath, type) {
+ let stats
+
if (type) return type
try {
- var stats = fs.lstatSync(srcpath)
+ stats = fs.lstatSync(srcpath)
} catch (e) {
return 'file'
}
@@ -22,6 +26,6 @@ function symlinkTypeSync (srcpath, type) {
}
module.exports = {
- symlinkType: symlinkType,
- symlinkTypeSync: symlinkTypeSync
+ symlinkType,
+ symlinkTypeSync
}
diff --git a/lib/ensure/symlink.js b/lib/ensure/symlink.js
index 6244790..2ea9e06 100644
--- a/lib/ensure/symlink.js
+++ b/lib/ensure/symlink.js
@@ -1,32 +1,34 @@
-var path = require('path')
-var fs = require('graceful-fs')
-var _mkdirs = require('../mkdirs')
-var mkdirs = _mkdirs.mkdirs
-var mkdirsSync = _mkdirs.mkdirsSync
+'use strict'
-var _symlinkPaths = require('./symlink-paths')
-var symlinkPaths = _symlinkPaths.symlinkPaths
-var symlinkPathsSync = _symlinkPaths.symlinkPathsSync
+const path = require('path')
+const fs = require('graceful-fs')
+const _mkdirs = require('../mkdirs')
+const mkdirs = _mkdirs.mkdirs
+const mkdirsSync = _mkdirs.mkdirsSync
-var _symlinkType = require('./symlink-type')
-var symlinkType = _symlinkType.symlinkType
-var symlinkTypeSync = _symlinkType.symlinkTypeSync
+const _symlinkPaths = require('./symlink-paths')
+const symlinkPaths = _symlinkPaths.symlinkPaths
+const symlinkPathsSync = _symlinkPaths.symlinkPathsSync
+
+const _symlinkType = require('./symlink-type')
+const symlinkType = _symlinkType.symlinkType
+const symlinkTypeSync = _symlinkType.symlinkTypeSync
function createSymlink (srcpath, dstpath, type, callback) {
callback = (typeof type === 'function') ? type : callback
type = (typeof type === 'function') ? false : type
- fs.exists(dstpath, function (destinationExists) {
+ fs.exists(dstpath, destinationExists => {
if (destinationExists) return callback(null)
- symlinkPaths(srcpath, dstpath, function (err, relative) {
+ symlinkPaths(srcpath, dstpath, (err, relative) => {
if (err) return callback(err)
srcpath = relative.toDst
- symlinkType(relative.toCwd, type, function (err, type) {
+ symlinkType(relative.toCwd, type, (err, type) => {
if (err) return callback(err)
- var dir = path.dirname(dstpath)
- fs.exists(dir, function (dirExists) {
+ const dir = path.dirname(dstpath)
+ fs.exists(dir, dirExists => {
if (dirExists) return fs.symlink(srcpath, dstpath, type, callback)
- mkdirs(dir, function (err) {
+ mkdirs(dir, err => {
if (err) return callback(err)
fs.symlink(srcpath, dstpath, type, callback)
})
@@ -40,22 +42,22 @@ function createSymlinkSync (srcpath, dstpath, type, callback) {
callback = (typeof type === 'function') ? type : callback
type = (typeof type === 'function') ? false : type
- var destinationExists = fs.existsSync(dstpath)
+ const destinationExists = fs.existsSync(dstpath)
if (destinationExists) return undefined
- var relative = symlinkPathsSync(srcpath, dstpath)
+ const relative = symlinkPathsSync(srcpath, dstpath)
srcpath = relative.toDst
type = symlinkTypeSync(relative.toCwd, type)
- var dir = path.dirname(dstpath)
- var exists = fs.existsSync(dir)
+ const dir = path.dirname(dstpath)
+ const exists = fs.existsSync(dir)
if (exists) return fs.symlinkSync(srcpath, dstpath, type)
mkdirsSync(dir)
return fs.symlinkSync(srcpath, dstpath, type)
}
module.exports = {
- createSymlink: createSymlink,
- createSymlinkSync: createSymlinkSync,
+ createSymlink,
+ createSymlinkSync,
// alias
ensureSymlink: createSymlink,
ensureSymlinkSync: createSymlinkSync
diff --git a/lib/index.js b/lib/index.js
index 977a75b..d25fd54 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -1,14 +1,16 @@
-var assign = require('./util/assign')
+'use strict'
-var fse = {}
-var gfs = require('graceful-fs')
+const assign = require('./util/assign')
+
+const fse = {}
+const gfs = require('graceful-fs')
// attach fs methods to fse
-Object.keys(gfs).forEach(function (key) {
+Object.keys(gfs).forEach(key => {
fse[key] = gfs[key]
})
-var fs = fse
+const fs = fse
assign(fs, require('./copy'))
assign(fs, require('./copy-sync'))
@@ -16,6 +18,7 @@ assign(fs, require('./mkdirs'))
assign(fs, require('./remove'))
assign(fs, require('./json'))
assign(fs, require('./move'))
+assign(fs, require('./move-sync'))
assign(fs, require('./empty'))
assign(fs, require('./ensure'))
assign(fs, require('./output'))
@@ -23,12 +26,10 @@ assign(fs, require('./output'))
module.exports = fs
// maintain backwards compatibility for awhile
-var jsonfile = {}
+const jsonfile = {}
Object.defineProperty(jsonfile, 'spaces', {
- get: function () {
- return fs.spaces // found in ./json
- },
- set: function (val) {
+ get: () => fs.spaces, // found in ./json
+ set: val => {
fs.spaces = val
}
})
diff --git a/lib/json/__tests__/jsonfile-integration.test.js b/lib/json/__tests__/jsonfile-integration.test.js
index 925b2dc..27f7665 100644
--- a/lib/json/__tests__/jsonfile-integration.test.js
+++ b/lib/json/__tests__/jsonfile-integration.test.js
@@ -1,36 +1,36 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-describe('jsonfile-integration', function () {
- var TEST_DIR
+describe('jsonfile-integration', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'json')
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- describe('+ writeJsonSync / spaces', function () {
- it('should read a file and parse the json', function () {
- var obj1 = {
+ describe('+ writeJsonSync / spaces', () => {
+ it('should read a file and parse the json', () => {
+ const obj1 = {
firstName: 'JP',
lastName: 'Richardson'
}
- var oldSpaces = fse.jsonfile.spaces
+ const oldSpaces = fse.jsonfile.spaces
fse.jsonfile.spaces = 4
- var file = path.join(TEST_DIR, 'file.json')
+ const file = path.join(TEST_DIR, 'file.json')
fse.writeJsonSync(file, obj1)
- var data = fs.readFileSync(file, 'utf8')
+ const data = fs.readFileSync(file, 'utf8')
assert.strictEqual(data, JSON.stringify(obj1, null, 4) + '\n')
fse.jsonfile.spaces = oldSpaces
diff --git a/lib/json/__tests__/output-json-sync.test.js b/lib/json/__tests__/output-json-sync.test.js
index 9d4ddd0..aa2d77f 100644
--- a/lib/json/__tests__/output-json-sync.test.js
+++ b/lib/json/__tests__/output-json-sync.test.js
@@ -1,45 +1,46 @@
-var assert = require('assert')
-var fs = require('fs')
-var os = require('os')
-var path = require('path')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
+const outputJsonSync = require('../output-json-sync')
/* global beforeEach, describe, it */
-describe('json', function () {
- var TEST_DIR
+describe('json', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra')
fse.emptyDir(TEST_DIR, done)
})
- describe('+ outputJsonSync(file, data)', function () {
- var outputJsonSync = require('../output-json-sync')
-
- it('should write the file regardless of whether the directory exists or not', function () {
- var file = path.join(TEST_DIR, 'this-dir', 'does-not', 'exist', 'file.json')
+ describe('+ outputJsonSync(file, data)', () => {
+ it('should write the file regardless of whether the directory exists or not', () => {
+ const file = path.join(TEST_DIR, 'this-dir', 'does-not', 'exist', 'file.json')
assert(!fs.existsSync(file))
- var data = {name: 'JP'}
+ const data = {name: 'JP'}
outputJsonSync(file, data)
assert(fs.existsSync(file))
- var newData = JSON.parse(fs.readFileSync(file, 'utf8'))
+ const newData = JSON.parse(fs.readFileSync(file, 'utf8'))
assert.equal(data.name, newData.name)
})
- describe('> when an option is passed, like JSON replacer', function () {
- it('should pass the option along to jsonfile module', function () {
- var file = path.join(TEST_DIR, 'this-dir', 'does-not', 'exist', 'really', 'file.json')
+ describe('> when an option is passed, like JSON replacer', () => {
+ it('should pass the option along to jsonfile module', () => {
+ const file = path.join(TEST_DIR, 'this-dir', 'does-not', 'exist', 'really', 'file.json')
assert(!fs.existsSync(file))
- var replacer = function (k, v) { if (v === 'JP') return 'Jon Paul'; else return v }
- var data = {name: 'JP'}
+ const replacer = (k, v) => v === 'JP' ? 'Jon Paul' : v
+ const data = {name: 'JP'}
- outputJsonSync(file, data, {replacer: replacer})
- var newData = JSON.parse(fs.readFileSync(file, 'utf8'))
+ outputJsonSync(file, data, { replacer })
+ const newData = JSON.parse(fs.readFileSync(file, 'utf8'))
assert.equal(newData.name, 'Jon Paul')
})
diff --git a/lib/json/__tests__/output-json.test.js b/lib/json/__tests__/output-json.test.js
index 8e6cd4c..03b4714 100644
--- a/lib/json/__tests__/output-json.test.js
+++ b/lib/json/__tests__/output-json.test.js
@@ -1,49 +1,50 @@
-var assert = require('assert')
-var fs = require('fs')
-var os = require('os')
-var path = require('path')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
+const outputJson = require('../output-json')
/* global beforeEach, describe, it */
-describe('json', function () {
- var TEST_DIR
+describe('json', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra')
fse.emptyDir(TEST_DIR, done)
})
- describe('+ outputJson(file, data)', function () {
- var outputJson = require('../output-json')
-
- it('should write the file regardless of whether the directory exists or not', function (done) {
- var file = path.join(TEST_DIR, 'this-dir', 'prob-does-not', 'exist', 'file.json')
+ describe('+ outputJson(file, data)', () => {
+ it('should write the file regardless of whether the directory exists or not', done => {
+ const file = path.join(TEST_DIR, 'this-dir', 'prob-does-not', 'exist', 'file.json')
assert(!fs.existsSync(file))
- var data = {name: 'JP'}
- outputJson(file, data, function (err) {
+ const data = {name: 'JP'}
+ outputJson(file, data, err => {
if (err) return done(err)
assert(fs.existsSync(file))
- var newData = JSON.parse(fs.readFileSync(file, 'utf8'))
+ const newData = JSON.parse(fs.readFileSync(file, 'utf8'))
assert.equal(data.name, newData.name)
done()
})
})
- describe('> when an option is passed, like JSON replacer', function () {
- it('should pass the option along to jsonfile module', function (done) {
- var file = path.join(TEST_DIR, 'this-dir', 'does-not', 'exist', 'really', 'file.json')
+ describe('> when an option is passed, like JSON replacer', () => {
+ it('should pass the option along to jsonfile module', done => {
+ const file = path.join(TEST_DIR, 'this-dir', 'does-not', 'exist', 'really', 'file.json')
assert(!fs.existsSync(file))
- var replacer = function (k, v) { if (v === 'JP') return 'Jon Paul'; else return v }
- var data = {name: 'JP'}
+ const replacer = (k, v) => v === 'JP' ? 'Jon Paul' : v
+ const data = {name: 'JP'}
- outputJson(file, data, {replacer: replacer}, function (err) {
+ outputJson(file, data, { replacer }, err => {
assert.ifError(err)
- var newData = JSON.parse(fs.readFileSync(file, 'utf8'))
+ const newData = JSON.parse(fs.readFileSync(file, 'utf8'))
assert.equal(newData.name, 'Jon Paul')
done()
})
diff --git a/lib/json/__tests__/read.test.js b/lib/json/__tests__/read.test.js
index 34bee4c..f26f221 100644
--- a/lib/json/__tests__/read.test.js
+++ b/lib/json/__tests__/read.test.js
@@ -1,45 +1,44 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-describe('read', function () {
- var TEST_DIR
+describe('read', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'read-json')
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- describe('+ readJSON', function () {
- it('should read a file and parse the json', function (done) {
- var obj1 = {
+ describe('+ readJSON', () => {
+ it('should read a file and parse the json', done => {
+ const obj1 = {
firstName: 'JP',
lastName: 'Richardson'
}
- var file = path.join(TEST_DIR, 'file.json')
+ const file = path.join(TEST_DIR, 'file.json')
fs.writeFileSync(file, JSON.stringify(obj1))
- fse.readJSON(file, function (err, obj2) {
+ fse.readJSON(file, (err, obj2) => {
assert.ifError(err)
assert.strictEqual(obj1.firstName, obj2.firstName)
assert.strictEqual(obj1.lastName, obj2.lastName)
-
done()
})
})
- it('should error if it cant parse the json', function (done) {
- var file = path.join(TEST_DIR, 'file2.json')
+ it('should error if it cant parse the json', done => {
+ const file = path.join(TEST_DIR, 'file2.json')
fs.writeFileSync(file, '%asdfasdff444')
- fse.readJSON(file, function (err, obj) {
+ fse.readJSON(file, (err, obj) => {
assert(err)
assert(!obj)
done()
diff --git a/lib/json/__tests__/spaces.test.js b/lib/json/__tests__/spaces.test.js
index f878688..ab428c8 100644
--- a/lib/json/__tests__/spaces.test.js
+++ b/lib/json/__tests__/spaces.test.js
@@ -1,34 +1,34 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require('../../')
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require('../../')
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
// trinity: mocha
-describe('json spaces', function () {
- var TEST_DIR
+describe('json spaces', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'json-spaces')
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- it('should write out the file with appropriate spacing (2)', function () {
+ it('should write out the file with appropriate spacing (2)', () => {
fse.spaces = 2 // for legacy compatibility
assert.strictEqual(fse.spaces, 2)
- var tempFile = path.join(TEST_DIR, 'temp.json')
+ const tempFile = path.join(TEST_DIR, 'temp.json')
- var data = { first: 'JP', last: 'Richardson' }
+ const data = { first: 'JP', last: 'Richardson' }
fse.outputJsonSync(tempFile, data)
- var dataRead = fs.readFileSync(tempFile, 'utf8')
+ const dataRead = fs.readFileSync(tempFile, 'utf8')
assert.strictEqual(dataRead, JSON.stringify(data, null, 2) + '\n')
})
})
diff --git a/lib/json/index.js b/lib/json/index.js
index b13cf54..4229311 100644
--- a/lib/json/index.js
+++ b/lib/json/index.js
@@ -1,4 +1,6 @@
-var jsonFile = require('./jsonfile')
+'use strict'
+
+const jsonFile = require('./jsonfile')
jsonFile.outputJsonSync = require('./output-json-sync')
jsonFile.outputJson = require('./output-json')
diff --git a/lib/json/jsonfile.js b/lib/json/jsonfile.js
index 51d8390..e3630ee 100644
--- a/lib/json/jsonfile.js
+++ b/lib/json/jsonfile.js
@@ -1,4 +1,6 @@
-var jsonFile = require('jsonfile')
+'use strict'
+
+const jsonFile = require('jsonfile')
module.exports = {
// jsonfile exports
diff --git a/lib/json/output-json-sync.js b/lib/json/output-json-sync.js
index 7684843..6f76710 100644
--- a/lib/json/output-json-sync.js
+++ b/lib/json/output-json-sync.js
@@ -1,10 +1,12 @@
-var fs = require('graceful-fs')
-var path = require('path')
-var jsonFile = require('./jsonfile')
-var mkdir = require('../mkdirs')
+'use strict'
+
+const fs = require('graceful-fs')
+const path = require('path')
+const mkdir = require('../mkdirs')
+const jsonFile = require('./jsonfile')
function outputJsonSync (file, data, options) {
- var dir = path.dirname(file)
+ const dir = path.dirname(file)
if (!fs.existsSync(dir)) {
mkdir.mkdirsSync(dir)
diff --git a/lib/json/output-json.js b/lib/json/output-json.js
index 7824597..2f928c5 100644
--- a/lib/json/output-json.js
+++ b/lib/json/output-json.js
@@ -1,7 +1,9 @@
-var fs = require('graceful-fs')
-var path = require('path')
-var jsonFile = require('./jsonfile')
-var mkdir = require('../mkdirs')
+'use strict'
+
+const fs = require('graceful-fs')
+const path = require('path')
+const mkdir = require('../mkdirs')
+const jsonFile = require('./jsonfile')
function outputJson (file, data, options, callback) {
if (typeof options === 'function') {
@@ -9,12 +11,12 @@ function outputJson (file, data, options, callback) {
options = {}
}
- var dir = path.dirname(file)
+ const dir = path.dirname(file)
- fs.exists(dir, function (itDoes) {
+ fs.exists(dir, itDoes => {
if (itDoes) return jsonFile.writeJson(file, data, options, callback)
- mkdir.mkdirs(dir, function (err) {
+ mkdir.mkdirs(dir, err => {
if (err) return callback(err)
jsonFile.writeJson(file, data, options, callback)
})
diff --git a/lib/mkdirs/__tests__/chmod.test.js b/lib/mkdirs/__tests__/chmod.test.js
index cb1b18a..5063d9d 100644
--- a/lib/mkdirs/__tests__/chmod.test.js
+++ b/lib/mkdirs/__tests__/chmod.test.js
@@ -1,24 +1,26 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require('../../')
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require('../../')
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-var o755 = parseInt('755', 8)
-var o744 = parseInt('744', 8)
-var o777 = parseInt('777', 8)
-var o666 = parseInt('666', 8)
+const o755 = parseInt('755', 8)
+const o744 = parseInt('744', 8)
+const o777 = parseInt('777', 8)
+const o666 = parseInt('666', 8)
-describe('mkdirp / chmod', function () {
- var TEST_DIR
- var TEST_SUBDIR
+describe('mkdirp / chmod', () => {
+ let TEST_DIR
+ let TEST_SUBDIR
- beforeEach(function (done) {
- var ps = []
- for (var i = 0; i < 15; i++) {
- var dir = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ beforeEach(done => {
+ const ps = []
+ for (let i = 0; i < 15; i++) {
+ const dir = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
ps.push(dir)
}
@@ -30,16 +32,14 @@ describe('mkdirp / chmod', function () {
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- it('chmod-pre', function (done) {
- var mode = o744
- fse.mkdirp(TEST_SUBDIR, mode, function (er) {
- assert.ifError(er, 'should not error')
- fs.stat(TEST_SUBDIR, function (er, stat) {
- assert.ifError(er, 'should exist')
+ it('chmod-pre', done => {
+ const mode = o744
+ fse.mkdirp(TEST_SUBDIR, mode, err => {
+ assert.ifError(err, 'should not error')
+ fs.stat(TEST_SUBDIR, (err, stat) => {
+ assert.ifError(err, 'should exist')
assert.ok(stat && stat.isDirectory(), 'should be directory')
if (os.platform().indexOf('win') === 0) {
@@ -53,12 +53,12 @@ describe('mkdirp / chmod', function () {
})
})
- it('chmod', function (done) {
- var mode = o755
- fse.mkdirp(TEST_SUBDIR, mode, function (er) {
- assert.ifError(er, 'should not error')
- fs.stat(TEST_SUBDIR, function (er, stat) {
- assert.ifError(er, 'should exist')
+ it('chmod', done => {
+ const mode = o755
+ fse.mkdirp(TEST_SUBDIR, mode, err => {
+ assert.ifError(err, 'should not error')
+ fs.stat(TEST_SUBDIR, (err, stat) => {
+ assert.ifError(err, 'should exist')
assert.ok(stat && stat.isDirectory(), 'should be directory')
done()
})
diff --git a/lib/mkdirs/__tests__/clobber.test.js b/lib/mkdirs/__tests__/clobber.test.js
index f69533b..67d8811 100644
--- a/lib/mkdirs/__tests__/clobber.test.js
+++ b/lib/mkdirs/__tests__/clobber.test.js
@@ -1,46 +1,48 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global before, describe, it */
-var o755 = parseInt('755', 8)
+const o755 = parseInt('755', 8)
-describe('mkdirp / clobber', function () {
- var TEST_DIR
- var file
+describe('mkdirp / clobber', () => {
+ let TEST_DIR
+ let file
- before(function (done) {
+ before(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-clobber')
- fse.emptyDir(TEST_DIR, function (err) {
+ fse.emptyDir(TEST_DIR, err => {
assert.ifError(err)
- var ps = [TEST_DIR]
+ const ps = [ TEST_DIR ]
- for (var i = 0; i < 15; i++) {
- var dir = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ for (let i = 0; i < 15; i++) {
+ const dir = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
ps.push(dir)
}
file = ps.join(path.sep)
// a file in the way
- var itw = ps.slice(0, 2).join(path.sep)
+ const itw = ps.slice(0, 2).join(path.sep)
fs.writeFileSync(itw, 'I AM IN THE WAY, THE TRUTH, AND THE LIGHT.')
- fs.stat(itw, function (er, stat) {
- assert.ifError(er)
+ fs.stat(itw, (err, stat) => {
+ assert.ifError(err)
assert.ok(stat && stat.isFile(), 'should be file')
done()
})
})
})
- it('should clobber', function (done) {
- fse.mkdirp(file, o755, function (err) {
+ it('should clobber', done => {
+ fse.mkdirp(file, o755, err => {
assert.ok(err)
if (os.platform().indexOf('win') === 0) {
assert.equal(err.code, 'EEXIST')
diff --git a/lib/mkdirs/__tests__/issue-209.test.js b/lib/mkdirs/__tests__/issue-209.test.js
index 2640eb9..7b16b81 100644
--- a/lib/mkdirs/__tests__/issue-209.test.js
+++ b/lib/mkdirs/__tests__/issue-209.test.js
@@ -1,20 +1,22 @@
-var assert = require('assert')
-var fse = require(process.cwd())
+'use strict'
+
+const assert = require('assert')
+const fse = require(process.cwd())
/* global describe, it */
-describe('mkdirp: issue-209, win32, when bad path, should return a cleaner error', function () {
+describe('mkdirp: issue-209, win32, when bad path, should return a cleaner error', () => {
// only seems to be an issue on Windows.
if (process.platform !== 'win32') return
- it('should return a callback', function (done) {
- var file = './bad?dir'
- fse.mkdirp(file, function (err) {
+ it('should return a callback', done => {
+ const file = './bad?dir'
+ fse.mkdirp(file, err => {
assert(err, 'error is present')
assert.strictEqual(err.code, 'EINVAL')
- var file2 = 'c:\\tmp\foo:moo'
- fse.mkdirp(file2, function (err) {
+ const file2 = 'c:\\tmp\foo:moo'
+ fse.mkdirp(file2, err => {
assert(err, 'error is present')
assert.strictEqual(err.code, 'EINVAL')
done()
@@ -22,11 +24,11 @@ describe('mkdirp: issue-209, win32, when bad path, should return a cleaner error
})
})
- describe('> sync', function () {
- it('should throw an error', function () {
- var didErr
+ describe('> sync', () => {
+ it('should throw an error', () => {
+ let didErr
try {
- var file = 'c:\\tmp\foo:moo'
+ const file = 'c:\\tmp\foo:moo'
fse.mkdirpSync(file)
} catch (err) {
// console.error(err)
diff --git a/lib/mkdirs/__tests__/issue-93.test.js b/lib/mkdirs/__tests__/issue-93.test.js
index f7fc1fb..771694e 100644
--- a/lib/mkdirs/__tests__/issue-93.test.js
+++ b/lib/mkdirs/__tests__/issue-93.test.js
@@ -1,27 +1,29 @@
-var assert = require('assert')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+'use strict'
+
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global before, describe, it */
-describe('mkdirp: issue-93, win32, when drive does not exist, it should return a cleaner error', function () {
- var TEST_DIR
+describe('mkdirp: issue-93, win32, when drive does not exist, it should return a cleaner error', () => {
+ let TEST_DIR
// only seems to be an issue on Windows.
if (process.platform !== 'win32') return
- before(function (done) {
+ before(done => {
TEST_DIR = path.join(os.tmpdir(), 'tests', 'fs-extra', 'mkdirp-issue-93')
- fse.emptyDir(TEST_DIR, function (err) {
+ fse.emptyDir(TEST_DIR, err => {
assert.ifError(err)
done()
})
})
- it('should return a cleaner error than inifinite loop, stack crash', function (done) {
- var file = 'R:\\afasd\\afaff\\fdfd' // hopefully drive 'r' does not exist on appveyor
- fse.mkdirp(file, function (err) {
+ it('should return a cleaner error than inifinite loop, stack crash', done => {
+ const file = 'R:\\afasd\\afaff\\fdfd' // hopefully drive 'r' does not exist on appveyor
+ fse.mkdirp(file, err => {
assert.strictEqual(err.code, 'ENOENT')
try {
diff --git a/lib/mkdirs/__tests__/mkdir.test.js b/lib/mkdirs/__tests__/mkdir.test.js
index adb3eeb..35cb7e6 100644
--- a/lib/mkdirs/__tests__/mkdir.test.js
+++ b/lib/mkdirs/__tests__/mkdir.test.js
@@ -1,43 +1,43 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-describe('fs-extra', function () {
- var TEST_DIR
+describe('fs-extra', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdir')
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- describe('+ mkdirs()', function () {
- it('should make the directory', function (done) {
- var dir = path.join(TEST_DIR, 'tmp-' + Date.now() + Math.random())
+ describe('+ mkdirs()', () => {
+ it('should make the directory', done => {
+ const dir = path.join(TEST_DIR, 'tmp-' + Date.now() + Math.random())
assert(!fs.existsSync(dir))
- fse.mkdirs(dir, function (err) {
+ fse.mkdirs(dir, err => {
assert.ifError(err)
assert(fs.existsSync(dir))
done()
})
})
- it('should make the entire directory path', function (done) {
- var dir = path.join(TEST_DIR, 'tmp-' + Date.now() + Math.random())
- var newDir = path.join(TEST_DIR, 'dfdf', 'ffff', 'aaa')
+ it('should make the entire directory path', done => {
+ const dir = path.join(TEST_DIR, 'tmp-' + Date.now() + Math.random())
+ const newDir = path.join(TEST_DIR, 'dfdf', 'ffff', 'aaa')
assert(!fs.existsSync(dir))
- fse.mkdirs(newDir, function (err) {
+ fse.mkdirs(newDir, err => {
assert.ifError(err)
assert(fs.existsSync(newDir))
done()
@@ -45,9 +45,9 @@ describe('fs-extra', function () {
})
})
- describe('+ mkdirsSync()', function () {
- it('should make the directory', function (done) {
- var dir = path.join(TEST_DIR, 'tmp-' + Date.now() + Math.random())
+ describe('+ mkdirsSync()', () => {
+ it('should make the directory', done => {
+ const dir = path.join(TEST_DIR, 'tmp-' + Date.now() + Math.random())
assert(!fs.existsSync(dir))
fse.mkdirsSync(dir)
@@ -56,9 +56,9 @@ describe('fs-extra', function () {
done()
})
- it('should make the entire directory path', function (done) {
- var dir = path.join(TEST_DIR, 'tmp-' + Date.now() + Math.random())
- var newDir = path.join(dir, 'dfdf', 'ffff', 'aaa')
+ it('should make the entire directory path', done => {
+ const dir = path.join(TEST_DIR, 'tmp-' + Date.now() + Math.random())
+ const newDir = path.join(dir, 'dfdf', 'ffff', 'aaa')
assert(!fs.existsSync(newDir))
fse.mkdirsSync(newDir)
diff --git a/lib/mkdirs/__tests__/mkdirp.test.js b/lib/mkdirs/__tests__/mkdirp.test.js
index 10b1f71..7ad3868 100644
--- a/lib/mkdirs/__tests__/mkdirp.test.js
+++ b/lib/mkdirs/__tests__/mkdirp.test.js
@@ -1,39 +1,39 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-var o755 = parseInt('755', 8)
-var o777 = parseInt('777', 8)
-var o666 = parseInt('666', 8)
+const o755 = parseInt('755', 8)
+const o777 = parseInt('777', 8)
+const o666 = parseInt('666', 8)
-describe('mkdirp / mkdirp', function () {
- var TEST_DIR
+describe('mkdirp / mkdirp', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp')
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- it('should make the dir', function (done) {
- var x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
- var y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
- var z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ it('should make the dir', done => {
+ const x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ const y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ const z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
- var file = path.join(TEST_DIR, x, y, z)
+ const file = path.join(TEST_DIR, x, y, z)
- fse.mkdirp(file, o755, function (err) {
+ fse.mkdirp(file, o755, err => {
assert.ifError(err)
- fs.exists(file, function (ex) {
+ fs.exists(file, ex => {
assert.ok(ex, 'file created')
- fs.stat(file, function (err, stat) {
+ fs.stat(file, (err, stat) => {
assert.ifError(err)
if (os.platform().indexOf('win') === 0) {
diff --git a/lib/mkdirs/__tests__/opts-undef.test.js b/lib/mkdirs/__tests__/opts-undef.test.js
index 096f77e..e72e79b 100644
--- a/lib/mkdirs/__tests__/opts-undef.test.js
+++ b/lib/mkdirs/__tests__/opts-undef.test.js
@@ -1,26 +1,28 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
-var mkdirs = require('../mkdirs')
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
+const mkdirs = require('../mkdirs')
/* global beforeEach, describe, it */
-describe('mkdirs / opts-undef', function () {
- var TEST_DIR
+describe('mkdirs / opts-undef', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirs')
fse.emptyDir(TEST_DIR, done)
})
// https://github.com/substack/node-mkdirp/issues/45
- it('should not hang', function (done) {
- var newDir = path.join(TEST_DIR, 'doest', 'not', 'exist')
+ it('should not hang', done => {
+ const newDir = path.join(TEST_DIR, 'doest', 'not', 'exist')
assert(!fs.existsSync(newDir))
- mkdirs(newDir, undefined, function (err) {
+ mkdirs(newDir, undefined, err => {
assert.ifError(err)
assert(fs.existsSync(newDir))
done()
diff --git a/lib/mkdirs/__tests__/perm.test.js b/lib/mkdirs/__tests__/perm.test.js
index d265086..873ed6a 100644
--- a/lib/mkdirs/__tests__/perm.test.js
+++ b/lib/mkdirs/__tests__/perm.test.js
@@ -1,35 +1,35 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require('../../')
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require('../../')
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-var o755 = parseInt('755', 8)
-var o777 = parseInt('777', 8)
-var o666 = parseInt('666', 8)
+const o755 = parseInt('755', 8)
+const o777 = parseInt('777', 8)
+const o666 = parseInt('666', 8)
-describe('mkdirp / perm', function () {
- var TEST_DIR
+describe('mkdirp / perm', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-perm')
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- it('async perm', function (done) {
- var file = path.join(TEST_DIR, (Math.random() * (1 << 30)).toString(16))
+ it('async perm', done => {
+ let file = path.join(TEST_DIR, (Math.random() * (1 << 30)).toString(16))
- fse.mkdirp(file, o755, function (err) {
+ fse.mkdirp(file, o755, err => {
assert.ifError(err)
- fs.exists(file, function (ex) {
+ fs.exists(file, ex => {
assert.ok(ex, 'file created')
- fs.stat(file, function (err, stat) {
+ fs.stat(file, (err, stat) => {
assert.ifError(err)
if (os.platform().indexOf('win') === 0) {
@@ -45,8 +45,8 @@ describe('mkdirp / perm', function () {
})
})
- it('async root perm', function (done) {
- fse.mkdirp(path.join(os.tmpdir(), 'tmp'), o755, function (err) {
+ it('async root perm', done => {
+ fse.mkdirp(path.join(os.tmpdir(), 'tmp'), o755, err => {
assert.ifError(err)
done()
})
diff --git a/lib/mkdirs/__tests__/perm_sync.test.js b/lib/mkdirs/__tests__/perm_sync.test.js
index f2fa7ce..cfd53be 100644
--- a/lib/mkdirs/__tests__/perm_sync.test.js
+++ b/lib/mkdirs/__tests__/perm_sync.test.js
@@ -1,34 +1,34 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-var o755 = parseInt('755', 8)
-var o777 = parseInt('777', 8)
-var o666 = parseInt('666', 8)
+const o755 = parseInt('755', 8)
+const o777 = parseInt('777', 8)
+const o666 = parseInt('666', 8)
-describe('mkdirp / perm_sync', function () {
- var TEST_DIR
+describe('mkdirp / perm_sync', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-perm-sync')
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- it('sync perm', function (done) {
- var file = path.join(TEST_DIR, (Math.random() * (1 << 30)).toString(16) + '.json')
+ it('sync perm', done => {
+ const file = path.join(TEST_DIR, (Math.random() * (1 << 30)).toString(16) + '.json')
fse.mkdirpSync(file, o755)
- fs.exists(file, function (ex) {
+ fs.exists(file, ex => {
assert.ok(ex, 'file created')
- fs.stat(file, function (err, stat) {
+ fs.stat(file, (err, stat) => {
assert.ifError(err)
if (os.platform().indexOf('win') === 0) {
@@ -43,12 +43,12 @@ describe('mkdirp / perm_sync', function () {
})
})
- it('sync root perm', function (done) {
- var file = TEST_DIR
+ it('sync root perm', done => {
+ const file = TEST_DIR
fse.mkdirpSync(file, o755)
- fs.exists(file, function (ex) {
+ fs.exists(file, ex => {
assert.ok(ex, 'file created')
- fs.stat(file, function (err, stat) {
+ fs.stat(file, (err, stat) => {
assert.ifError(err)
assert.ok(stat.isDirectory(), 'target not a directory')
done()
diff --git a/lib/mkdirs/__tests__/race.test.js b/lib/mkdirs/__tests__/race.test.js
index 9022df6..982b713 100644
--- a/lib/mkdirs/__tests__/race.test.js
+++ b/lib/mkdirs/__tests__/race.test.js
@@ -1,27 +1,30 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-var o755 = parseInt('755', 8)
-var o777 = parseInt('777', 8)
-var o666 = parseInt('666', 8)
+const o755 = parseInt('755', 8)
+const o777 = parseInt('777', 8)
+const o666 = parseInt('666', 8)
-describe('mkdirp / race', function () {
- var TEST_DIR, file
+describe('mkdirp / race', () => {
+ let TEST_DIR
+ let file
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-race')
- fse.emptyDir(TEST_DIR, function (err) {
+ fse.emptyDir(TEST_DIR, err => {
assert.ifError(err)
- var ps = [TEST_DIR]
+ const ps = [ TEST_DIR ]
- for (var i = 0; i < 15; i++) {
- var dir = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ for (let i = 0; i < 15; i++) {
+ const dir = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
ps.push(dir)
}
@@ -30,26 +33,20 @@ describe('mkdirp / race', function () {
})
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- it('race', function (done) {
- var res = 2
- mk(file, function () {
- if (--res === 0) done()
- })
+ it('race', done => {
+ let res = 2
- mk(file, function () {
- if (--res === 0) done()
- })
+ mk(file, () => --res === 0 ? done() : undefined)
+ mk(file, () => --res === 0 ? done() : undefined)
function mk (file, callback) {
- fse.mkdirp(file, o755, function (err) {
+ fse.mkdirp(file, o755, err => {
assert.ifError(err)
- fs.exists(file, function (ex) {
+ fs.exists(file, ex => {
assert.ok(ex, 'file created')
- fs.stat(file, function (err, stat) {
+ fs.stat(file, (err, stat) => {
assert.ifError(err)
if (os.platform().indexOf('win') === 0) {
diff --git a/lib/mkdirs/__tests__/rel.test.js b/lib/mkdirs/__tests__/rel.test.js
index 815d9d6..4971d0e 100644
--- a/lib/mkdirs/__tests__/rel.test.js
+++ b/lib/mkdirs/__tests__/rel.test.js
@@ -1,26 +1,31 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+'use strict'
+
+const CWD = process.cwd()
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(CWD)
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-var o755 = parseInt('755', 8)
-var o777 = parseInt('777', 8)
-var o666 = parseInt('666', 8)
+const o755 = parseInt('755', 8)
+const o777 = parseInt('777', 8)
+const o666 = parseInt('666', 8)
-describe('mkdirp / relative', function () {
- var TEST_DIR, file
+describe('mkdirp / relative', () => {
+ let TEST_DIR
+ let file
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-relative')
- fse.emptyDir(TEST_DIR, function (err) {
+ fse.emptyDir(TEST_DIR, err => {
assert.ifError(err)
- var x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
- var y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
- var z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ const x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ const y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ const z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
// relative path
file = path.join(x, y, z)
@@ -29,22 +34,19 @@ describe('mkdirp / relative', function () {
})
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- it('should make the directory with relative path', function (done) {
- var cwd = process.cwd()
+ it('should make the directory with relative path', done => {
process.chdir(TEST_DIR)
- fse.mkdirp(file, o755, function (err) {
+ fse.mkdirp(file, o755, err => {
assert.ifError(err)
- fs.exists(file, function (ex) {
+ fs.exists(file, ex => {
assert.ok(ex, 'file created')
- fs.stat(file, function (err, stat) {
+ fs.stat(file, (err, stat) => {
assert.ifError(err)
// restore
- process.chdir(cwd)
+ process.chdir(CWD)
if (os.platform().indexOf('win') === 0) {
assert.equal(stat.mode & o777, o666)
diff --git a/lib/mkdirs/__tests__/return.test.js b/lib/mkdirs/__tests__/return.test.js
index fecea89..ae32ad0 100644
--- a/lib/mkdirs/__tests__/return.test.js
+++ b/lib/mkdirs/__tests__/return.test.js
@@ -1,37 +1,37 @@
-var assert = require('assert')
-var os = require('os')
-var path = require('path')
-var fse = require('../../')
+'use strict'
+
+const os = require('os')
+const fse = require('../../')
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-describe('mkdirp / return value', function () {
- var TEST_DIR
+describe('mkdirp / return value', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-return')
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- it('should', function (done) {
- var x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
- var y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
- var z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ it('should', done => {
+ const x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ const y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ const z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
- var dir = TEST_DIR + path.sep
- var file = dir + [x, y, z].join(path.sep)
+ const dir = TEST_DIR + path.sep
+ const file = dir + [x, y, z].join(path.sep)
// should return the first dir created.
// By this point, it would be profoundly surprising if /tmp didn't
// already exist, since every other test makes things in there.
- fse.mkdirp(file, function (err, made) {
+ fse.mkdirp(file, (err, made) => {
assert.ifError(err)
assert.equal(made, dir + x)
- fse.mkdirp(file, function (err, made) {
+ fse.mkdirp(file, (err, made) => {
assert.ifError(err)
assert.equal(made, null)
done()
diff --git a/lib/mkdirs/__tests__/return_sync.test.js b/lib/mkdirs/__tests__/return_sync.test.js
index d16dd90..b274058 100644
--- a/lib/mkdirs/__tests__/return_sync.test.js
+++ b/lib/mkdirs/__tests__/return_sync.test.js
@@ -1,35 +1,35 @@
-var assert = require('assert')
-var os = require('os')
-var path = require('path')
-var fse = require('../../')
+'use strict'
+
+const os = require('os')
+const fse = require('../../')
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-describe('mkdirp / return value', function () {
- var TEST_DIR
+describe('mkdirp / return value', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-return')
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- it('should', function () {
- var x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
- var y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
- var z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ it('should', () => {
+ const x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ const y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ const z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
- var dir = TEST_DIR + path.sep
- var file = dir + [x, y, z].join(path.sep)
+ const dir = TEST_DIR + path.sep
+ const file = dir + [x, y, z].join(path.sep)
// should return the first dir created.
// By this point, it would be profoundly surprising if /tmp didn't
// already exist, since every other test makes things in there.
// Note that this will throw on failure, which will fail the test.
- var made = fse.mkdirpSync(file)
+ let made = fse.mkdirpSync(file)
assert.equal(made, dir + x)
// making the same file again should have no effect.
diff --git a/lib/mkdirs/__tests__/root.test.js b/lib/mkdirs/__tests__/root.test.js
index 5311103..ee49c39 100644
--- a/lib/mkdirs/__tests__/root.test.js
+++ b/lib/mkdirs/__tests__/root.test.js
@@ -1,23 +1,25 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var fse = require('../../')
+'use strict'
+
+const fs = require('fs')
+const fse = require('../../')
+const path = require('path')
+const assert = require('assert')
/* global describe, it */
-var o755 = parseInt('755', 8)
+const o755 = parseInt('755', 8)
-describe('mkdirp / root', function () {
+describe('mkdirp / root', () => {
// '/' on unix, 'c:/' on windows.
- var dir = path.normalize(path.resolve(path.sep)).toLowerCase()
+ const dir = path.normalize(path.resolve(path.sep)).toLowerCase()
// if not 'c:\\' or 'd:\\', it's probably a network mounted drive, this fails then. TODO: investigate
if (process.platform === 'win32' && (dir.indexOf('c:\\') === -1) && (dir.indexOf('d:\\') === -1)) return
- it('should', function (done) {
- fse.mkdirp(dir, o755, function (err) {
+ it('should', done => {
+ fse.mkdirp(dir, o755, err => {
if (err) throw err
- fs.stat(dir, function (er, stat) {
+ fs.stat(dir, (er, stat) => {
if (er) throw er
assert.ok(stat.isDirectory(), 'target is a directory')
done()
diff --git a/lib/mkdirs/__tests__/sync.test.js b/lib/mkdirs/__tests__/sync.test.js
index 4290be1..a7033fe 100644
--- a/lib/mkdirs/__tests__/sync.test.js
+++ b/lib/mkdirs/__tests__/sync.test.js
@@ -1,26 +1,29 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-var o755 = parseInt('755', 8)
-var o777 = parseInt('777', 8)
-var o666 = parseInt('666', 8)
+const o755 = parseInt('755', 8)
+const o777 = parseInt('777', 8)
+const o666 = parseInt('666', 8)
-describe('mkdirp / sync', function () {
- var TEST_DIR, file
+describe('mkdirp / sync', () => {
+ let TEST_DIR
+ let file
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-sync')
- fse.emptyDir(TEST_DIR, function (err) {
+ fse.emptyDir(TEST_DIR, err => {
assert.ifError(err)
- var x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
- var y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
- var z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ const x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ const y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
+ const z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16)
file = path.join(TEST_DIR, x, y, z)
@@ -28,20 +31,18 @@ describe('mkdirp / sync', function () {
})
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- it('should', function (done) {
+ it('should', done => {
try {
fse.mkdirpSync(file, o755)
} catch (err) {
assert.fail(err)
}
- fs.exists(file, function (ex) {
+ fs.exists(file, ex => {
assert.ok(ex, 'file created')
- fs.stat(file, function (err, stat) {
+ fs.stat(file, (err, stat) => {
assert.ifError(err)
// http://stackoverflow.com/questions/592448/c-how-to-set-file-permissions-cross-platform
if (os.platform().indexOf('win') === 0) {
diff --git a/lib/mkdirs/__tests__/umask.test.js b/lib/mkdirs/__tests__/umask.test.js
index c5bfc31..a3d06e7 100644
--- a/lib/mkdirs/__tests__/umask.test.js
+++ b/lib/mkdirs/__tests__/umask.test.js
@@ -1,27 +1,29 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require('../../')
+'use strict'
+
+const assert = require('assert')
+const fs = require('fs')
+const path = require('path')
+const os = require('os')
+const fse = require('../../')
/* global afterEach, beforeEach, describe, it */
-var o777 = parseInt('777', 8)
+const o777 = parseInt('777', 8)
-describe('mkdirp', function () {
- var TEST_DIR
- var _rndDir
+describe('mkdirp', () => {
+ let TEST_DIR
+ let _rndDir
// should investigate this test and file more
if (os.platform().indexOf('win') === 0) return
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'mkdirp')
- fse.emptyDir(TEST_DIR, function () {
+ fse.emptyDir(TEST_DIR, () => {
// for actual tests
- var x = Math.floor(Math.random() * Math.pow(16, 6)).toString(16)
- var y = Math.floor(Math.random() * Math.pow(16, 6)).toString(16)
- var z = Math.floor(Math.random() * Math.pow(16, 6)).toString(16)
+ const x = Math.floor(Math.random() * Math.pow(16, 6)).toString(16)
+ const y = Math.floor(Math.random() * Math.pow(16, 6)).toString(16)
+ const z = Math.floor(Math.random() * Math.pow(16, 6)).toString(16)
_rndDir = path.join(TEST_DIR, [x, y, z].join(path.sep))
@@ -31,20 +33,18 @@ describe('mkdirp', function () {
})
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- describe('umask', function () {
- describe('async', function () {
- it('should have proper umask', function (done) {
+ describe('umask', () => {
+ describe('async', () => {
+ it('should have proper umask', done => {
process.umask(0)
- fse.mkdirp(_rndDir, function (err) {
+ fse.mkdirp(_rndDir, err => {
assert.ifError(err)
- fs.exists(_rndDir, function (ex) {
+ fs.exists(_rndDir, ex => {
assert.ok(ex, 'file created')
- fs.stat(_rndDir, function (err, stat) {
+ fs.stat(_rndDir, (err, stat) => {
assert.ifError(err)
assert.equal(stat.mode & o777, o777 & (~process.umask()))
assert.ok(stat.isDirectory(), 'target not a directory')
@@ -55,8 +55,8 @@ describe('mkdirp', function () {
})
})
- describe('sync', function () {
- it('should have proper umask', function (done) {
+ describe('sync', () => {
+ it('should have proper umask', done => {
process.umask(0)
try {
@@ -65,9 +65,9 @@ describe('mkdirp', function () {
return done(err)
}
- fs.exists(_rndDir, function (ex) {
+ fs.exists(_rndDir, ex => {
assert.ok(ex, 'file created')
- fs.stat(_rndDir, function (err, stat) {
+ fs.stat(_rndDir, (err, stat) => {
assert.ifError(err)
assert.equal(stat.mode & o777, (o777 & (~process.umask())))
assert.ok(stat.isDirectory(), 'target not a directory')
diff --git a/lib/mkdirs/mkdirs-sync.js b/lib/mkdirs/mkdirs-sync.js
index 3f30680..a3ece40 100644
--- a/lib/mkdirs/mkdirs-sync.js
+++ b/lib/mkdirs/mkdirs-sync.js
@@ -1,19 +1,21 @@
-var fs = require('graceful-fs')
-var path = require('path')
-var invalidWin32Path = require('./win32').invalidWin32Path
+'use strict'
-var o777 = parseInt('0777', 8)
+const fs = require('graceful-fs')
+const path = require('path')
+const invalidWin32Path = require('./win32').invalidWin32Path
+
+const o777 = parseInt('0777', 8)
function mkdirsSync (p, opts, made) {
if (!opts || typeof opts !== 'object') {
opts = { mode: opts }
}
- var mode = opts.mode
- var xfs = opts.fs || fs
+ let mode = opts.mode
+ const xfs = opts.fs || fs
if (process.platform === 'win32' && invalidWin32Path(p)) {
- var errInval = new Error(p + ' contains invalid WIN32 path characters.')
+ const errInval = new Error(p + ' contains invalid WIN32 path characters.')
errInval.code = 'EINVAL'
throw errInval
}
@@ -40,7 +42,7 @@ function mkdirsSync (p, opts, made) {
// there already. If so, then hooray! If not, then something
// is borked.
default:
- var stat
+ let stat
try {
stat = xfs.statSync(p)
} catch (err1) {
diff --git a/lib/mkdirs/mkdirs.js b/lib/mkdirs/mkdirs.js
index 939776c..1897533 100644
--- a/lib/mkdirs/mkdirs.js
+++ b/lib/mkdirs/mkdirs.js
@@ -1,8 +1,10 @@
-var fs = require('graceful-fs')
-var path = require('path')
-var invalidWin32Path = require('./win32').invalidWin32Path
+'use strict'
-var o777 = parseInt('0777', 8)
+const fs = require('graceful-fs')
+const path = require('path')
+const invalidWin32Path = require('./win32').invalidWin32Path
+
+const o777 = parseInt('0777', 8)
function mkdirs (p, opts, callback, made) {
if (typeof opts === 'function') {
@@ -13,13 +15,13 @@ function mkdirs (p, opts, callback, made) {
}
if (process.platform === 'win32' && invalidWin32Path(p)) {
- var errInval = new Error(p + ' contains invalid WIN32 path characters.')
+ const errInval = new Error(p + ' contains invalid WIN32 path characters.')
errInval.code = 'EINVAL'
return callback(errInval)
}
- var mode = opts.mode
- var xfs = opts.fs || fs
+ let mode = opts.mode
+ const xfs = opts.fs || fs
if (mode === undefined) {
mode = o777 & (~process.umask())
@@ -29,7 +31,7 @@ function mkdirs (p, opts, callback, made) {
callback = callback || function () {}
p = path.resolve(p)
- xfs.mkdir(p, mode, function (er) {
+ xfs.mkdir(p, mode, er => {
if (!er) {
made = made || p
return callback(null, made)
@@ -37,7 +39,7 @@ function mkdirs (p, opts, callback, made) {
switch (er.code) {
case 'ENOENT':
if (path.dirname(p) === p) return callback(er)
- mkdirs(path.dirname(p), opts, function (er, made) {
+ mkdirs(path.dirname(p), opts, (er, made) => {
if (er) callback(er, made)
else mkdirs(p, opts, callback, made)
})
@@ -47,7 +49,7 @@ function mkdirs (p, opts, callback, made) {
// there already. If so, then hooray! If not, then something
// is borked.
default:
- xfs.stat(p, function (er2, stat) {
+ xfs.stat(p, (er2, stat) => {
// if the stat fails, then that's super weird.
// let the original error be the failure reason.
if (er2 || !stat.isDirectory()) callback(er, made)
diff --git a/lib/mkdirs/win32.js b/lib/mkdirs/win32.js
index 569ac1a..99b3920 100644
--- a/lib/mkdirs/win32.js
+++ b/lib/mkdirs/win32.js
@@ -1,24 +1,25 @@
'use strict'
-var path = require('path')
+
+const path = require('path')
// get drive on windows
function getRootPath (p) {
p = path.normalize(path.resolve(p)).split(path.sep)
if (p.length > 0) return p[0]
- else return null
+ return null
}
// http://stackoverflow.com/a/62888/10333 contains more accurate
// TODO: expand to include the rest
-var INVALID_PATH_CHARS = /[<>:"|?*]/
+const INVALID_PATH_CHARS = /[<>:"|?*]/
function invalidWin32Path (p) {
- var rp = getRootPath(p)
+ const rp = getRootPath(p)
p = p.replace(rp, '')
return INVALID_PATH_CHARS.test(p)
}
module.exports = {
- getRootPath: getRootPath,
- invalidWin32Path: invalidWin32Path
+ getRootPath,
+ invalidWin32Path
}
diff --git a/lib/move-sync/__tests__/move-sync-prevent-moving-into-itself.test.js b/lib/move-sync/__tests__/move-sync-prevent-moving-into-itself.test.js
new file mode 100644
index 0000000..3685faa
--- /dev/null
+++ b/lib/move-sync/__tests__/move-sync-prevent-moving-into-itself.test.js
@@ -0,0 +1,189 @@
+'use strict'
+
+const assert = require('assert')
+const os = require('os')
+const path = require('path')
+const fs = require(process.cwd())
+const klawSync = require('klaw-sync')
+
+/* global beforeEach, afterEach, describe, it */
+
+const FILES = [
+ 'file0.txt',
+ path.join('dir1', 'file1.txt'),
+ path.join('dir1', 'dir2', 'file2.txt'),
+ path.join('dir1', 'dir2', 'dir3', 'file3.txt')
+]
+
+const dat0 = 'file0'
+const dat1 = 'file1'
+const dat2 = 'file2'
+const dat3 = 'file3'
+
+describe('+ moveSync() - prevent moving into itself', () => {
+ let TEST_DIR, src, dest
+
+ beforeEach(() => {
+ TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'move-sync-prevent-moving-into-itself')
+ src = path.join(TEST_DIR, 'src')
+ fs.mkdirsSync(src)
+
+ fs.outputFileSync(path.join(src, FILES[0]), dat0)
+ fs.outputFileSync(path.join(src, FILES[1]), dat1)
+ fs.outputFileSync(path.join(src, FILES[2]), dat2)
+ fs.outputFileSync(path.join(src, FILES[3]), dat3)
+ })
+
+ afterEach(() => fs.removeSync(TEST_DIR))
+
+ describe('> when source is a file', () => {
+ it(`should move the file successfully even when dest parent is 'src/dest'`, () => {
+ const destFile = path.join(TEST_DIR, 'src', 'dest', 'destfile.txt')
+ return testSuccessFile(src, destFile)
+ })
+
+ it(`should move the file successfully when dest parent is 'src/src_dest'`, () => {
+ const destFile = path.join(TEST_DIR, 'src', 'src_dest', 'destfile.txt')
+ return testSuccessFile(src, destFile)
+ })
+
+ it(`should move the file successfully when dest parent is 'src/dest_src'`, () => {
+ const destFile = path.join(TEST_DIR, 'src', 'dest_src', 'destfile.txt')
+ return testSuccessFile(src, destFile)
+ })
+
+ it(`should move the file successfully when dest parent is 'src/dest/src'`, () => {
+ const destFile = path.join(TEST_DIR, 'src', 'dest', 'src', 'destfile.txt')
+ return testSuccessFile(src, destFile)
+ })
+
+ it(`should move the file successfully when dest parent is 'srcsrc/dest'`, () => {
+ const destFile = path.join(TEST_DIR, 'srcsrc', 'dest', 'destfile.txt')
+ return testSuccessFile(src, destFile)
+ })
+ })
+
+ describe('> when source is a directory', () => {
+ it(`should move the directory successfully when dest is 'src_dest'`, () => {
+ dest = path.join(TEST_DIR, 'src_dest')
+ return testSuccessDir(src, dest)
+ })
+
+ it(`should move the directory successfully when dest is 'src-dest'`, () => {
+ dest = path.join(TEST_DIR, 'src-dest')
+ return testSuccessDir(src, dest)
+ })
+
+ it(`should move the directory successfully when dest is 'dest_src'`, () => {
+ dest = path.join(TEST_DIR, 'dest_src')
+ return testSuccessDir(src, dest)
+ })
+
+ it(`should move the directory successfully when dest is 'src_dest/src'`, () => {
+ dest = path.join(TEST_DIR, 'src_dest', 'src')
+ return testSuccessDir(src, dest)
+ })
+
+ it(`should move the directory successfully when dest is 'src-dest/src'`, () => {
+ dest = path.join(TEST_DIR, 'src-dest', 'src')
+ return testSuccessDir(src, dest)
+ })
+
+ it(`should move the directory successfully when dest is 'dest_src/src'`, () => {
+ dest = path.join(TEST_DIR, 'dest_src', 'src')
+ return testSuccessDir(src, dest)
+ })
+
+ it(`should move the directory successfully when dest is 'src_src/dest'`, () => {
+ dest = path.join(TEST_DIR, 'src_src', 'dest')
+ return testSuccessDir(src, dest)
+ })
+
+ it(`should move the directory successfully when dest is 'src-src/dest'`, () => {
+ dest = path.join(TEST_DIR, 'src-src', 'dest')
+ return testSuccessDir(src, dest)
+ })
+
+ it(`should move the directory successfully when dest is 'srcsrc/dest'`, () => {
+ dest = path.join(TEST_DIR, 'srcsrc', 'dest')
+ return testSuccessDir(src, dest)
+ })
+
+ it(`should move the directory successfully when dest is 'dest/src'`, () => {
+ dest = path.join(TEST_DIR, 'dest', 'src')
+ return testSuccessDir(src, dest)
+ })
+
+ it('should move the directory successfully when dest is very nested that all its parents need to be created', () => {
+ dest = path.join(TEST_DIR, 'dest', 'src', 'foo', 'bar', 'baz', 'qux', 'quux', 'waldo',
+ 'grault', 'garply', 'fred', 'plugh', 'thud', 'some', 'highly', 'deeply',
+ 'badly', 'nasty', 'crazy', 'mad', 'nested', 'dest')
+ assert(!fs.existsSync(dest))
+ return testSuccessDir(src, dest)
+ })
+
+ it(`should throw error when dest is 'src/dest'`, () => {
+ dest = path.join(TEST_DIR, 'src', 'dest')
+ return testError(src, dest)
+ })
+
+ it(`should throw error when dest is 'src/src_dest'`, () => {
+ dest = path.join(TEST_DIR, 'src', 'src_dest')
+ return testError(src, dest)
+ })
+
+ it(`should throw error when dest is 'src/dest_src'`, () => {
+ dest = path.join(TEST_DIR, 'src', 'dest_src')
+ return testError(src, dest)
+ })
+
+ it(`should throw error when dest is 'src/dest/src'`, () => {
+ dest = path.join(TEST_DIR, 'src', 'dest', 'src')
+ return testError(src, dest)
+ })
+ })
+})
+
+function testSuccessFile (src, destFile) {
+ const srcFile = path.join(src, FILES[0])
+
+ fs.moveSync(srcFile, destFile)
+
+ const o0 = fs.readFileSync(destFile, 'utf8')
+ assert.strictEqual(o0, dat0, 'file contents matched')
+ assert(!fs.existsSync(srcFile))
+}
+
+function testSuccessDir (src, dest) {
+ const srclen = klawSync(src).length
+ // assert src has contents
+ assert(srclen > 2)
+
+ fs.moveSync(src, dest)
+
+ const destlen = klawSync(dest).length
+
+ // assert src and dest length are the same
+ assert.strictEqual(destlen, srclen, 'src and dest length should be equal')
+
+ const o0 = fs.readFileSync(path.join(dest, FILES[0]), 'utf8')
+ const o1 = fs.readFileSync(path.join(dest, FILES[1]), 'utf8')
+ const o2 = fs.readFileSync(path.join(dest, FILES[2]), 'utf8')
+ const o3 = fs.readFileSync(path.join(dest, FILES[3]), 'utf8')
+
+ assert.strictEqual(o0, dat0, 'file contents matched')
+ assert.strictEqual(o1, dat1, 'file contents matched')
+ assert.strictEqual(o2, dat2, 'file contents matched')
+ assert.strictEqual(o3, dat3, 'file contents matched')
+ assert(!fs.existsSync(src))
+}
+
+function testError (src, dest) {
+ try {
+ fs.moveSync(src, dest)
+ } catch (err) {
+ assert.strictEqual(err.message, `Cannot move '${src}' into itself '${dest}'.`)
+ assert(fs.existsSync(src))
+ assert(!fs.existsSync(dest))
+ }
+}
diff --git a/lib/move-sync/__tests__/move-sync.test.js b/lib/move-sync/__tests__/move-sync.test.js
new file mode 100644
index 0000000..58361a9
--- /dev/null
+++ b/lib/move-sync/__tests__/move-sync.test.js
@@ -0,0 +1,352 @@
+'use strict'
+
+const fs = require('graceful-fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
+const rimraf = require('rimraf')
+
+/* global afterEach, beforeEach, describe, it */
+
+function createSyncErrFn (errCode) {
+ const fn = function () {
+ const err = new Error()
+ err.code = errCode
+ throw err
+ }
+ return fn
+}
+
+const originalRenameSync = fs.renameSync
+const originalLinkSync = fs.linkSync
+
+function setUpMockFs (errCode) {
+ fs.renameSync = createSyncErrFn(errCode)
+ fs.linkSync = createSyncErrFn(errCode)
+}
+
+function tearDownMockFs () {
+ fs.renameSync = originalRenameSync
+ fs.linkSync = originalLinkSync
+}
+
+describe('moveSync()', () => {
+ let TEST_DIR
+
+ beforeEach(() => {
+ TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'move-sync')
+
+ fse.emptyDirSync(TEST_DIR)
+
+ // Create fixtures:
+ fs.writeFileSync(path.join(TEST_DIR, 'a-file'), 'sonic the hedgehog\n')
+ fs.mkdirSync(path.join(TEST_DIR, 'a-folder'))
+ fs.writeFileSync(path.join(TEST_DIR, 'a-folder/another-file'), 'tails\n')
+ fs.mkdirSync(path.join(TEST_DIR, 'a-folder/another-folder'))
+ fs.writeFileSync(path.join(TEST_DIR, 'a-folder/another-folder/file3'), 'knuckles\n')
+ })
+
+ afterEach(done => rimraf(TEST_DIR, done))
+
+ it('should not move if src and dest are the same', () => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/a-file`
+
+ fse.moveSync(src, dest)
+
+ // assert src not affected
+ const contents = fs.readFileSync(src, 'utf8')
+ const expected = /^sonic the hedgehog\r?\n$/
+ assert.ok(contents.match(expected), `${contents} match ${expected}`)
+ })
+
+ it('should rename a file on the same device', () => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/a-file-dest`
+
+ fse.moveSync(src, dest)
+
+ const contents = fs.readFileSync(dest, 'utf8')
+ const expected = /^sonic the hedgehog\r?\n$/
+ assert.ok(contents.match(expected), `${contents} match ${expected}`)
+ })
+
+ it('should not overwrite the destination by default', () => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/a-folder/another-file`
+
+ // verify file exists already
+ assert(fs.existsSync(dest))
+
+ try {
+ fse.moveSync(src, dest)
+ } catch (err) {
+ assert.ok(err && err.code === 'EEXIST', 'throw EEXIST')
+ }
+ })
+
+ it('should not overwrite if overwrite = false', () => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/a-folder/another-file`
+
+ // verify file exists already
+ assert(fs.existsSync(dest))
+
+ try {
+ fse.moveSync(src, dest, {overwrite: false})
+ } catch (err) {
+ assert.ok(err && err.code === 'EEXIST', 'throw EEXIST')
+ }
+ })
+
+ it('should overwrite file if overwrite = true', () => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/a-folder/another-file`
+
+ // verify file exists already
+ assert(fs.existsSync(dest))
+
+ fse.moveSync(src, dest, {overwrite: true})
+
+ const contents = fs.readFileSync(dest, 'utf8')
+ const expected = /^sonic the hedgehog\r?\n$/
+ assert.ok(contents.match(expected), `${contents} match ${expected}`)
+ })
+
+ it('should overwrite the destination directory if overwrite = true', function (done) {
+ // Tests fail on appveyor/Windows due to
+ // https://github.com/isaacs/node-graceful-fs/issues/98.
+ // Workaround by increasing the timeout by a minute (because
+ // graceful times out after a minute).
+ this.timeout(90000)
+
+ // Create src
+ const src = path.join(TEST_DIR, 'src')
+ fse.ensureDirSync(src)
+ fse.mkdirsSync(path.join(src, 'some-folder'))
+ fs.writeFileSync(path.join(src, 'some-file'), 'hi')
+
+ const dest = path.join(TEST_DIR, 'a-folder')
+
+ // verify dest has stuff in it
+ const pathsBefore = fs.readdirSync(dest)
+ assert(pathsBefore.indexOf('another-file') >= 0)
+ assert(pathsBefore.indexOf('another-folder') >= 0)
+
+ fse.moveSync(src, dest, {overwrite: true})
+
+ // verify dest does not have old stuff
+ const pathsAfter = fs.readdirSync(dest)
+ assert.strictEqual(pathsAfter.indexOf('another-file'), -1)
+ assert.strictEqual(pathsAfter.indexOf('another-folder'), -1)
+
+ // verify dest has new stuff
+ assert(pathsAfter.indexOf('some-file') >= 0)
+ assert(pathsAfter.indexOf('some-folder') >= 0)
+ done()
+ })
+
+ it('should create directory structure by default', () => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/does/not/exist/a-file-dest`
+
+ // verify dest directory does not exist
+ assert(!fs.existsSync(path.dirname(dest)))
+
+ fse.moveSync(src, dest)
+
+ const contents = fs.readFileSync(dest, 'utf8')
+ const expected = /^sonic the hedgehog\r?\n$/
+ assert.ok(contents.match(expected), `${contents} match ${expected}`)
+ })
+
+ it('should work across devices', () => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/a-file-dest`
+
+ setUpMockFs('EXDEV')
+
+ fse.moveSync(src, dest)
+
+ const contents = fs.readFileSync(dest, 'utf8')
+ const expected = /^sonic the hedgehog\r?\n$/
+ assert.ok(contents.match(expected), `${contents} match ${expected}`)
+ tearDownMockFs()
+ })
+
+ it('should move folders', () => {
+ const src = `${TEST_DIR}/a-folder`
+ const dest = `${TEST_DIR}/a-folder-dest`
+
+ // verify it doesn't exist
+ assert(!fs.existsSync(dest))
+
+ fse.moveSync(src, dest)
+
+ const contents = fs.readFileSync(dest + '/another-file', 'utf8')
+ const expected = /^tails\r?\n$/
+ assert.ok(contents.match(expected), `${contents} match ${expected}`)
+ })
+
+ it('should move folders across devices with EISDIR error', () => {
+ const src = `${TEST_DIR}/a-folder`
+ const dest = `${TEST_DIR}/a-folder-dest`
+
+ setUpMockFs('EISDIR')
+
+ fse.moveSync(src, dest)
+
+ const contents = fs.readFileSync(dest + '/another-folder/file3', 'utf8')
+ const expected = /^knuckles\r?\n$/
+ assert.ok(contents.match(expected), `${contents} match ${expected}`)
+ tearDownMockFs()
+ })
+
+ it('should overwrite folders across devices', () => {
+ const src = `${TEST_DIR}/a-folder`
+ const dest = `${TEST_DIR}/a-folder-dest`
+
+ fs.mkdirSync(dest)
+
+ setUpMockFs('EXDEV')
+
+ fse.moveSync(src, dest, {overwrite: true})
+
+ const contents = fs.readFileSync(dest + '/another-folder/file3', 'utf8')
+ const expected = /^knuckles\r?\n$/
+ assert.ok(contents.match(expected), `${contents} match ${expected}`)
+ tearDownMockFs()
+ })
+
+ it('should move folders across devices with EXDEV error', () => {
+ const src = `${TEST_DIR}/a-folder`
+ const dest = `${TEST_DIR}/a-folder-dest`
+
+ setUpMockFs('EXDEV')
+
+ fse.moveSync(src, dest)
+
+ const contents = fs.readFileSync(dest + '/another-folder/file3', 'utf8')
+ const expected = /^knuckles\r?\n$/
+ assert.ok(contents.match(expected), `${contents} match ${expected}`)
+ tearDownMockFs()
+ })
+
+ it('should move folders across devices with EPERM error', () => {
+ const src = `${TEST_DIR}/a-folder`
+ const dest = `${TEST_DIR}/a-folder-dest`
+
+ setUpMockFs('EPERM')
+
+ fse.moveSync(src, dest)
+
+ const contents = fs.readFileSync(dest + '/another-folder/file3', 'utf8')
+ const expected = /^knuckles\r?\n$/
+ assert.ok(contents.match(expected), `${contents} match ${expected}`)
+ tearDownMockFs()
+ })
+
+ it('should move folders across devices with ENOTSUP error', () => {
+ const src = `${TEST_DIR}/a-folder`
+ const dest = `${TEST_DIR}/a-folder-dest`
+
+ setUpMockFs('ENOTSUP')
+
+ fse.moveSync(src, dest)
+
+ const contents = fs.readFileSync(dest + '/another-folder/file3', 'utf8')
+ const expected = /^knuckles\r?\n$/
+ assert.ok(contents.match(expected), `${contents} match ${expected}`)
+ tearDownMockFs()
+ })
+
+ describe('clobber', () => {
+ it('should be an alias for overwrite', () => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/a-folder/another-file`
+
+ // verify file exists already
+ assert(fs.existsSync(dest))
+
+ fse.moveSync(src, dest, {clobber: true})
+
+ const contents = fs.readFileSync(dest, 'utf8')
+ const expected = /^sonic the hedgehog\r?\n$/
+ assert.ok(contents.match(expected), `${contents} match ${expected}`)
+ })
+ })
+
+ describe('> when trying to move a folder into itself', () => {
+ it('should produce an error', () => {
+ const SRC_DIR = path.join(TEST_DIR, 'src')
+ const DEST_DIR = path.join(TEST_DIR, 'src', 'dest')
+
+ assert(!fs.existsSync(SRC_DIR))
+ fs.mkdirSync(SRC_DIR)
+ assert(fs.existsSync(SRC_DIR))
+
+ try {
+ fse.moveSync(SRC_DIR, DEST_DIR)
+ } catch (err) {
+ assert(err.message, `Cannot move ${SRC_DIR} into itself ${DEST_DIR}.`)
+ assert(fs.existsSync(SRC_DIR))
+ assert(!fs.existsSync(DEST_DIR))
+ }
+ })
+ })
+
+ describe('> when trying to move a file into its parent subdirectory', () => {
+ it('should move successfully', () => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/dest/a-file-dest`
+
+ fse.moveSync(src, dest)
+
+ const contents = fs.readFileSync(dest, 'utf8')
+ const expected = /^sonic the hedgehog\r?\n$/
+ assert.ok(contents.match(expected), `${contents} match ${expected}`)
+ })
+ })
+
+ describe('> when actually trying to move a folder across devices', () => {
+ const differentDevice = '/mnt'
+ let __skipTests = false
+
+ // must set this up, if not, exit silently
+ if (!fs.existsSync(differentDevice)) {
+ console.log('Skipping cross-device moveSync test')
+ __skipTests = true
+ }
+
+ // make sure we have permission on device
+ try {
+ fs.writeFileSync(path.join(differentDevice, 'file'), 'hi')
+ } catch (err) {
+ console.log("Can't write to device. Skipping moveSync test.")
+ __skipTests = true
+ }
+
+ const _it = __skipTests ? it.skip : it
+
+ describe('> just the folder', () => {
+ _it('should move the folder', () => {
+ const src = '/mnt/some/weird/dir-really-weird'
+ const dest = path.join(TEST_DIR, 'device-weird')
+
+ if (!fs.existsSync(src)) {
+ fse.mkdirpSync(src)
+ }
+
+ assert(!fs.existsSync(dest))
+
+ assert(fs.lstatSync(src).isDirectory())
+
+ fse.moveSync(src, dest)
+
+ assert(fs.existsSync(dest))
+ assert(fs.lstatSync(dest).isDirectory())
+ })
+ })
+ })
+})
diff --git a/lib/move-sync/index.js b/lib/move-sync/index.js
new file mode 100644
index 0000000..3c2aa93
--- /dev/null
+++ b/lib/move-sync/index.js
@@ -0,0 +1,117 @@
+'use strict'
+
+const fs = require('graceful-fs')
+const path = require('path')
+const copySync = require('../copy-sync').copySync
+const removeSync = require('../remove').removeSync
+const mkdirpSync = require('../mkdirs').mkdirsSync
+
+function moveSync (src, dest, options) {
+ options = options || {}
+ const overwrite = options.overwrite || options.clobber || false
+
+ src = path.resolve(src)
+ dest = path.resolve(dest)
+
+ if (src === dest) return
+
+ if (isSrcSubdir(src, dest)) throw new Error(`Cannot move '${src}' into itself '${dest}'.`)
+
+ mkdirpSync(path.dirname(dest))
+ tryRenameSync()
+
+ function tryRenameSync () {
+ if (overwrite) {
+ try {
+ return fs.renameSync(src, dest)
+ } catch (err) {
+ if (err.code === 'ENOTEMPTY' || err.code === 'EEXIST' || err.code === 'EPERM') {
+ removeSync(dest)
+ options.overwrite = false // just overwriteed it, no need to do it again
+ return moveSync(src, dest, options)
+ }
+
+ if (err.code !== 'EXDEV') throw err
+ return moveSyncAcrossDevice(src, dest, overwrite)
+ }
+ } else {
+ try {
+ fs.linkSync(src, dest)
+ return fs.unlinkSync(src)
+ } catch (err) {
+ if (err.code === 'EXDEV' || err.code === 'EISDIR' || err.code === 'EPERM' || err.code === 'ENOTSUP') {
+ return moveSyncAcrossDevice(src, dest, overwrite)
+ }
+ throw err
+ }
+ }
+ }
+}
+
+function moveSyncAcrossDevice (src, dest, overwrite) {
+ const stat = fs.statSync(src)
+
+ if (stat.isDirectory()) {
+ return moveDirSyncAcrossDevice(src, dest, overwrite)
+ } else {
+ return moveFileSyncAcrossDevice(src, dest, overwrite)
+ }
+}
+
+function moveFileSyncAcrossDevice (src, dest, overwrite) {
+ const BUF_LENGTH = 64 * 1024
+ const _buff = new Buffer(BUF_LENGTH)
+
+ const flags = overwrite ? 'w' : 'wx'
+
+ const fdr = fs.openSync(src, 'r')
+ const stat = fs.fstatSync(fdr)
+ const fdw = fs.openSync(dest, flags, stat.mode)
+ let bytesRead = 1
+ let pos = 0
+
+ while (bytesRead > 0) {
+ bytesRead = fs.readSync(fdr, _buff, 0, BUF_LENGTH, pos)
+ fs.writeSync(fdw, _buff, 0, bytesRead)
+ pos += bytesRead
+ }
+
+ fs.closeSync(fdr)
+ fs.closeSync(fdw)
+ return fs.unlinkSync(src)
+}
+
+function moveDirSyncAcrossDevice (src, dest, overwrite) {
+ const options = {
+ overwrite: false
+ }
+
+ if (overwrite) {
+ removeSync(dest)
+ tryCopySync()
+ } else {
+ tryCopySync()
+ }
+
+ function tryCopySync () {
+ copySync(src, dest, options)
+ return removeSync(src)
+ }
+}
+
+// return true if dest is a subdir of src, otherwise false.
+// extract dest base dir and check if that is the same as src basename
+function isSrcSubdir (src, dest) {
+ try {
+ return fs.statSync(src).isDirectory() &&
+ src !== dest &&
+ dest.indexOf(src) > -1 &&
+ dest.split(path.dirname(src) + path.sep)[1].split(path.sep)[0] === path.basename(src)
+ } catch (e) {
+ return false
+ }
+}
+
+module.exports = {
+ moveSync
+}
diff --git a/lib/move/__tests__/move.test.js b/lib/move/__tests__/move.test.js
index bdf199f..31469cd 100644
--- a/lib/move/__tests__/move.test.js
+++ b/lib/move/__tests__/move.test.js
@@ -1,18 +1,20 @@
-var assert = require('assert')
-var os = require('os')
-var path = require('path')
-var rimraf = require('rimraf')
-var fs = require('graceful-fs')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('graceful-fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
+const rimraf = require('rimraf')
/* global afterEach, beforeEach, describe, it */
function createAsyncErrFn (errCode) {
- var fn = function () {
+ const fn = function () {
fn.callCount++
- var callback = arguments[arguments.length - 1]
- setTimeout(function () {
- var err = new Error()
+ const callback = arguments[arguments.length - 1]
+ setTimeout(() => {
+ const err = new Error()
err.code = errCode
callback(err)
}, 10)
@@ -21,8 +23,8 @@ function createAsyncErrFn (errCode) {
return fn
}
-var originalRename = fs.rename
-var originalLink = fs.link
+const originalRename = fs.rename
+const originalLink = fs.link
function setUpMockFs (errCode) {
fs.rename = createAsyncErrFn(errCode)
@@ -34,10 +36,10 @@ function tearDownMockFs () {
fs.link = originalLink
}
-describe('move', function () {
- var TEST_DIR
+describe('move', () => {
+ let TEST_DIR
- beforeEach(function () {
+ beforeEach(() => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'move')
fse.emptyDirSync(TEST_DIR)
@@ -50,18 +52,16 @@ describe('move', function () {
fs.writeFileSync(path.join(TEST_DIR, 'a-folder/another-folder/file3'), 'knuckles\n')
})
- afterEach(function (done) {
- rimraf(TEST_DIR, done)
- })
+ afterEach(done => rimraf(TEST_DIR, done))
- it('should rename a file on the same device', function (done) {
- var src = TEST_DIR + '/a-file'
- var dest = TEST_DIR + '/a-file-dest'
+ it('should rename a file on the same device', done => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/a-file-dest`
- fse.move(src, dest, function (err) {
+ fse.move(src, dest, err => {
assert.ifError(err)
- fs.readFile(dest, 'utf8', function (err, contents) {
- var expected = /^sonic the hedgehog\r?\n$/
+ fs.readFile(dest, 'utf8', (err, contents) => {
+ const expected = /^sonic the hedgehog\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
done()
@@ -69,43 +69,63 @@ describe('move', function () {
})
})
- it('should not overwrite the destination by default', function (done) {
- var src = TEST_DIR + '/a-file'
- var dest = TEST_DIR + '/a-folder/another-file'
+ it('should not move a file if source and destination are the same', done => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = src
+
+ fse.move(src, dest, err => {
+ assert.ifError(err)
+ done()
+ })
+ })
+
+ it('should not move a directory if source and destination are the same', done => {
+ const src = `${TEST_DIR}/a-folder`
+ const dest = src
+
+ fse.move(src, dest, err => {
+ assert.ifError(err)
+ done()
+ })
+ })
+
+ it('should not overwrite the destination by default', done => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/a-folder/another-file`
// verify file exists already
assert(fs.existsSync(dest))
- fse.move(src, dest, function (err) {
+ fse.move(src, dest, err => {
assert.ok(err && err.code === 'EEXIST', 'throw EEXIST')
done()
})
})
- it('should not overwrite if overwrite = false', function (done) {
- var src = TEST_DIR + '/a-file'
- var dest = TEST_DIR + '/a-folder/another-file'
+ it('should not overwrite if overwrite = false', done => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/a-folder/another-file`
// verify file exists already
assert(fs.existsSync(dest))
- fse.move(src, dest, {overwrite: false}, function (err) {
+ fse.move(src, dest, {overwrite: false}, err => {
assert.ok(err && err.code === 'EEXIST', 'throw EEXIST')
done()
})
})
- it('should overwrite file if overwrite = true', function (done) {
- var src = TEST_DIR + '/a-file'
- var dest = TEST_DIR + '/a-folder/another-file'
+ it('should overwrite file if overwrite = true', done => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/a-folder/another-file`
// verify file exists already
assert(fs.existsSync(dest))
- fse.move(src, dest, {overwrite: true}, function (err) {
+ fse.move(src, dest, {overwrite: true}, err => {
assert.ifError(err)
- fs.readFile(dest, 'utf8', function (err, contents) {
- var expected = /^sonic the hedgehog\r?\n$/
+ fs.readFile(dest, 'utf8', (err, contents) => {
+ const expected = /^sonic the hedgehog\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
done()
@@ -121,23 +141,23 @@ describe('move', function () {
this.timeout(90000)
// Create src
- var src = path.join(TEST_DIR, 'src')
+ const src = path.join(TEST_DIR, 'src')
fse.ensureDirSync(src)
fse.mkdirsSync(path.join(src, 'some-folder'))
fs.writeFileSync(path.join(src, 'some-file'), 'hi')
- var dest = path.join(TEST_DIR, 'a-folder')
+ const dest = path.join(TEST_DIR, 'a-folder')
// verify dest has stuff in it
- var paths = fs.readdirSync(dest)
+ const paths = fs.readdirSync(dest)
assert(paths.indexOf('another-file') >= 0)
assert(paths.indexOf('another-folder') >= 0)
- fse.move(src, dest, {overwrite: true}, function (err) {
+ fse.move(src, dest, {overwrite: true}, err => {
assert.ifError(err)
// verify dest does not have old stuff
- var paths = fs.readdirSync(dest)
+ const paths = fs.readdirSync(dest)
assert.strictEqual(paths.indexOf('another-file'), -1)
assert.strictEqual(paths.indexOf('another-folder'), -1)
@@ -149,30 +169,30 @@ describe('move', function () {
})
})
- it('should not create directory structure if mkdirp is false', function (done) {
- var src = TEST_DIR + '/a-file'
- var dest = TEST_DIR + '/does/not/exist/a-file-dest'
+ it('should not create directory structure if mkdirp is false', done => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/does/not/exist/a-file-dest`
// verify dest directory does not exist
assert(!fs.existsSync(path.dirname(dest)))
- fse.move(src, dest, {mkdirp: false}, function (err) {
+ fse.move(src, dest, {mkdirp: false}, err => {
assert.strictEqual(err.code, 'ENOENT')
done()
})
})
- it('should create directory structure by default', function (done) {
- var src = TEST_DIR + '/a-file'
- var dest = TEST_DIR + '/does/not/exist/a-file-dest'
+ it('should create directory structure by default', done => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/does/not/exist/a-file-dest`
// verify dest directory does not exist
assert(!fs.existsSync(path.dirname(dest)))
- fse.move(src, dest, function (err) {
+ fse.move(src, dest, err => {
assert.ifError(err)
- fs.readFile(dest, 'utf8', function (err, contents) {
- var expected = /^sonic the hedgehog\r?\n$/
+ fs.readFile(dest, 'utf8', (err, contents) => {
+ const expected = /^sonic the hedgehog\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
done()
@@ -180,18 +200,18 @@ describe('move', function () {
})
})
- it('should work across devices', function (done) {
- var src = TEST_DIR + '/a-file'
- var dest = TEST_DIR + '/a-file-dest'
+ it('should work across devices', done => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/a-file-dest`
setUpMockFs('EXDEV')
- fse.move(src, dest, function (err) {
+ fse.move(src, dest, err => {
assert.ifError(err)
assert.strictEqual(fs.link.callCount, 1)
- fs.readFile(dest, 'utf8', function (err, contents) {
- var expected = /^sonic the hedgehog\r?\n$/
+ fs.readFile(dest, 'utf8', (err, contents) => {
+ const expected = /^sonic the hedgehog\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
@@ -201,17 +221,17 @@ describe('move', function () {
})
})
- it('should move folders', function (done) {
- var src = TEST_DIR + '/a-folder'
- var dest = TEST_DIR + '/a-folder-dest'
+ it('should move folders', done => {
+ const src = `${TEST_DIR}/a-folder`
+ const dest = `${TEST_DIR}/a-folder-dest`
// verify it doesn't exist
assert(!fs.existsSync(dest))
- fse.move(src, dest, function (err) {
+ fse.move(src, dest, err => {
assert.ifError(err)
- fs.readFile(dest + '/another-file', 'utf8', function (err, contents) {
- var expected = /^tails\r?\n$/
+ fs.readFile(dest + '/another-file', 'utf8', (err, contents) => {
+ const expected = /^tails\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
done()
@@ -219,18 +239,18 @@ describe('move', function () {
})
})
- it('should move folders across devices with EISDIR error', function (done) {
- var src = TEST_DIR + '/a-folder'
- var dest = TEST_DIR + '/a-folder-dest'
+ it('should move folders across devices with EISDIR error', done => {
+ const src = `${TEST_DIR}/a-folder`
+ const dest = `${TEST_DIR}/a-folder-dest`
setUpMockFs('EISDIR')
- fse.move(src, dest, function (err) {
+ fse.move(src, dest, err => {
assert.ifError(err)
assert.strictEqual(fs.link.callCount, 1)
- fs.readFile(dest + '/another-folder/file3', 'utf8', function (err, contents) {
- var expected = /^knuckles\r?\n$/
+ fs.readFile(dest + '/another-folder/file3', 'utf8', (err, contents) => {
+ const expected = /^knuckles\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
@@ -241,20 +261,20 @@ describe('move', function () {
})
})
- it('should overwrite folders across devices', function (done) {
- var src = TEST_DIR + '/a-folder'
- var dest = TEST_DIR + '/a-folder-dest'
+ it('should overwrite folders across devices', done => {
+ const src = `${TEST_DIR}/a-folder`
+ const dest = `${TEST_DIR}/a-folder-dest`
fs.mkdirSync(dest)
setUpMockFs('EXDEV')
- fse.move(src, dest, {overwrite: true}, function (err) {
+ fse.move(src, dest, {overwrite: true}, err => {
assert.ifError(err)
assert.strictEqual(fs.rename.callCount, 1)
- fs.readFile(dest + '/another-folder/file3', 'utf8', function (err, contents) {
- var expected = /^knuckles\r?\n$/
+ fs.readFile(dest + '/another-folder/file3', 'utf8', (err, contents) => {
+ const expected = /^knuckles\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
@@ -265,18 +285,18 @@ describe('move', function () {
})
})
- it('should move folders across devices with EXDEV error', function (done) {
- var src = TEST_DIR + '/a-folder'
- var dest = TEST_DIR + '/a-folder-dest'
+ it('should move folders across devices with EXDEV error', done => {
+ const src = `${TEST_DIR}/a-folder`
+ const dest = `${TEST_DIR}/a-folder-dest`
setUpMockFs('EXDEV')
- fse.move(src, dest, function (err) {
+ fse.move(src, dest, err => {
assert.ifError(err)
assert.strictEqual(fs.link.callCount, 1)
- fs.readFile(dest + '/another-folder/file3', 'utf8', function (err, contents) {
- var expected = /^knuckles\r?\n$/
+ fs.readFile(dest + '/another-folder/file3', 'utf8', (err, contents) => {
+ const expected = /^knuckles\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
@@ -287,18 +307,18 @@ describe('move', function () {
})
})
- describe('clobber', function () {
- it('should be an alias for overwrite', function (done) {
- var src = TEST_DIR + '/a-file'
- var dest = TEST_DIR + '/a-folder/another-file'
+ describe('clobber', () => {
+ it('should be an alias for overwrite', done => {
+ const src = `${TEST_DIR}/a-file`
+ const dest = `${TEST_DIR}/a-folder/another-file`
// verify file exists already
assert(fs.existsSync(dest))
- fse.move(src, dest, {overwrite: true}, function (err) {
+ fse.move(src, dest, {clobber: true}, err => {
assert.ifError(err)
- fs.readFile(dest, 'utf8', function (err, contents) {
- var expected = /^sonic the hedgehog\r?\n$/
+ fs.readFile(dest, 'utf8', (err, contents) => {
+ const expected = /^sonic the hedgehog\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
done()
@@ -307,16 +327,16 @@ describe('move', function () {
})
})
- describe.skip('> when trying to a move a folder into itself', function () {
- it('should produce an error', function (done) {
- var SRC_DIR = path.join(TEST_DIR, 'test')
- var DEST_DIR = path.join(TEST_DIR, 'test', 'test')
+ describe.skip('> when trying to move a folder into itself', () => {
+ it('should produce an error', done => {
+ const SRC_DIR = path.join(TEST_DIR, 'test')
+ const DEST_DIR = path.join(TEST_DIR, 'test', 'test')
assert(!fs.existsSync(SRC_DIR))
fs.mkdirSync(SRC_DIR)
assert(fs.existsSync(SRC_DIR))
- fse.move(SRC_DIR, DEST_DIR, function (err) {
+ fse.move(SRC_DIR, DEST_DIR, err => {
assert(fs.existsSync(SRC_DIR))
assert(err)
done()
@@ -327,9 +347,9 @@ describe('move', function () {
// tested on Linux ubuntu 3.13.0-32-generic #57-Ubuntu SMP i686 i686 GNU/Linux
// this won't trigger a bug on Mac OS X Yosimite with a USB drive (/Volumes)
// see issue #108
- describe('> when actually trying to a move a folder across devices', function () {
- var differentDevice = '/mnt'
- var __skipTests = false
+ describe('> when actually trying to move a folder across devices', () => {
+ const differentDevice = '/mnt'
+ let __skipTests = false
// must set this up, if not, exit silently
if (!fs.existsSync(differentDevice)) {
@@ -341,16 +361,16 @@ describe('move', function () {
try {
fs.writeFileSync(path.join(differentDevice, 'file'), 'hi')
} catch (err) {
- console.log("Can't write to device. Skipping test.")
+ console.log("Can't write to device. Skipping move test.")
__skipTests = true
}
- var _it = __skipTests ? it.skip : it
+ const _it = __skipTests ? it.skip : it
- describe('> just the folder', function () {
- _it('should move the folder', function (done) {
- var src = '/mnt/some/weird/dir-really-weird'
- var dest = path.join(TEST_DIR, 'device-weird')
+ describe('> just the folder', () => {
+ _it('should move the folder', done => {
+ const src = '/mnt/some/weird/dir-really-weird'
+ const dest = path.join(TEST_DIR, 'device-weird')
if (!fs.existsSync(src)) {
fse.mkdirpSync(src)
@@ -360,7 +380,7 @@ describe('move', function () {
assert(fs.lstatSync(src).isDirectory())
- fse.move(src, dest, function (err) {
+ fse.move(src, dest, err => {
assert.ifError(err)
assert(fs.existsSync(dest))
// console.log(path.normalize(dest))
diff --git a/lib/move/index.js b/lib/move/index.js
index d5fcd70..6bbdaf4 100644
--- a/lib/move/index.js
+++ b/lib/move/index.js
@@ -1,23 +1,25 @@
+'use strict'
+
// most of this code was written by Andrew Kelley
// licensed under the BSD license: see
// https://github.com/andrewrk/node-mv/blob/master/package.json
// this needs a cleanup
-var fs = require('graceful-fs')
-var ncp = require('../copy/ncp')
-var path = require('path')
-var remove = require('../remove').remove
-var mkdirp = require('../mkdirs').mkdirs
+const fs = require('graceful-fs')
+const ncp = require('../copy/ncp')
+const path = require('path')
+const remove = require('../remove').remove
+const mkdirp = require('../mkdirs').mkdirs
-function mv (source, dest, options, callback) {
+function move (source, dest, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
- var shouldMkdirp = ('mkdirp' in options) ? options.mkdirp : true
- var overwrite = options.overwrite || options.clobber || false
+ const shouldMkdirp = ('mkdirp' in options) ? options.mkdirp : true
+ const overwrite = options.overwrite || options.clobber || false
if (shouldMkdirp) {
mkdirs()
@@ -26,33 +28,35 @@ function mv (source, dest, options, callback) {
}
function mkdirs () {
- mkdirp(path.dirname(dest), function (err) {
+ mkdirp(path.dirname(dest), err => {
if (err) return callback(err)
doRename()
})
}
function doRename () {
- if (overwrite) {
- fs.rename(source, dest, function (err) {
+ if (path.resolve(source) === path.resolve(dest)) {
+ setImmediate(callback)
+ } else if (overwrite) {
+ fs.rename(source, dest, err => {
if (!err) return callback()
if (err.code === 'ENOTEMPTY' || err.code === 'EEXIST') {
- remove(dest, function (err) {
+ remove(dest, err => {
if (err) return callback(err)
options.overwrite = false // just overwriteed it, no need to do it again
- mv(source, dest, options, callback)
+ move(source, dest, options, callback)
})
return
}
// weird Windows shit
if (err.code === 'EPERM') {
- setTimeout(function () {
- remove(dest, function (err) {
+ setTimeout(() => {
+ remove(dest, err => {
if (err) return callback(err)
options.overwrite = false
- mv(source, dest, options, callback)
+ move(source, dest, options, callback)
})
}, 200)
return
@@ -62,9 +66,9 @@ function mv (source, dest, options, callback) {
moveAcrossDevice(source, dest, overwrite, callback)
})
} else {
- fs.link(source, dest, function (err) {
+ fs.link(source, dest, err => {
if (err) {
- if (err.code === 'EXDEV' || err.code === 'EISDIR' || err.code === 'EPERM') {
+ if (err.code === 'EXDEV' || err.code === 'EISDIR' || err.code === 'EPERM' || err.code === 'ENOTSUP') {
moveAcrossDevice(source, dest, overwrite, callback)
return
}
@@ -78,7 +82,7 @@ function mv (source, dest, options, callback) {
}
function moveAcrossDevice (source, dest, overwrite, callback) {
- fs.stat(source, function (err, stat) {
+ fs.stat(source, (err, stat) => {
if (err) {
callback(err)
return
@@ -93,11 +97,11 @@ function moveAcrossDevice (source, dest, overwrite, callback) {
}
function moveFileAcrossDevice (source, dest, overwrite, callback) {
- var outFlags = overwrite ? 'w' : 'wx'
- var ins = fs.createReadStream(source)
- var outs = fs.createWriteStream(dest, {flags: outFlags})
+ const flags = overwrite ? 'w' : 'wx'
+ const ins = fs.createReadStream(source)
+ const outs = fs.createWriteStream(dest, { flags })
- ins.on('error', function (err) {
+ ins.on('error', err => {
ins.destroy()
outs.destroy()
outs.removeListener('close', onClose)
@@ -105,7 +109,7 @@ function moveFileAcrossDevice (source, dest, overwrite, callback) {
// may want to create a directory but `out` line above
// creates an empty file for us: See #108
// don't care about error here
- fs.unlink(dest, function () {
+ fs.unlink(dest, () => {
// note: `err` here is from the input stream errror
if (err.code === 'EISDIR' || err.code === 'EPERM') {
moveDirAcrossDevice(source, dest, overwrite, callback)
@@ -115,7 +119,7 @@ function moveFileAcrossDevice (source, dest, overwrite, callback) {
})
})
- outs.on('error', function (err) {
+ outs.on('error', err => {
ins.destroy()
outs.destroy()
outs.removeListener('close', onClose)
@@ -131,27 +135,27 @@ function moveFileAcrossDevice (source, dest, overwrite, callback) {
}
function moveDirAcrossDevice (source, dest, overwrite, callback) {
- var options = {
+ const options = {
overwrite: false
}
- function startNcp () {
- ncp(source, dest, options, function (err) {
- if (err) return callback(err)
- remove(source, callback)
- })
- }
-
if (overwrite) {
- remove(dest, function (err) {
+ remove(dest, err => {
if (err) return callback(err)
startNcp()
})
} else {
startNcp()
}
+
+ function startNcp () {
+ ncp(source, dest, options, err => {
+ if (err) return callback(err)
+ remove(source, callback)
+ })
+ }
}
module.exports = {
- move: mv
+ move
}
diff --git a/lib/output/__tests__/output.test.js b/lib/output/__tests__/output.test.js
index e1a857b..59ff05f 100644
--- a/lib/output/__tests__/output.test.js
+++ b/lib/output/__tests__/output.test.js
@@ -1,29 +1,29 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global afterEach, beforeEach, describe, it */
-describe('output', function () {
- var TEST_DIR
+describe('output', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'output')
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- describe('+ outputFile', function () {
- describe('> when the file and directory does not exist', function () {
- it('should create the file', function (done) {
- var file = path.join(TEST_DIR, Math.random() + 't-ne', Math.random() + '.txt')
+ describe('+ outputFile', () => {
+ describe('> when the file and directory does not exist', () => {
+ it('should create the file', done => {
+ const file = path.join(TEST_DIR, Math.random() + 't-ne', Math.random() + '.txt')
assert(!fs.existsSync(file))
- fse.outputFile(file, 'hi jp', function (err) {
+ fse.outputFile(file, 'hi jp', err => {
assert.ifError(err)
assert(fs.existsSync(file))
assert.equal(fs.readFileSync(file, 'utf8'), 'hi jp')
@@ -32,12 +32,12 @@ describe('output', function () {
})
})
- describe('> when the file does exist', function () {
- it('should still modify the file', function (done) {
- var file = path.join(TEST_DIR, Math.random() + 't-e', Math.random() + '.txt')
+ describe('> when the file does exist', () => {
+ it('should still modify the file', done => {
+ const file = path.join(TEST_DIR, Math.random() + 't-e', Math.random() + '.txt')
fse.mkdirsSync(path.dirname(file))
fs.writeFileSync(file, 'hello world')
- fse.outputFile(file, 'hello jp', function (err) {
+ fse.outputFile(file, 'hello jp', err => {
if (err) return done(err)
assert.equal(fs.readFileSync(file, 'utf8'), 'hello jp')
done()
@@ -46,10 +46,10 @@ describe('output', function () {
})
})
- describe('+ outputFileSync', function () {
- describe('> when the file and directory does not exist', function () {
- it('should create the file', function () {
- var file = path.join(TEST_DIR, Math.random() + 'ts-ne', Math.random() + '.txt')
+ describe('+ outputFileSync', () => {
+ describe('> when the file and directory does not exist', () => {
+ it('should create the file', () => {
+ const file = path.join(TEST_DIR, Math.random() + 'ts-ne', Math.random() + '.txt')
assert(!fs.existsSync(file))
fse.outputFileSync(file, 'hello man')
assert(fs.existsSync(file))
@@ -57,9 +57,9 @@ describe('output', function () {
})
})
- describe('> when the file does exist', function () {
- it('should still modify the file', function () {
- var file = path.join(TEST_DIR, Math.random() + 'ts-e', Math.random() + '.txt')
+ describe('> when the file does exist', () => {
+ it('should still modify the file', () => {
+ const file = path.join(TEST_DIR, Math.random() + 'ts-e', Math.random() + '.txt')
fse.mkdirsSync(path.dirname(file))
fs.writeFileSync(file, 'hello world')
fse.outputFileSync(file, 'hello man')
diff --git a/lib/output/index.js b/lib/output/index.js
index e8f45f3..f4eb9bb 100644
--- a/lib/output/index.js
+++ b/lib/output/index.js
@@ -1,6 +1,8 @@
-var path = require('path')
-var fs = require('graceful-fs')
-var mkdir = require('../mkdirs')
+'use strict'
+
+const fs = require('graceful-fs')
+const path = require('path')
+const mkdir = require('../mkdirs')
function outputFile (file, data, encoding, callback) {
if (typeof encoding === 'function') {
@@ -8,11 +10,11 @@ function outputFile (file, data, encoding, callback) {
encoding = 'utf8'
}
- var dir = path.dirname(file)
- fs.exists(dir, function (itDoes) {
+ const dir = path.dirname(file)
+ fs.exists(dir, itDoes => {
if (itDoes) return fs.writeFile(file, data, encoding, callback)
- mkdir.mkdirs(dir, function (err) {
+ mkdir.mkdirs(dir, err => {
if (err) return callback(err)
fs.writeFile(file, data, encoding, callback)
@@ -21,7 +23,7 @@ function outputFile (file, data, encoding, callback) {
}
function outputFileSync (file, data, encoding) {
- var dir = path.dirname(file)
+ const dir = path.dirname(file)
if (fs.existsSync(dir)) {
return fs.writeFileSync.apply(fs, arguments)
}
@@ -30,6 +32,6 @@ function outputFileSync (file, data, encoding) {
}
module.exports = {
- outputFile: outputFile,
- outputFileSync: outputFileSync
+ outputFile,
+ outputFileSync
}
diff --git a/lib/remove/__tests__/remove-dir.test.js b/lib/remove/__tests__/remove-dir.test.js
index 0b0f5be..6edec0c 100644
--- a/lib/remove/__tests__/remove-dir.test.js
+++ b/lib/remove/__tests__/remove-dir.test.js
@@ -1,24 +1,26 @@
-var assert = require('assert')
-var fs = require('fs')
-var path = require('path')
-var os = require('os')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global beforeEach, describe, it */
-describe('remove / async / dir', function () {
- var TEST_DIR
+describe('remove / async / dir', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'remove-async-dir')
fse.emptyDir(TEST_DIR, done)
})
- describe('> when dir does not exist', function () {
- it('should not throw an error', function (done) {
- var someDir = path.join(TEST_DIR, 'some-dir/')
+ describe('> when dir does not exist', () => {
+ it('should not throw an error', done => {
+ const someDir = path.join(TEST_DIR, 'some-dir/')
assert.equal(fs.existsSync(someDir), false)
- fse.remove(someDir, function (err) {
+ fse.remove(someDir, err => {
assert.ifError(err)
done()
})
diff --git a/lib/remove/__tests__/remove-sync-dir.test.js b/lib/remove/__tests__/remove-sync-dir.test.js
index 7469039..5372bd6 100644
--- a/lib/remove/__tests__/remove-sync-dir.test.js
+++ b/lib/remove/__tests__/remove-sync-dir.test.js
@@ -1,28 +1,30 @@
-var assert = require('assert')
-var fs = require('fs')
-var os = require('os')
-var path = require('path')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global beforeEach, describe, it */
-describe('remove/sync', function () {
- var TEST_DIR
+describe('remove/sync', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'remove-sync')
fse.emptyDir(TEST_DIR, done)
})
- describe('+ removeSync()', function () {
- it('should delete directories and files synchronously', function () {
+ describe('+ removeSync()', () => {
+ it('should delete directories and files synchronously', () => {
assert(fs.existsSync(TEST_DIR))
fs.writeFileSync(path.join(TEST_DIR, 'somefile'), 'somedata')
fse.removeSync(TEST_DIR)
assert(!fs.existsSync(TEST_DIR))
})
- it('should delete an empty directory synchronously', function () {
+ it('should delete an empty directory synchronously', () => {
assert(fs.existsSync(TEST_DIR))
fse.removeSync(TEST_DIR)
assert(!fs.existsSync(TEST_DIR))
diff --git a/lib/remove/__tests__/remove-sync-file.test.js b/lib/remove/__tests__/remove-sync-file.test.js
index 5a4bed7..68a2296 100644
--- a/lib/remove/__tests__/remove-sync-file.test.js
+++ b/lib/remove/__tests__/remove-sync-file.test.js
@@ -1,22 +1,24 @@
-var assert = require('assert')
-var fs = require('fs')
-var os = require('os')
-var path = require('path')
-var fse = require(process.cwd())
+'use strict'
+
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
/* global beforeEach, describe, it */
-describe('remove/sync', function () {
- var TEST_DIR
+describe('remove/sync', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'remove-sync')
fse.emptyDir(TEST_DIR, done)
})
- describe('+ removeSync()', function () {
- it('should delete a file synchronously', function () {
- var file = path.join(TEST_DIR, 'file')
+ describe('+ removeSync()', () => {
+ it('should delete a file synchronously', () => {
+ const file = path.join(TEST_DIR, 'file')
fs.writeFileSync(file, 'hello')
assert(fs.existsSync(file))
fse.removeSync(file)
diff --git a/lib/remove/__tests__/remove.test.js b/lib/remove/__tests__/remove.test.js
index bf4d5de..d7c2fbd 100644
--- a/lib/remove/__tests__/remove.test.js
+++ b/lib/remove/__tests__/remove.test.js
@@ -1,77 +1,77 @@
-var assert = require('assert')
-var fs = require('fs')
-var os = require('os')
-var path = require('path')
-var sr = require('secure-random')
-var fse = require(process.cwd())
+'use strict'
+
+const assert = require('assert')
+const fs = require('fs')
+const os = require('os')
+const path = require('path')
+const sr = require('secure-random')
+const fse = require(process.cwd())
/* global afterEach, beforeEach, describe, it */
-var TEST_DIR
+let TEST_DIR
function buildFixtureDir () {
- var buf = sr.randomBuffer(5)
- var baseDir = path.join(TEST_DIR, 'TEST_fs-extra_remove-' + Date.now())
+ const buf = sr.randomBuffer(5)
+ const baseDir = path.join(TEST_DIR, `TEST_fs-extra_remove-${Date.now()}`)
fs.mkdirSync(baseDir)
fs.writeFileSync(path.join(baseDir, Math.random() + ''), buf)
fs.writeFileSync(path.join(baseDir, Math.random() + ''), buf)
- var subDir = path.join(TEST_DIR, Math.random() + '')
+ const subDir = path.join(TEST_DIR, Math.random() + '')
fs.mkdirSync(subDir)
fs.writeFileSync(path.join(subDir, Math.random() + ''))
return baseDir
}
-describe('remove', function () {
- beforeEach(function (done) {
+describe('remove', () => {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'remove')
fse.emptyDir(TEST_DIR, done)
})
- afterEach(function (done) {
- fse.remove(TEST_DIR, done)
- })
+ afterEach(done => fse.remove(TEST_DIR, done))
- describe('+ remove()', function () {
- it('should delete an empty directory', function (done) {
+ describe('+ remove()', () => {
+ it('should delete an empty directory', done => {
assert(fs.existsSync(TEST_DIR))
- fse.remove(TEST_DIR, function (err) {
+ fse.remove(TEST_DIR, err => {
assert.ifError(err)
assert(!fs.existsSync(TEST_DIR))
done()
})
})
- it('should delete a directory full of directories and files', function (done) {
+ it('should delete a directory full of directories and files', done => {
buildFixtureDir()
assert(fs.existsSync(TEST_DIR))
- fse.remove(TEST_DIR, function (err) {
+ fse.remove(TEST_DIR, err => {
assert.ifError(err)
assert(!fs.existsSync(TEST_DIR))
done()
})
})
- it('should delete a file', function (done) {
- var file = path.join(TEST_DIR, 'file')
+ it('should delete a file', done => {
+ const file = path.join(TEST_DIR, 'file')
fs.writeFileSync(file, 'hello')
assert(fs.existsSync(file))
- fse.remove(file, function (err) {
+ fse.remove(file, err => {
assert.ifError(err)
assert(!fs.existsSync(file))
done()
})
})
- it('should delete without a callback', function (done) {
- var file = path.join(TEST_DIR, 'file')
+ it('should delete without a callback', done => {
+ const file = path.join(TEST_DIR, 'file')
fs.writeFileSync(file, 'hello')
assert(fs.existsSync(file))
- var existsChecker = setInterval(function () {
- fs.exists(file, function (itDoes) {
+ const existsChecker = setInterval(() => {
+ fs.exists(file, (itDoes) => {
if (!itDoes) {
clearInterval(existsChecker)
done()
@@ -82,22 +82,20 @@ describe('remove', function () {
})
it('shouldn’t delete glob matches', function (done) {
- var file = path.join(TEST_DIR, 'file?')
+ const file = path.join(TEST_DIR, 'file?')
try {
fs.writeFileSync(file, 'hello')
} catch (ex) {
- if (ex.code === 'ENOENT') {
- return this.skip('Windows does not support filenames with ‘?’ or ‘*’ in them.')
- }
+ if (ex.code === 'ENOENT') return this.skip('Windows does not support filenames with ‘?’ or ‘*’ in them.')
throw ex
}
- var wrongFile = path.join(TEST_DIR, 'file1')
+ const wrongFile = path.join(TEST_DIR, 'file1')
fs.writeFileSync(wrongFile, 'yo')
assert(fs.existsSync(file))
assert(fs.existsSync(wrongFile))
- fse.remove(file, function (err) {
+ fse.remove(file, err => {
assert.ifError(err)
assert(!fs.existsSync(file))
assert(fs.existsSync(wrongFile))
@@ -105,15 +103,15 @@ describe('remove', function () {
})
})
- it('shouldn’t delete glob matches when file doesn’t exist', function (done) {
- var nonexistentFile = path.join(TEST_DIR, 'file?')
+ it('shouldn’t delete glob matches when file doesn’t exist', done => {
+ const nonexistentFile = path.join(TEST_DIR, 'file?')
- var wrongFile = path.join(TEST_DIR, 'file1')
+ const wrongFile = path.join(TEST_DIR, 'file1')
fs.writeFileSync(wrongFile, 'yo')
assert(!fs.existsSync(nonexistentFile))
assert(fs.existsSync(wrongFile))
- fse.remove(nonexistentFile, function (err) {
+ fse.remove(nonexistentFile, err => {
assert.ifError(err)
assert(!fs.existsSync(nonexistentFile))
assert(fs.existsSync(wrongFile))
diff --git a/lib/remove/index.js b/lib/remove/index.js
index 566b866..0e3b5dd 100644
--- a/lib/remove/index.js
+++ b/lib/remove/index.js
@@ -1,15 +1,17 @@
-var rimraf = require('./rimraf')
+'use strict'
+
+const rimraf = require('./rimraf')
function removeSync (dir) {
return rimraf.sync(dir, {disableGlob: true})
}
function remove (dir, callback) {
- var options = {disableGlob: true}
+ const options = {disableGlob: true}
return callback ? rimraf(dir, options, callback) : rimraf(dir, options, function () {})
}
module.exports = {
- remove: remove,
- removeSync: removeSync
+ remove,
+ removeSync
}
diff --git a/lib/remove/rimraf.js b/lib/remove/rimraf.js
index da55a62..141a82d 100644
--- a/lib/remove/rimraf.js
+++ b/lib/remove/rimraf.js
@@ -1,14 +1,13 @@
-module.exports = rimraf
-rimraf.sync = rimrafSync
+'use strict'
-var assert = require('assert')
-var path = require('path')
-var fs = require('graceful-fs')
+const fs = require('graceful-fs')
+const path = require('path')
+const assert = require('assert')
-var isWindows = (process.platform === 'win32')
+const isWindows = (process.platform === 'win32')
function defaults (options) {
- var methods = [
+ const methods = [
'unlink',
'chmod',
'stat',
@@ -16,7 +15,7 @@ function defaults (options) {
'rmdir',
'readdir'
]
- methods.forEach(function (m) {
+ methods.forEach(m => {
options[m] = options[m] || fs[m]
m = m + 'Sync'
options[m] = options[m] || fs[m]
@@ -26,6 +25,8 @@ function defaults (options) {
}
function rimraf (p, options, cb) {
+ let busyTries = 0
+
if (typeof options === 'function') {
cb = options
options = {}
@@ -39,18 +40,14 @@ function rimraf (p, options, cb) {
defaults(options)
- var busyTries = 0
-
rimraf_(p, options, function CB (er) {
if (er) {
if (isWindows && (er.code === 'EBUSY' || er.code === 'ENOTEMPTY' || er.code === 'EPERM') &&
busyTries < options.maxBusyTries) {
busyTries++
- var time = busyTries * 100
+ let time = busyTries * 100
// try again, with the same exact callback as this one.
- return setTimeout(function () {
- rimraf_(p, options, CB)
- }, time)
+ return setTimeout(() => rimraf_(p, options, CB), time)
}
// already gone
@@ -79,21 +76,21 @@ function rimraf_ (p, options, cb) {
// sunos lets the root user unlink directories, which is... weird.
// so we have to lstat here and make sure it's not a dir.
- options.lstat(p, function (er, st) {
+ options.lstat(p, (er, st) => {
if (er && er.code === 'ENOENT') {
return cb(null)
}
// Windows can EPERM on stat. Life is suffering.
if (er && er.code === 'EPERM' && isWindows) {
- fixWinEPERM(p, options, er, cb)
+ return fixWinEPERM(p, options, er, cb)
}
if (st && st.isDirectory()) {
return rmdir(p, options, er, cb)
}
- options.unlink(p, function (er) {
+ options.unlink(p, er => {
if (er) {
if (er.code === 'ENOENT') {
return cb(null)
@@ -120,11 +117,11 @@ function fixWinEPERM (p, options, er, cb) {
assert(er instanceof Error)
}
- options.chmod(p, 666, function (er2) {
+ options.chmod(p, 666, er2 => {
if (er2) {
cb(er2.code === 'ENOENT' ? null : er)
} else {
- options.stat(p, function (er3, stats) {
+ options.stat(p, (er3, stats) => {
if (er3) {
cb(er3.code === 'ENOENT' ? null : er)
} else if (stats.isDirectory()) {
@@ -138,6 +135,8 @@ function fixWinEPERM (p, options, er, cb) {
}
function fixWinEPERMSync (p, options, er) {
+ let stats
+
assert(p)
assert(options)
if (er) {
@@ -155,7 +154,7 @@ function fixWinEPERMSync (p, options, er) {
}
try {
- var stats = options.statSync(p)
+ stats = options.statSync(p)
} catch (er3) {
if (er3.code === 'ENOENT') {
return
@@ -182,7 +181,7 @@ function rmdir (p, options, originalEr, cb) {
// try to rmdir first, and only readdir on ENOTEMPTY or EEXIST (SunOS)
// if we guessed wrong, and it's not a directory, then
// raise the original error.
- options.rmdir(p, function (er) {
+ options.rmdir(p, er => {
if (er && (er.code === 'ENOTEMPTY' || er.code === 'EEXIST' || er.code === 'EPERM')) {
rmkids(p, options, cb)
} else if (er && er.code === 'ENOTDIR') {
@@ -198,23 +197,20 @@ function rmkids (p, options, cb) {
assert(options)
assert(typeof cb === 'function')
- options.readdir(p, function (er, files) {
- if (er) {
- return cb(er)
- }
- var n = files.length
- if (n === 0) {
- return options.rmdir(p, cb)
- }
- var errState
- files.forEach(function (f) {
- rimraf(path.join(p, f), options, function (er) {
+ options.readdir(p, (er, files) => {
+ if (er) return cb(er)
+
+ let n = files.length
+ let errState
+
+ if (n === 0) return options.rmdir(p, cb)
+
+ files.forEach(f => {
+ rimraf(path.join(p, f), options, er => {
if (errState) {
return
}
- if (er) {
- return cb(errState = er)
- }
+ if (er) return cb(errState = er)
if (--n === 0) {
options.rmdir(p, cb)
}
@@ -227,6 +223,8 @@ function rmkids (p, options, cb) {
// tie up the JavaScript thread and fail on excessively
// deep directory trees.
function rimrafSync (p, options) {
+ let st
+
options = options || {}
defaults(options)
@@ -236,7 +234,7 @@ function rimrafSync (p, options) {
assert.equal(typeof options, 'object', 'rimraf: options should be object')
try {
- var st = options.lstatSync(p)
+ st = options.lstatSync(p)
} catch (er) {
if (er.code === 'ENOENT') {
return
@@ -258,11 +256,9 @@ function rimrafSync (p, options) {
} catch (er) {
if (er.code === 'ENOENT') {
return
- }
- if (er.code === 'EPERM') {
+ } else if (er.code === 'EPERM') {
return isWindows ? fixWinEPERMSync(p, options, er) : rmdirSync(p, options, er)
- }
- if (er.code !== 'EISDIR') {
+ } else if (er.code !== 'EISDIR') {
throw er
}
rmdirSync(p, options, er)
@@ -281,11 +277,9 @@ function rmdirSync (p, options, originalEr) {
} catch (er) {
if (er.code === 'ENOENT') {
return
- }
- if (er.code === 'ENOTDIR') {
+ } else if (er.code === 'ENOTDIR') {
throw originalEr
- }
- if (er.code === 'ENOTEMPTY' || er.code === 'EEXIST' || er.code === 'EPERM') {
+ } else if (er.code === 'ENOTEMPTY' || er.code === 'EEXIST' || er.code === 'EPERM') {
rmkidsSync(p, options)
}
}
@@ -294,8 +288,9 @@ function rmdirSync (p, options, originalEr) {
function rmkidsSync (p, options) {
assert(p)
assert(options)
- options.readdirSync(p).forEach(function (f) {
- rimrafSync(path.join(p, f), options)
- })
+ options.readdirSync(p).forEach(f => rimrafSync(path.join(p, f), options))
options.rmdirSync(p, options)
}
+
+module.exports = rimraf
+rimraf.sync = rimrafSync
diff --git a/lib/util/__tests__/utimes.test.js b/lib/util/__tests__/utimes.test.js
index 648f424..23349ee 100644
--- a/lib/util/__tests__/utimes.test.js
+++ b/lib/util/__tests__/utimes.test.js
@@ -1,22 +1,20 @@
-var assert = require('assert')
-var path = require('path')
-var os = require('os')
-var fs = require('fs')
-var fse = require(process.cwd())
-var semver = require('semver')
-var proxyquire = require('proxyquire')
-var gracefulFsStub
-var utimes
+'use strict'
-/* global beforeEach, describe, it */
+const fs = require('fs')
+const os = require('os')
+const fse = require(process.cwd())
+const path = require('path')
+const assert = require('assert')
+const proxyquire = require('proxyquire')
+let gracefulFsStub
+let utimes
-describe('utimes', function () {
- var TEST_DIR
+/* global beforeEach, describe, it */
- // ignore Node.js v0.10.x
- if (semver.lt(process.version, '0.11.0')) return
+describe('utimes', () => {
+ let TEST_DIR
- beforeEach(function (done) {
+ beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'utimes')
fse.emptyDir(TEST_DIR, done)
// reset stubs
@@ -24,54 +22,48 @@ describe('utimes', function () {
utimes = proxyquire('../utimes', {'graceful-fs': gracefulFsStub})
})
- describe('hasMillisResSync()', function () {
- it('should return a boolean indicating whether it has support', function () {
- var res = utimes.hasMillisResSync()
+ describe('hasMillisResSync()', () => {
+ it('should return a boolean indicating whether it has support', () => {
+ const res = utimes.hasMillisResSync()
assert.equal(typeof res, 'boolean')
// HFS => false
- if (process.platform === 'darwin') {
- assert.equal(res, false)
- }
+ if (process.platform === 'darwin') assert.equal(res, false)
// does anyone use FAT anymore?
- /* if (process.platform === 'win32') {
- assert.equal(res, true)
- } */
+ // if (process.platform === 'win32') assert.equal(res, true)
// fails on appveyor... could appveyor be using FAT?
// this would fail if ext2/ext3
- if (process.platform === 'linux') {
- assert.equal(res, true)
- }
+ if (process.platform === 'linux') assert.equal(res, true)
})
})
- describe('timeRemoveMills()', function () {
- it('should remove millisecond precision from a timestamp', function () {
- var ts = 1334990868773
- var ets = 1334990868000
+ describe('timeRemoveMills()', () => {
+ it('should remove millisecond precision from a timestamp', () => {
+ const ts = 1334990868773
+ const ets = 1334990868000
assert.strictEqual(utimes.timeRemoveMillis(ts), ets)
assert.strictEqual(utimes.timeRemoveMillis(new Date(ts)).getTime(), ets)
})
})
- describe('utimesMillis()', function () {
+ describe('utimesMillis()', () => {
// see discussion https://github.com/jprichardson/node-fs-extra/pull/141
- it('should set the utimes w/ millisecond precision', function (done) {
- var tmpFile = path.join(TEST_DIR, 'someFile')
+ it('should set the utimes w/ millisecond precision', done => {
+ const tmpFile = path.join(TEST_DIR, 'someFile')
fs.writeFileSync(tmpFile, 'hello')
- var stats = fs.lstatSync(tmpFile)
+ let stats = fs.lstatSync(tmpFile)
// Apr 21st, 2012
- var awhileAgo = new Date(1334990868773)
- var awhileAgoNoMillis = new Date(1334990868000)
+ const awhileAgo = new Date(1334990868773)
+ const awhileAgoNoMillis = new Date(1334990868000)
assert.notDeepEqual(stats.mtime, awhileAgo)
assert.notDeepEqual(stats.atime, awhileAgo)
- utimes.utimesMillis(tmpFile, awhileAgo, awhileAgo, function (err) {
+ utimes.utimesMillis(tmpFile, awhileAgo, awhileAgo, err => {
assert.ifError(err)
stats = fs.statSync(tmpFile)
if (utimes.hasMillisResSync()) {
@@ -85,34 +77,30 @@ describe('utimes', function () {
})
})
- it('should close open file desciptors after encountering an error', function (done) {
- var fakeFd = Math.random()
+ it('should close open file desciptors after encountering an error', done => {
+ const fakeFd = Math.random()
- gracefulFsStub.open = function (pathIgnored, flagsIgnored, modeIgnored, callback) {
- if (typeof modeIgnored === 'function') {
- callback = modeIgnored
- }
- process.nextTick(function () {
- callback(null, fakeFd)
- })
+ gracefulFsStub.open = (pathIgnored, flagsIgnored, modeIgnored, callback) => {
+ if (typeof modeIgnored === 'function') callback = modeIgnored
+ process.nextTick(() => callback(null, fakeFd))
}
- var closeCalled = false
- gracefulFsStub.close = function (fd, callback) {
+ let closeCalled = false
+ gracefulFsStub.close = (fd, callback) => {
assert.equal(fd, fakeFd)
closeCalled = true
if (callback) process.nextTick(callback)
}
- var testError
- gracefulFsStub.futimes = function (fd, atimeIgnored, mtimeIgnored, callback) {
- process.nextTick(function () {
+ let testError
+ gracefulFsStub.futimes = (fd, atimeIgnored, mtimeIgnored, callback) => {
+ process.nextTick(() => {
testError = new Error('A test error')
callback(testError)
})
}
- utimes.utimesMillis('ignored', 'ignored', 'ignored', function (err) {
+ utimes.utimesMillis('ignored', 'ignored', 'ignored', err => {
assert.equal(err, testError)
assert(closeCalled)
done()
diff --git a/lib/util/assign.js b/lib/util/assign.js
index 8e41f9a..317e5ec 100644
--- a/lib/util/assign.js
+++ b/lib/util/assign.js
@@ -1,9 +1,11 @@
+'use strict'
+
// simple mutable assign
function assign () {
- var args = [].slice.call(arguments).filter(function (i) { return i })
- var dest = args.shift()
- args.forEach(function (src) {
- Object.keys(src).forEach(function (key) {
+ const args = [].slice.call(arguments).filter(i => i)
+ const dest = args.shift()
+ args.forEach(src => {
+ Object.keys(src).forEach(key => {
dest[key] = src[key]
})
})
diff --git a/lib/util/utimes.js b/lib/util/utimes.js
index 118f8a7..4c32099 100644
--- a/lib/util/utimes.js
+++ b/lib/util/utimes.js
@@ -1,36 +1,38 @@
-var fs = require('graceful-fs')
-var path = require('path')
-var os = require('os')
+'use strict'
+
+const fs = require('graceful-fs')
+const os = require('os')
+const path = require('path')
// HFS, ext{2,3}, FAT do not, Node.js v0.10 does not
function hasMillisResSync () {
- var tmpfile = path.join('millis-test-sync' + Date.now().toString() + Math.random().toString().slice(2))
+ let tmpfile = path.join('millis-test-sync' + Date.now().toString() + Math.random().toString().slice(2))
tmpfile = path.join(os.tmpdir(), tmpfile)
// 550 millis past UNIX epoch
- var d = new Date(1435410243862)
+ const d = new Date(1435410243862)
fs.writeFileSync(tmpfile, 'https://github.com/jprichardson/node-fs-extra/pull/141')
- var fd = fs.openSync(tmpfile, 'r+')
+ const fd = fs.openSync(tmpfile, 'r+')
fs.futimesSync(fd, d, d)
fs.closeSync(fd)
return fs.statSync(tmpfile).mtime > 1435410243000
}
function hasMillisRes (callback) {
- var tmpfile = path.join('millis-test' + Date.now().toString() + Math.random().toString().slice(2))
+ let tmpfile = path.join('millis-test' + Date.now().toString() + Math.random().toString().slice(2))
tmpfile = path.join(os.tmpdir(), tmpfile)
// 550 millis past UNIX epoch
- var d = new Date(1435410243862)
- fs.writeFile(tmpfile, 'https://github.com/jprichardson/node-fs-extra/pull/141', function (err) {
+ const d = new Date(1435410243862)
+ fs.writeFile(tmpfile, 'https://github.com/jprichardson/node-fs-extra/pull/141', err => {
if (err) return callback(err)
- fs.open(tmpfile, 'r+', function (err, fd) {
+ fs.open(tmpfile, 'r+', (err, fd) => {
if (err) return callback(err)
- fs.futimes(fd, d, d, function (err) {
+ fs.futimes(fd, d, d, err => {
if (err) return callback(err)
- fs.close(fd, function (err) {
+ fs.close(fd, err => {
if (err) return callback(err)
- fs.stat(tmpfile, function (err, stats) {
+ fs.stat(tmpfile, (err, stats) => {
if (err) return callback(err)
callback(null, stats.mtime > 1435410243000)
})
@@ -52,10 +54,10 @@ function timeRemoveMillis (timestamp) {
function utimesMillis (path, atime, mtime, callback) {
// if (!HAS_MILLIS_RES) return fs.utimes(path, atime, mtime, callback)
- fs.open(path, 'r+', function (err, fd) {
+ fs.open(path, 'r+', (err, fd) => {
if (err) return callback(err)
- fs.futimes(fd, atime, mtime, function (futimesErr) {
- fs.close(fd, function (closeErr) {
+ fs.futimes(fd, atime, mtime, futimesErr => {
+ fs.close(fd, closeErr => {
if (callback) callback(futimesErr || closeErr)
})
})
@@ -63,8 +65,8 @@ function utimesMillis (path, atime, mtime, callback) {
}
module.exports = {
- hasMillisRes: hasMillisRes,
- hasMillisResSync: hasMillisResSync,
- timeRemoveMillis: timeRemoveMillis,
- utimesMillis: utimesMillis
+ hasMillisRes,
+ hasMillisResSync,
+ timeRemoveMillis,
+ utimesMillis
}
diff --git a/package.json b/package.json
index 8295992..d489494 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "fs-extra",
- "version": "2.0.0",
+ "version": "2.1.2",
"description": "fs-extra contains methods that aren't included in the vanilla Node.js fs package. Such as mkdir -p, cp -r, and rm -rf.",
"homepage": "https://github.com/jprichardson/node-fs-extra",
"repository": {
@@ -40,13 +40,13 @@
"coveralls": "^2.11.2",
"istanbul": "^0.4.5",
"klaw": "^1.0.0",
+ "klaw-sync": "^1.1.2",
"minimist": "^1.1.1",
"mocha": "^3.1.2",
"proxyquire": "^1.7.10",
"read-dir-files": "^0.1.1",
"rimraf": "^2.2.8",
"secure-random": "^1.1.1",
- "semver": "^5.3.0",
"standard": "^8.5.0"
},
"main": "./lib/index",
diff --git a/test.js b/test.js
index 1877731..659ea68 100644
--- a/test.js
+++ b/test.js
@@ -1,30 +1,31 @@
-var os = require('os')
-var path = require('path')
-var Mocha = require('mocha')
-var assign = require('./lib/util/assign')
-var klaw = require('klaw')
+'use strict'
-var argv = require('minimist')(process.argv.slice(2))
+const os = require('os')
+const path = require('path')
+const klaw = require('klaw')
+const Mocha = require('mocha')
+const assign = require('./lib/util/assign')
-var mochaOpts = assign({
+const argv = require('minimist')(process.argv.slice(2))
+
+const mochaOpts = assign({
ui: 'bdd',
reporter: 'dot',
timeout: 30000
}, argv)
-var mocha = new Mocha(mochaOpts)
+const mocha = new Mocha(mochaOpts)
+const testExt = '.test.js'
klaw('./lib').on('readable', function () {
- var item
+ let item
while ((item = this.read())) {
if (!item.stats.isFile()) return
- if (item.path.lastIndexOf('.test.js') !== (item.path.length - '.test.js'.length)) return
+ if (item.path.lastIndexOf(testExt) !== (item.path.length - testExt.length)) return
mocha.addFile(item.path)
}
-}).on('end', function () {
- mocha.run(function (failures) {
- require('./').remove(path.join(os.tmpdir(), 'fs-extra'), function () {
- process.exit(failures)
- })
+}).on('end', () => {
+ mocha.run(failures => {
+ require('./').remove(path.join(os.tmpdir(), 'fs-extra'), () => process.exit(failures))
})
})
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-fs-extra.git
More information about the Pkg-javascript-commits
mailing list