[Pkg-javascript-commits] [node-fs-extra] 08/10: New upstream version 4.0.0
Julien Puydt
julien.puydt at laposte.net
Mon Jul 24 09:18:39 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 865a6fab423a95fbb164fe89fc6d03f219317d83
Author: Julien Puydt <julien.puydt at laposte.net>
Date: Sun Jul 23 11:45:47 2017 +0200
New upstream version 4.0.0
---
CHANGELOG.md | 15 ++
README.md | 4 +-
docs/fs-read-write.md | 39 +++++
docs/outputJson-sync.md | 4 +-
docs/outputJson.md | 4 +-
docs/writeJson-sync.md | 6 +-
docs/writeJson.md | 6 +-
.../__tests__/copy-sync-preserve-time.test.js | 6 +-
lib/copy/__tests__/copy-preserve-time.test.js | 6 +-
lib/fs/__tests__/multi-param.test.js | 154 +++++++++++++++++
lib/fs/index.js | 44 ++++-
lib/json/index.js | 4 +-
.../move-prevent-moving-into-itself.test.js | 192 +++++++++++++++++++++
lib/move/__tests__/move.test.js | 17 +-
lib/move/index.js | 82 +++++----
lib/remove/rimraf.js | 22 ++-
package.json | 5 +-
17 files changed, 540 insertions(+), 70 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ff2a6dc..70efd66 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,18 @@
+4.0.0 / 2017-07-14
+------------------
+
+### Changed
+
+- **BREAKING:** The promisified versions of `fs.read()` & `fs.write()` now return objects. See [the docs](docs/fs-read-write.md) for details.
+- `fs.move()` now errors out when source and destination are the same.
+- Applied upstream fixes from `rimraf` to `fs.remove()` & `fs.removeSync()`.
+
+### Fixed
+
+- Got `fs.outputJSONSync()` working again; it was broken due to refactoring.
+
+Also clarified the docs in a few places.
+
3.0.1 / 2017-05-04
------------------
diff --git a/README.md b/README.md
index bb9422d..7dbb9f7 100644
--- a/README.md
+++ b/README.md
@@ -128,7 +128,7 @@ Methods
- [writeJsonSync](docs/writeJson-sync.md)
-**NOTE:** You can still use the native Node.js methods. They are promisified and copied over to `fs-extra`.
+**NOTE:** You can still use the native Node.js methods. They are promisified and copied over to `fs-extra`. See [notes on `fs.read()` & `fs.write()`](docs/fs-read-write.md)
### What happened to `walk()` and `walkSync()`?
@@ -141,7 +141,7 @@ Third Party
### TypeScript
-If you like TypeScript, you can use `fs-extra` with it: https://github.com/borisyankov/DefinitelyTyped/tree/master/fs-extra
+If you like TypeScript, you can use `fs-extra` with it: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/fs-extra
### File / Directory Watching
diff --git a/docs/fs-read-write.md b/docs/fs-read-write.md
new file mode 100644
index 0000000..5e5f420
--- /dev/null
+++ b/docs/fs-read-write.md
@@ -0,0 +1,39 @@
+# About `fs.read()` & `fs.write()`
+
+[`fs.read()`](https://nodejs.org/api/fs.html#fs_fs_read_fd_buffer_offset_length_position_callback) & [`fs.write()`](https://nodejs.org/api/fs.html#fs_fs_write_fd_buffer_offset_length_position_callback) are different from other `fs` methods in that their callbacks are called with 3 arguments instead of the usual 2 arguments.
+
+If you're using them with callbacks, they will behave as usual. However, their promise usage is a little different. `fs-extra` promisifies these methods like [`util.promisify()`](https://nodejs.org/api/util.html#util_util_promisify_original) (only availible in Node 8+) does.
+
+Here's the example promise usage:
+
+## `fs.read()`
+
+```js
+// Basic promises
+fs.read(fd, buffer, offset, length, position)
+ .then(results => {
+ console.log(results)
+ // { bytesRead: 20, buffer: <Buffer 0f 34 5d ...> }
+ })
+
+// Async/await usage:
+async function example () {
+ const { bytesRead, buffer } = await fs.read(fd, Buffer.alloc(length), offset, length, position)
+}
+```
+
+## `fs.write()`
+
+```js
+// Basic promises
+fs.write(fd, buffer, offset, length, position)
+ .then(results => {
+ console.log(results)
+ // { bytesWritten: 20, buffer: <Buffer 0f 34 5d ...> }
+ })
+
+// Async/await usage:
+async function example () {
+ const { bytesWritten, buffer } = await fs.write(fd, Buffer.alloc(length), offset, length, position)
+}
+```
diff --git a/docs/outputJson-sync.md b/docs/outputJson-sync.md
index cab6316..13c214d 100644
--- a/docs/outputJson-sync.md
+++ b/docs/outputJson-sync.md
@@ -1,13 +1,15 @@
# 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>`
+ - `spaces` `<Number|String>` Number of spaces to indent; or a string to use for indentation (i.e. pass `'\t'` for tab indentation). See [the docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_space_argument) for more info.
+ - `replacer` [JSON replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter)
+ - Also accepts [`fs.writeFileSync` options](https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options)
## Example:
diff --git a/docs/outputJson.md b/docs/outputJson.md
index fc6f2fe..d3fbe67 100644
--- a/docs/outputJson.md
+++ b/docs/outputJson.md
@@ -1,13 +1,15 @@
# 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()`
- `file` `<String>`
- `object` `<Object>`
- `options` `<Object>`
+ - `spaces` `<Number|String>` Number of spaces to indent; or a string to use for indentation (i.e. pass `'\t'` for tab indentation). See [the docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_space_argument) for more info.
+ - `replacer` [JSON replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter)
+ - Also accepts [`fs.writeFile` options](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback)
- `callback` `<Function>`
## Example:
diff --git a/docs/writeJson-sync.md b/docs/writeJson-sync.md
index 9e649bb..d2a3ef6 100644
--- a/docs/writeJson-sync.md
+++ b/docs/writeJson-sync.md
@@ -1,13 +1,15 @@
# 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).
+Writes an object to a JSON file.
**Alias:** `writeJSONSync()`
- `file` `<String>`
- `object` `<Object>`
- `options` `<Object>`
+ - `spaces` `<Number|String>` Number of spaces to indent; or a string to use for indentation (i.e. pass `'\t'` for tab indentation). See [the docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_space_argument) for more info.
+ - `replacer` [JSON replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter)
+ - Also accepts [`fs.writeFileSync` options](https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options)
## Example:
diff --git a/docs/writeJson.md b/docs/writeJson.md
index 0e9c393..af94ffc 100644
--- a/docs/writeJson.md
+++ b/docs/writeJson.md
@@ -1,13 +1,15 @@
# writeJson(file, object, [options, callback])
-Writes an object to a JSON file. `options` are the same that
-you'd pass to [`jsonFile.writeFile()`](https://github.com/jprichardson/node-jsonfile#writefilefilename-options-callback).
+Writes an object to a JSON file.
**Alias:** `writeJSON()`
- `file` `<String>`
- `object` `<Object>`
- `options` `<Object>`
+ - `spaces` `<Number|String>` Number of spaces to indent; or a string to use for indentation (i.e. pass `'\t'` for tab indentation). See [the docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_space_argument) for more info.
+ - `replacer` [JSON replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter)
+ - Also accepts [`fs.writeFile` options](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback)
- `callback` `<Function>`
## Example:
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 8c4bfa9..6a4ad9f 100644
--- a/lib/copy-sync/__tests__/copy-sync-preserve-time.test.js
+++ b/lib/copy-sync/__tests__/copy-sync-preserve-time.test.js
@@ -9,7 +9,11 @@ const copySync = require('../copy-sync')
/* global beforeEach, describe, it */
-describe('copy', () => {
+if (process.arch === 'ia32') console.warn('32 bit arch; skipping copySync timestamp tests')
+
+const describeIf64 = process.arch === 'ia32' ? describe.skip : describe
+
+describeIf64('copySync', () => {
let TEST_DIR
beforeEach(done => {
diff --git a/lib/copy/__tests__/copy-preserve-time.test.js b/lib/copy/__tests__/copy-preserve-time.test.js
index 1c9fcb0..fe17563 100644
--- a/lib/copy/__tests__/copy-preserve-time.test.js
+++ b/lib/copy/__tests__/copy-preserve-time.test.js
@@ -9,7 +9,11 @@ const assert = require('assert')
/* global beforeEach, describe, it */
-describe('copy', () => {
+if (process.arch === 'ia32') console.warn('32 bit arch; skipping copy timestamp tests')
+
+const describeIf64 = process.arch === 'ia32' ? describe.skip : describe
+
+describeIf64('copy', () => {
let TEST_DIR
beforeEach(done => {
diff --git a/lib/fs/__tests__/multi-param.test.js b/lib/fs/__tests__/multi-param.test.js
new file mode 100644
index 0000000..92c619b
--- /dev/null
+++ b/lib/fs/__tests__/multi-param.test.js
@@ -0,0 +1,154 @@
+'use strict'
+/* eslint-env mocha */
+const assert = require('assert')
+const path = require('path')
+const crypto = require('crypto')
+const os = require('os')
+const semver = require('semver')
+const fs = require('../..')
+
+const SIZE = 1000
+
+// Used for tests on Node 7.2.0+ only
+const onNode7it = semver.gte(process.version, '7.2.0') ? it : it.skip
+
+describe('fs.read()', () => {
+ let TEST_FILE
+ let TEST_DATA
+ let TEST_FD
+
+ beforeEach(() => {
+ TEST_FILE = path.join(os.tmpdir(), 'fs-extra', 'read-test-file')
+ TEST_DATA = crypto.randomBytes(SIZE)
+ fs.writeFileSync(TEST_FILE, TEST_DATA)
+ TEST_FD = fs.openSync(TEST_FILE, 'r')
+ })
+
+ afterEach(() => {
+ return fs.close(TEST_FD)
+ .then(() => fs.remove(TEST_FILE))
+ })
+
+ describe('with promises', () => {
+ it('returns an object', () => {
+ return fs.read(TEST_FD, Buffer.alloc(SIZE), 0, SIZE, 0)
+ .then(results => {
+ const bytesRead = results.bytesRead
+ const buffer = results.buffer
+ assert.equal(bytesRead, SIZE, 'bytesRead is correct')
+ assert(buffer.equals(TEST_DATA), 'data is correct')
+ })
+ })
+
+ it('returns an object when position is not set', () => {
+ return fs.read(TEST_FD, Buffer.alloc(SIZE), 0, SIZE)
+ .then(results => {
+ const bytesRead = results.bytesRead
+ const buffer = results.buffer
+ assert.equal(bytesRead, SIZE, 'bytesRead is correct')
+ assert(buffer.equals(TEST_DATA), 'data is correct')
+ })
+ })
+ })
+
+ describe('with callbacks', () => {
+ it('works', done => {
+ fs.read(TEST_FD, Buffer.alloc(SIZE), 0, SIZE, 0, (err, bytesRead, buffer) => {
+ assert.ifError(err)
+ assert.equal(bytesRead, SIZE, 'bytesRead is correct')
+ assert(buffer.equals(TEST_DATA), 'data is correct')
+ done()
+ })
+ })
+
+ it('works when position is null', done => {
+ fs.read(TEST_FD, Buffer.alloc(SIZE), 0, SIZE, null, (err, bytesRead, buffer) => {
+ assert.ifError(err)
+ assert.equal(bytesRead, SIZE, 'bytesRead is correct')
+ assert(buffer.equals(TEST_DATA), 'data is correct')
+ done()
+ })
+ })
+ })
+})
+
+describe('fs.write()', () => {
+ let TEST_FILE
+ let TEST_DATA
+ let TEST_FD
+
+ beforeEach(() => {
+ TEST_FILE = path.join(os.tmpdir(), 'fs-extra', 'write-test-file')
+ TEST_DATA = crypto.randomBytes(SIZE)
+ fs.ensureDirSync(path.dirname(TEST_FILE))
+ TEST_FD = fs.openSync(TEST_FILE, 'w')
+ })
+
+ afterEach(() => {
+ return fs.close(TEST_FD)
+ .then(() => fs.remove(TEST_FILE))
+ })
+
+ describe('with promises', () => {
+ it('returns an object', () => {
+ return fs.write(TEST_FD, TEST_DATA, 0, SIZE, 0)
+ .then(results => {
+ const bytesWritten = results.bytesWritten
+ const buffer = results.buffer
+ assert.equal(bytesWritten, SIZE, 'bytesWritten is correct')
+ assert(buffer.equals(TEST_DATA), 'data is correct')
+ })
+ })
+
+ onNode7it('returns an object when minimal arguments are passed', () => {
+ return fs.write(TEST_FD, TEST_DATA)
+ .then(results => {
+ const bytesWritten = results.bytesWritten
+ const buffer = results.buffer
+ assert.equal(bytesWritten, SIZE, 'bytesWritten is correct')
+ assert(buffer.equals(TEST_DATA), 'data is correct')
+ })
+ })
+
+ it('returns an object when writing a string', () => {
+ const message = 'Hello World!'
+ return fs.write(TEST_FD, message)
+ .then(results => {
+ const bytesWritten = results.bytesWritten
+ const buffer = results.buffer
+ assert.equal(bytesWritten, message.length, 'bytesWritten is correct')
+ assert.equal(buffer, message, 'data is correct')
+ })
+ })
+ })
+
+ describe('with callbacks', () => {
+ it('works', done => {
+ fs.write(TEST_FD, TEST_DATA, 0, SIZE, 0, (err, bytesWritten, buffer) => {
+ assert.ifError(err)
+ assert.equal(bytesWritten, SIZE, 'bytesWritten is correct')
+ assert(buffer.equals(TEST_DATA), 'data is correct')
+ done()
+ })
+ })
+
+ onNode7it('works when minimal arguments are passed', done => {
+ fs.write(TEST_FD, TEST_DATA, (err, bytesWritten, buffer) => {
+ assert.ifError(err)
+ assert.equal(bytesWritten, SIZE, 'bytesWritten is correct')
+ assert(buffer.equals(TEST_DATA), 'data is correct')
+ done()
+ })
+ })
+
+ it('works when writing a string', done => {
+ const message = 'Hello World!'
+ return fs.write(TEST_FD, message, (err, bytesWritten, buffer) => {
+ assert.ifError(err)
+ assert.equal(bytesWritten, message.length, 'bytesWritten is correct')
+ assert.equal(buffer, message, 'data is correct')
+ done()
+ })
+ })
+ })
+})
diff --git a/lib/fs/index.js b/lib/fs/index.js
index e20ae76..551ca6a 100644
--- a/lib/fs/index.js
+++ b/lib/fs/index.js
@@ -21,7 +21,6 @@ const api = [
'lstat',
'mkdir',
'open',
- 'read',
'readFile',
'readdir',
'readlink',
@@ -33,7 +32,6 @@ const api = [
'truncate',
'unlink',
'utimes',
- 'write',
'writeFile'
]
// fs.mkdtemp() was added in Node.js v5.10.0, so check if it exists
@@ -59,3 +57,45 @@ exports.exists = function (filename, callback) {
return fs.exists(filename, resolve)
})
}
+
+// fs.read() & fs.write need special treatment due to multiple callback args
+
+exports.read = function (fd, buffer, offset, length, position, callback) {
+ if (typeof callback === 'function') {
+ return fs.read(fd, buffer, offset, length, position, callback)
+ }
+ return new Promise((resolve, reject) => {
+ fs.read(fd, buffer, offset, length, position, (err, bytesRead, buffer) => {
+ if (err) return reject(err)
+ resolve({ bytesRead, buffer })
+ })
+ })
+}
+
+// Function signature can be
+// fs.write(fd, buffer[, offset[, length[, position]]], callback)
+// OR
+// fs.write(fd, string[, position[, encoding]], callback)
+// so we need to handle both cases
+exports.write = function (fd, buffer, a, b, c, callback) {
+ if (typeof arguments[arguments.length - 1] === 'function') {
+ return fs.write(fd, buffer, a, b, c, callback)
+ }
+
+ // Check for old, depricated fs.write(fd, string[, position[, encoding]], callback)
+ if (typeof buffer === 'string') {
+ return new Promise((resolve, reject) => {
+ fs.write(fd, buffer, a, b, (err, bytesWritten, buffer) => {
+ if (err) return reject(err)
+ resolve({ bytesWritten, buffer })
+ })
+ })
+ }
+
+ return new Promise((resolve, reject) => {
+ fs.write(fd, buffer, a, b, c, (err, bytesWritten, buffer) => {
+ if (err) return reject(err)
+ resolve({ bytesWritten, buffer })
+ })
+ })
+}
diff --git a/lib/json/index.js b/lib/json/index.js
index 717d48f..bae68d4 100644
--- a/lib/json/index.js
+++ b/lib/json/index.js
@@ -3,11 +3,11 @@
const u = require('universalify').fromCallback
const jsonFile = require('./jsonfile')
-jsonFile.outputJsonSync = require('./output-json-sync')
jsonFile.outputJson = u(require('./output-json'))
+jsonFile.outputJsonSync = require('./output-json-sync')
// aliases
-jsonFile.outputJSONSync = jsonFile.outputJSONSync
jsonFile.outputJSON = jsonFile.outputJson
+jsonFile.outputJSONSync = jsonFile.outputJsonSync
jsonFile.writeJSON = jsonFile.writeJson
jsonFile.writeJSONSync = jsonFile.writeJsonSync
jsonFile.readJSON = jsonFile.readJson
diff --git a/lib/move/__tests__/move-prevent-moving-into-itself.test.js b/lib/move/__tests__/move-prevent-moving-into-itself.test.js
new file mode 100644
index 0000000..4b98fc6
--- /dev/null
+++ b/lib/move/__tests__/move-prevent-moving-into-itself.test.js
@@ -0,0 +1,192 @@
+'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('+ move() - prevent moving into itself', () => {
+ let TEST_DIR, src, dest
+
+ beforeEach(() => {
+ TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'move-prevent-moving-into-itself')
+ src = path.join(TEST_DIR, 'src')
+ fs.mkdirpSync(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'`, done => {
+ const destFile = path.join(TEST_DIR, 'src', 'dest', 'destfile.txt')
+ return testSuccessFile(src, destFile, done)
+ })
+
+ it(`should move the file successfully when dest parent is 'src/src_dest'`, done => {
+ const destFile = path.join(TEST_DIR, 'src', 'src_dest', 'destfile.txt')
+ return testSuccessFile(src, destFile, done)
+ })
+
+ it(`should move the file successfully when dest parent is 'src/dest_src'`, done => {
+ const destFile = path.join(TEST_DIR, 'src', 'dest_src', 'destfile.txt')
+ return testSuccessFile(src, destFile, done)
+ })
+
+ it(`should move the file successfully when dest parent is 'src/dest/src'`, done => {
+ const destFile = path.join(TEST_DIR, 'src', 'dest', 'src', 'destfile.txt')
+ return testSuccessFile(src, destFile, done)
+ })
+
+ it(`should move the file successfully when dest parent is 'srcsrc/dest'`, done => {
+ const destFile = path.join(TEST_DIR, 'srcsrc', 'dest', 'destfile.txt')
+ return testSuccessFile(src, destFile, done)
+ })
+ })
+
+ describe('> when source is a directory', () => {
+ it(`should move the directory successfully when dest is 'src_dest'`, done => {
+ dest = path.join(TEST_DIR, 'src_dest')
+ return testSuccessDir(src, dest, done)
+ })
+
+ it(`should move the directory successfully when dest is 'src-dest'`, done => {
+ dest = path.join(TEST_DIR, 'src-dest')
+ return testSuccessDir(src, dest, done)
+ })
+
+ it(`should move the directory successfully when dest is 'dest_src'`, done => {
+ dest = path.join(TEST_DIR, 'dest_src')
+ return testSuccessDir(src, dest, done)
+ })
+
+ it(`should move the directory successfully when dest is 'src_dest/src'`, done => {
+ dest = path.join(TEST_DIR, 'src_dest', 'src')
+ return testSuccessDir(src, dest, done)
+ })
+
+ it(`should move the directory successfully when dest is 'src-dest/src'`, done => {
+ dest = path.join(TEST_DIR, 'src-dest', 'src')
+ return testSuccessDir(src, dest, done)
+ })
+
+ it(`should move the directory successfully when dest is 'dest_src/src'`, done => {
+ dest = path.join(TEST_DIR, 'dest_src', 'src')
+ return testSuccessDir(src, dest, done)
+ })
+
+ it(`should move the directory successfully when dest is 'src_src/dest'`, done => {
+ dest = path.join(TEST_DIR, 'src_src', 'dest')
+ return testSuccessDir(src, dest, done)
+ })
+
+ it(`should move the directory successfully when dest is 'src-src/dest'`, done => {
+ dest = path.join(TEST_DIR, 'src-src', 'dest')
+ return testSuccessDir(src, dest, done)
+ })
+
+ it(`should move the directory successfully when dest is 'srcsrc/dest'`, done => {
+ dest = path.join(TEST_DIR, 'srcsrc', 'dest')
+ return testSuccessDir(src, dest, done)
+ })
+
+ it(`should move the directory successfully when dest is 'dest/src'`, done => {
+ dest = path.join(TEST_DIR, 'dest', 'src')
+ return testSuccessDir(src, dest, done)
+ })
+
+ it('should move the directory successfully when dest is very nested that all its parents need to be created', done => {
+ 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, done)
+ })
+
+ it(`should return error when dest is 'src/dest'`, done => {
+ dest = path.join(TEST_DIR, 'src', 'dest')
+ return testError(src, dest, done)
+ })
+
+ it(`should return error when dest is 'src/src_dest'`, done => {
+ dest = path.join(TEST_DIR, 'src', 'src_dest')
+ return testError(src, dest, done)
+ })
+
+ it(`should return error when dest is 'src/dest_src'`, done => {
+ dest = path.join(TEST_DIR, 'src', 'dest_src')
+ return testError(src, dest, done)
+ })
+
+ it(`should return error when dest is 'src/dest/src'`, done => {
+ dest = path.join(TEST_DIR, 'src', 'dest', 'src')
+ return testError(src, dest, done)
+ })
+ })
+})
+
+function testSuccessFile (src, destFile, done) {
+ const srcFile = path.join(src, FILES[0])
+
+ fs.move(srcFile, destFile, err => {
+ assert.ifError(err)
+ const f0 = fs.readFileSync(destFile, 'utf8')
+ assert.strictEqual(f0, dat0, 'file contents matched')
+ assert(!fs.existsSync(srcFile))
+ return done()
+ })
+}
+
+function testSuccessDir (src, dest, done) {
+ const srclen = klawSync(src).length
+
+ assert(srclen > 2) // assert src has contents
+
+ fs.move(src, dest, err => {
+ assert.ifError(err)
+ const destlen = klawSync(dest).length
+
+ assert.strictEqual(destlen, srclen, 'src and dest length should be equal')
+
+ const f0 = fs.readFileSync(path.join(dest, FILES[0]), 'utf8')
+ const f1 = fs.readFileSync(path.join(dest, FILES[1]), 'utf8')
+ const f2 = fs.readFileSync(path.join(dest, FILES[2]), 'utf8')
+ const f3 = fs.readFileSync(path.join(dest, FILES[3]), 'utf8')
+
+ assert.strictEqual(f0, dat0, 'file contents matched')
+ assert.strictEqual(f1, dat1, 'file contents matched')
+ assert.strictEqual(f2, dat2, 'file contents matched')
+ assert.strictEqual(f3, dat3, 'file contents matched')
+ assert(!fs.existsSync(src))
+ return done()
+ })
+}
+
+function testError (src, dest, done) {
+ fs.move(src, dest, err => {
+ assert(err)
+ assert.strictEqual(err.message, `Cannot move '${src}' to a subdirectory of itself, '${dest}'.`)
+ assert(fs.existsSync(src))
+ assert(!fs.existsSync(dest))
+ return done()
+ })
+}
diff --git a/lib/move/__tests__/move.test.js b/lib/move/__tests__/move.test.js
index 988a75c..342a8d2 100644
--- a/lib/move/__tests__/move.test.js
+++ b/lib/move/__tests__/move.test.js
@@ -173,19 +173,6 @@ describe('move', () => {
})
})
- 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}, err => {
- assert.strictEqual(err.code, 'ENOENT')
- done()
- })
- })
-
it('should create directory structure by default', done => {
const src = `${TEST_DIR}/a-file`
const dest = `${TEST_DIR}/does/not/exist/a-file-dest`
@@ -331,7 +318,7 @@ describe('move', () => {
})
})
- describe.skip('> when trying to move a folder into itself', () => {
+ describe('> 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')
@@ -342,7 +329,7 @@ describe('move', () => {
fse.move(SRC_DIR, DEST_DIR, err => {
assert(fs.existsSync(SRC_DIR))
- assert(err)
+ assert.equal(err.message, `Cannot move '${SRC_DIR}' to a subdirectory of itself, '${DEST_DIR}'.`)
done()
})
})
diff --git a/lib/move/index.js b/lib/move/index.js
index e5eb2c1..eeeb30f 100644
--- a/lib/move/index.js
+++ b/lib/move/index.js
@@ -13,40 +13,35 @@ const path = require('path')
const remove = require('../remove').remove
const mkdirp = require('../mkdirs').mkdirs
-function move (source, dest, options, callback) {
+function move (src, dest, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
- const shouldMkdirp = ('mkdirp' in options) ? options.mkdirp : true
const overwrite = options.overwrite || options.clobber || false
- if (shouldMkdirp) {
- mkdirs()
- } else {
- doRename()
- }
-
- function mkdirs () {
+ isSrcSubdir(src, dest, (err, itIs) => {
+ if (err) return callback(err)
+ if (itIs) return callback(new Error(`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`))
mkdirp(path.dirname(dest), err => {
if (err) return callback(err)
doRename()
})
- }
+ })
function doRename () {
- if (path.resolve(source) === path.resolve(dest)) {
- fs.access(source, callback)
+ if (path.resolve(src) === path.resolve(dest)) {
+ fs.access(src, callback)
} else if (overwrite) {
- fs.rename(source, dest, err => {
+ fs.rename(src, dest, err => {
if (!err) return callback()
if (err.code === 'ENOTEMPTY' || err.code === 'EEXIST') {
remove(dest, err => {
if (err) return callback(err)
options.overwrite = false // just overwriteed it, no need to do it again
- move(source, dest, options, callback)
+ move(src, dest, options, callback)
})
return
}
@@ -57,49 +52,44 @@ function move (source, dest, options, callback) {
remove(dest, err => {
if (err) return callback(err)
options.overwrite = false
- move(source, dest, options, callback)
+ move(src, dest, options, callback)
})
}, 200)
return
}
if (err.code !== 'EXDEV') return callback(err)
- moveAcrossDevice(source, dest, overwrite, callback)
+ moveAcrossDevice(src, dest, overwrite, callback)
})
} else {
- fs.link(source, dest, err => {
+ fs.link(src, dest, err => {
if (err) {
if (err.code === 'EXDEV' || err.code === 'EISDIR' || err.code === 'EPERM' || err.code === 'ENOTSUP') {
- moveAcrossDevice(source, dest, overwrite, callback)
- return
+ return moveAcrossDevice(src, dest, overwrite, callback)
}
- callback(err)
- return
+ return callback(err)
}
- fs.unlink(source, callback)
+ return fs.unlink(src, callback)
})
}
}
}
-function moveAcrossDevice (source, dest, overwrite, callback) {
- fs.stat(source, (err, stat) => {
- if (err) {
- callback(err)
- return
- }
+function moveAcrossDevice (src, dest, overwrite, callback) {
+ fs.stat(src, (err, stat) => {
+ if (err) return callback(err)
if (stat.isDirectory()) {
- moveDirAcrossDevice(source, dest, overwrite, callback)
+ moveDirAcrossDevice(src, dest, overwrite, callback)
} else {
- moveFileAcrossDevice(source, dest, overwrite, callback)
+ moveFileAcrossDevice(src, dest, overwrite, callback)
}
})
}
-function moveFileAcrossDevice (source, dest, overwrite, callback) {
+function moveFileAcrossDevice (src, dest, overwrite, callback) {
const flags = overwrite ? 'w' : 'wx'
- const ins = fs.createReadStream(source)
+ const ins = fs.createReadStream(src)
const outs = fs.createWriteStream(dest, { flags })
ins.on('error', err => {
@@ -113,7 +103,7 @@ function moveFileAcrossDevice (source, dest, overwrite, callback) {
fs.unlink(dest, () => {
// note: `err` here is from the input stream errror
if (err.code === 'EISDIR' || err.code === 'EPERM') {
- moveDirAcrossDevice(source, dest, overwrite, callback)
+ moveDirAcrossDevice(src, dest, overwrite, callback)
} else {
callback(err)
}
@@ -131,11 +121,11 @@ function moveFileAcrossDevice (source, dest, overwrite, callback) {
ins.pipe(outs)
function onClose () {
- fs.unlink(source, callback)
+ fs.unlink(src, callback)
}
}
-function moveDirAcrossDevice (source, dest, overwrite, callback) {
+function moveDirAcrossDevice (src, dest, overwrite, callback) {
const options = {
overwrite: false
}
@@ -150,13 +140,31 @@ function moveDirAcrossDevice (source, dest, overwrite, callback) {
}
function startNcp () {
- ncp(source, dest, options, err => {
+ ncp(src, dest, options, err => {
if (err) return callback(err)
- remove(source, callback)
+ remove(src, callback)
})
}
}
+// 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, cb) {
+ fs.stat(src, (err, st) => {
+ if (err) return cb(err)
+ if (st.isDirectory()) {
+ const baseDir = dest.split(path.dirname(src) + path.sep)[1]
+ if (baseDir) {
+ const destBasename = baseDir.split(path.sep)[0]
+ if (destBasename) return cb(null, src !== dest && dest.indexOf(src) > -1 && destBasename === path.basename(src))
+ return cb(null, false)
+ }
+ return cb(null, false)
+ }
+ return cb(null, false)
+ })
+}
+
module.exports = {
move: u(move)
}
diff --git a/lib/remove/rimraf.js b/lib/remove/rimraf.js
index 28d4aeb..15924c3 100644
--- a/lib/remove/rimraf.js
+++ b/lib/remove/rimraf.js
@@ -42,7 +42,7 @@ function rimraf (p, options, cb) {
rimraf_(p, options, function CB (er) {
if (er) {
- if (isWindows && (er.code === 'EBUSY' || er.code === 'ENOTEMPTY' || er.code === 'EPERM') &&
+ if ((er.code === 'EBUSY' || er.code === 'ENOTEMPTY' || er.code === 'EPERM') &&
busyTries < options.maxBusyTries) {
busyTries++
let time = busyTries * 100
@@ -289,7 +289,25 @@ function rmkidsSync (p, options) {
assert(p)
assert(options)
options.readdirSync(p).forEach(f => rimrafSync(path.join(p, f), options))
- options.rmdirSync(p, options)
+
+ // We only end up here once we got ENOTEMPTY at least once, and
+ // at this point, we are guaranteed to have removed all the kids.
+ // So, we know that it won't be ENOENT or ENOTDIR or anything else.
+ // try really hard to delete stuff on windows, because it has a
+ // PROFOUNDLY annoying habit of not closing handles promptly when
+ // files are deleted, resulting in spurious ENOTEMPTY errors.
+ const retries = isWindows ? 100 : 1
+ let i = 0
+ do {
+ let threw = true
+ try {
+ const ret = options.rmdirSync(p, options)
+ threw = false
+ return ret
+ } finally {
+ if (++i < retries && threw) continue // eslint-disable-line
+ }
+ } while (true)
}
module.exports = rimraf
diff --git a/package.json b/package.json
index e539c95..89887b6 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "fs-extra",
- "version": "3.0.1",
+ "version": "4.0.0",
"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": {
@@ -48,8 +48,9 @@
"read-dir-files": "^0.1.1",
"rimraf": "^2.2.8",
"secure-random": "^1.1.1",
+ "semver": "^5.3.0",
"standard": "^10.0.2",
- "standard-markdown": "^2.3.0"
+ "standard-markdown": "^4.0.1"
},
"main": "./lib/index",
"scripts": {
--
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