[Pkg-javascript-commits] [node-mv] 01/02: Imported Upstream version 2.0.1

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Mon Jun 30 06:33:56 UTC 2014


This is an automated email from the git hooks/post-receive script.

andrewrk-guest pushed a commit to branch master
in repository node-mv.

commit 764975643a63ac0aded1b754188fbbc61bba41e3
Author: Andrew Kelley <superjoe30 at gmail.com>
Date:   Mon Jun 30 06:28:21 2014 +0000

    Imported Upstream version 2.0.1
---
 .gitignore                         |   1 +
 .travis.yml                        |   5 ++
 README.md                          |  33 ++++++++++++
 index.js                           | 105 ++++++++++++++++++++++++++++++++++++
 package.json                       |  42 +++++++++++++++
 test/a-file                        |   1 +
 test/a-folder/another-file         |   1 +
 test/a-folder/another-folder/file3 |   1 +
 test/test.js                       | 106 +++++++++++++++++++++++++++++++++++++
 9 files changed, 295 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..07e6e47
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/node_modules
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..4a83e22
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+  - "0.11"
+  - "0.10"
+  - "0.8"
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..7708039
--- /dev/null
+++ b/README.md
@@ -0,0 +1,33 @@
+[![Build Status](https://secure.travis-ci.org/andrewrk/node-mv.png)](http://travis-ci.org/andrewrk/node-mv)
+
+Usage:
+------
+
+```js
+var mv = require('mv');
+
+mv('source/file', 'dest/file', function(err) {
+  // done. it tried fs.rename first, and then falls back to
+  // piping the source file to the dest file and then unlinking
+  // the source file.
+});
+```
+
+Another example:
+
+```js
+mv('source/dir', 'dest/a/b/c/dir', {mkdirp: true}, function(err) {
+  // done. it first created all the necessary directories, and then
+  // tried fs.rename, then falls back to using ncp to copy the dir
+  // to dest and then rimraf to remove the source dir
+});
+```
+
+Another example:
+
+```js
+mv('source/file', 'dest/file', {clobber: false}, function(err) {
+  // done. If 'dest/file' exists, an error is returned
+  // with err.code === 'EEXIST'.
+});
+```
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..e70342a
--- /dev/null
+++ b/index.js
@@ -0,0 +1,105 @@
+var fs = require('fs');
+var ncp = require('ncp').ncp;
+var path = require('path');
+var rimraf = require('rimraf');
+var mkdirp = require('mkdirp');
+
+module.exports = mv;
+
+function mv(source, dest, options, cb){
+  if (typeof options === 'function') {
+    cb = options;
+    options = {};
+  }
+  var shouldMkdirp = !!options.mkdirp;
+  var clobber = options.clobber !== false;
+  var limit = options.limit || 16;
+
+  if (shouldMkdirp) {
+    mkdirs();
+  } else {
+    doRename();
+  }
+
+  function mkdirs() {
+    mkdirp(path.dirname(dest), function(err) {
+      if (err) return cb(err);
+      doRename();
+    });
+  }
+
+  function doRename() {
+    if (clobber) {
+      fs.rename(source, dest, function(err) {
+        if (!err) return cb();
+        if (err.code !== 'EXDEV') return cb(err);
+        moveFileAcrossDevice(source, dest, clobber, limit, cb);
+      });
+    } else {
+      fs.link(source, dest, function(err) {
+        if (err) {
+          if (err.code === 'EXDEV') {
+            moveFileAcrossDevice(source, dest, clobber, limit, cb);
+            return;
+          }
+          if (err.code === 'EISDIR' || err.code === 'EPERM') {
+            moveDirAcrossDevice(source, dest, clobber, limit, cb);
+            return;
+          }
+          cb(err);
+          return;
+        }
+        fs.unlink(source, cb);
+      });
+    }
+  }
+}
+
+function moveFileAcrossDevice(source, dest, clobber, limit, cb) {
+  var outFlags = clobber ? 'w' : 'wx';
+  var ins = fs.createReadStream(source);
+  var outs = fs.createWriteStream(dest, {flags: outFlags});
+  ins.on('error', function(err){
+    ins.destroy();
+    outs.destroy();
+    outs.removeListener('close', onClose);
+    if (err.code === 'EISDIR' || err.code === 'EPERM') {
+      moveDirAcrossDevice(source, dest, clobber, limit, cb);
+    } else {
+      cb(err);
+    }
+  });
+  outs.on('error', function(err){
+    ins.destroy();
+    outs.destroy();
+    outs.removeListener('close', onClose);
+    cb(err);
+  });
+  outs.once('close', onClose);
+  ins.pipe(outs);
+  function onClose(){
+    fs.unlink(source, cb);
+  }
+}
+
+function moveDirAcrossDevice(source, dest, clobber, limit, cb) {
+  var options = {
+    stopOnErr: true,
+    clobber: false,
+    limit: limit,
+  };
+  if (clobber) {
+    rimraf(dest, function(err) {
+      if (err) return cb(err);
+      startNcp();
+    });
+  } else {
+    startNcp();
+  }
+  function startNcp() {
+    ncp(source, dest, options, function(errList) {
+      if (errList) return cb(errList[0]);
+      rimraf(source, cb);
+    });
+  }
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..e507676
--- /dev/null
+++ b/package.json
@@ -0,0 +1,42 @@
+{
+  "name": "mv",
+  "version": "2.0.1",
+  "description": "fs.rename but works across devices. same as the unix utility 'mv'",
+  "main": "index.js",
+  "scripts": {
+    "test": "mocha test/test.js --reporter spec"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git://github.com/andrewrk/node-mv.git"
+  },
+  "keywords": [
+    "mv",
+    "move",
+    "rename",
+    "device",
+    "recursive",
+    "folder"
+  ],
+  "author": "Andrew Kelley",
+  "license": "BSD",
+  "engines": {
+    "node": ">=0.8.0"
+  },
+  "devDependencies": {
+    "mocha": "~1.20.1",
+    "proxyquire": "~1.0.1"
+  },
+  "dependencies": {
+    "mkdirp": "~0.5.0",
+    "ncp": "~0.5.1",
+    "rimraf": "~2.2.8"
+  },
+  "bugs": {
+    "url": "https://github.com/andrewrk/node-mv/issues"
+  },
+  "homepage": "https://github.com/andrewrk/node-mv",
+  "directories": {
+    "test": "test"
+  }
+}
diff --git a/test/a-file b/test/a-file
new file mode 100644
index 0000000..94a709d
--- /dev/null
+++ b/test/a-file
@@ -0,0 +1 @@
+sonic the hedgehog
diff --git a/test/a-folder/another-file b/test/a-folder/another-file
new file mode 100644
index 0000000..31340c7
--- /dev/null
+++ b/test/a-folder/another-file
@@ -0,0 +1 @@
+tails
diff --git a/test/a-folder/another-folder/file3 b/test/a-folder/another-folder/file3
new file mode 100644
index 0000000..73a394d
--- /dev/null
+++ b/test/a-folder/another-folder/file3
@@ -0,0 +1 @@
+knuckles
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..ce0d528
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,106 @@
+var assert = require('assert');
+var proxyquire = require('proxyquire');
+var fs = require('fs');
+var rimraf = require('rimraf');
+
+describe("mv", function() {
+  // makes fs.rename return cross-device error.
+  var mock_fs = {};
+  mock_fs.rename = function(src, dest, cb) {
+    setTimeout(function() {
+      var err = new Error();
+      err.code = 'EXDEV';
+      cb(err);
+    }, 10);
+  };
+
+  it("should rename a file on the same device", function (done) {
+    var mv = proxyquire('../index', {});
+
+    mv("test/a-file", "test/a-file-dest", function (err) {
+      assert.ifError(err);
+      fs.readFile("test/a-file-dest", 'utf8', function (err, contents) {
+        assert.ifError(err);
+        assert.strictEqual(contents, "sonic the hedgehog\n");
+        // move it back
+        mv("test/a-file-dest", "test/a-file", done);
+      });
+    });
+  });
+
+  it("should not overwrite if clobber = false", function (done) {
+    var mv = proxyquire('../index', {});
+
+    mv("test/a-file", "test/a-folder/another-file", {clobber: false}, function (err) {
+      assert.ok(err && err.code === 'EEXIST', "throw EEXIST");
+      done();
+    });
+  });
+
+  it("should not create directory structure by default", function (done) {
+    var mv = proxyquire('../index', {});
+
+    mv("test/a-file", "test/does/not/exist/a-file-dest", function (err) {
+      assert.strictEqual(err.code, 'ENOENT');
+      done();
+    });
+  });
+
+  it("should create directory structure when mkdirp option set", function (done) {
+    var mv = proxyquire('../index', {});
+
+    mv("test/a-file", "test/does/not/exist/a-file-dest", {mkdirp: true}, function (err) {
+      assert.ifError(err);
+      fs.readFile("test/does/not/exist/a-file-dest", 'utf8', function (err, contents) {
+        assert.ifError(err);
+        assert.strictEqual(contents, "sonic the hedgehog\n");
+        // move it back
+        mv("test/does/not/exist/a-file-dest", "test/a-file", function(err) {
+          assert.ifError(err);
+          rimraf("test/does", done);
+        });
+      });
+    });
+  });
+
+  it("should work across devices", function (done) {
+    var mv = proxyquire('../index', {fs: mock_fs});
+    mv("test/a-file", "test/a-file-dest", function (err) {
+      assert.ifError(err);
+      fs.readFile("test/a-file-dest", 'utf8', function (err, contents) {
+        assert.ifError(err);
+        assert.strictEqual(contents, "sonic the hedgehog\n");
+        // move it back
+        mv("test/a-file-dest", "test/a-file", done);
+      });
+    });
+  });
+
+  it("should move folders", function (done) {
+    var mv = proxyquire('../index', {});
+
+    mv("test/a-folder", "test/a-folder-dest", function (err) {
+      assert.ifError(err);
+      fs.readFile("test/a-folder-dest/another-file", 'utf8', function (err, contents) {
+        assert.ifError(err);
+        assert.strictEqual(contents, "tails\n");
+        // move it back
+        mv("test/a-folder-dest", "test/a-folder", done);
+      });
+    });
+  });
+
+  it("should move folders across devices", function (done) {
+    var mv = proxyquire('../index', {fs: mock_fs});
+
+    mv("test/a-folder", "test/a-folder-dest", function (err) {
+      assert.ifError(err);
+      fs.readFile("test/a-folder-dest/another-folder/file3", 'utf8', function (err, contents) {
+        assert.ifError(err);
+        assert.strictEqual(contents, "knuckles\n");
+        // move it back
+        mv("test/a-folder-dest", "test/a-folder", done);
+      });
+    });
+  });
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-mv.git



More information about the Pkg-javascript-commits mailing list