Skip to content

Commit

Permalink
Merge pull request #17 from blai/master
Browse files Browse the repository at this point in the history
Implements `move` function
  • Loading branch information
SBoudrias committed Jan 13, 2015
2 parents 5ab3e03 + f51aa93 commit a0f9f3d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ Copy the `from` file and parse its content as an underscore template where `cont

Optionally pass a template `settings` object.

### `#move(from, to, [options])`

Move/rename a file from the `from` path to the `to` path.

`#move` internally uses `#copy` and `#delete`, so `from` can be a glob pattern, and you can provide `options.globOptions` with it.

### `#exists(filepath)`

Returns `true` if a file exists. Returns `false` if the file is not found or deleted.
Expand Down
6 changes: 6 additions & 0 deletions actions/move.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = function (from, to, options) {
this.copy(from, to, options);
this.delete(from, options);
};
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ EditionInterface.prototype.delete = require('./actions/delete.js');
EditionInterface.prototype.copy = require('./actions/copy.js').copy;
EditionInterface.prototype._copySingle = require('./actions/copy.js')._copySingle;
EditionInterface.prototype.copyTpl = require('./actions/copy-tpl.js');
EditionInterface.prototype.move = require('./actions/move.js');
EditionInterface.prototype.commit = require('./actions/commit.js');

exports.create = function (store) {
Expand Down
64 changes: 64 additions & 0 deletions test/move.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';

var assert = require('assert');
var path = require('path');
var sinon = require('sinon');
var editor = require('..');
var memFs = require('mem-fs');

describe('#move()', function () {
beforeEach(function() {
var store = memFs.create();
this.fs = editor.create(store);
});

it('move file', function () {
var filepath = path.join(__dirname, 'fixtures/file-a.txt');
var initialContents = this.fs.read(filepath);
var newpath = '/new/path/file.txt';
this.fs.move(filepath, newpath);
assert.equal(this.fs.read(newpath), initialContents);
assert.throws(this.fs.read.bind(this.fs, filepath));
});

it('move directory', function() {
var filename = 'file.txt';
var dirpath = path.join(__dirname, 'fixtures/nested');
var filepath = path.join(dirpath, filename);
var newdirpath = '/new/path';
var newfilepath = path.join(newdirpath, filename);
this.fs.move(dirpath, newdirpath);
assert.equal(this.fs.store.get(newfilepath).state, 'modified');
assert.throws(this.fs.read.bind(this.fs, filepath));
});

it('move file to an existing `to` path', function() {
var filepath = path.join(__dirname, 'fixtures/file-a.txt');
var initialContents = this.fs.read(filepath);
var newpath = path.join(__dirname, 'fixtures/nested/file.txt');
this.fs.move(filepath, newpath);
assert.equal(this.fs.read(newpath), initialContents);
assert.throws(this.fs.read.bind(this.fs, filepath));
});

it('move directory to an existing `to` path (as a directory)', function () {
var dirpath = path.join(__dirname, 'fixtures/other');
var filepath = path.join(__dirname, 'another.txt');
var contents = 'another';
var fromdir = path.join(__dirname, 'fixtures/nested');

this.fs.write(filepath, contents);
this.fs.move(fromdir, dirpath);

assert.equal(this.fs.read(path.join(dirpath, 'file.txt')), 'nested\n');
assert.equal(this.fs.read(filepath), contents);
assert.throws(this.fs.read.bind(this.fs, path.join(fromdir, 'file.txt')));
});

it('throws when moving directory to an existing `to` path (as a file)', function() {
var filepath = path.join(__dirname, 'fixtures/file-a.txt');
var frompath = path.join(__dirname, 'fixtures/nested');

assert.throws(this.fs.move.bind(this.fs, frompath, filepath));
});
});

0 comments on commit a0f9f3d

Please sign in to comment.