Skip to content

Commit

Permalink
Implemented moving the file and added removeEmptryDirectory function
Browse files Browse the repository at this point in the history
  • Loading branch information
Baris Cicek committed Apr 8, 2013
1 parent ecc596b commit 33d5fda
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
90 changes: 68 additions & 22 deletions lib/commands/mv.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Q = require('q'),
QFS = require('q-fs'),
QFS = require('q-io/fs'),
UTIL = require('util'),
U = require('../util');

Expand All @@ -9,17 +9,21 @@ module.exports = function() {
.title('BEM entities move and rename tool.')
.helpful()
.apply(U.chdirOptParse)
.opt()
.name('defaultLevel')
.title('default level')
.short('l').long('level')
.apply(U.applyLevelOpt('default level'))
.end()
.opt()
.name('sourceLevel')
.title('source level')
.short('s').long('src')
.apply(U.applyLevelOpt('source level'))
.end()
.opt()
.name('targetLevel')
.title('target level')
.short('t').long('target')
.apply(U.applyLevelOpt('destination level'))
.end()
.opt()
.name('cherryPick')
Expand Down Expand Up @@ -56,10 +60,14 @@ module.exports = function() {
force = opts.force,
source = args.source,
target = args.target || source,
sourceLevel = opts.sourceLevel,
targetLevel = opts.targetLevel,
sourceLevel = typeof opts.sourceLevel == 'string' ? require('../level').createLevel(opts.sourceLevel) : opts.defaultLevel,
targetLevel = typeof opts.targetLevel == 'string' ? require('../level').createLevel(opts.targetLevel) : opts.defaultLevel,
items = getItems(sourceLevel, source, strict);

if (items.length == 0) {
return Q.reject('bem mv: No source BEM entity found.')
}

if (!source.block) {
return Q.reject('bem mv: You should specify full BEM entity to move');
}
Expand All @@ -72,21 +80,18 @@ module.exports = function() {
if (U.bemKey(source) === U.bemKey(target) && sourceLevel.dir === targetLevel.dir) {
return Q.reject(UTIL.format("bem mv: Could not move '%s' to self", U.bemKey(source)));
}

if (U.bemType(source) === U.bemType(target)) {

// CASE: Rename BEM entity in the context of the same level
// CASE: Move BEM entity to another level
// CASE: Move BEM entity to another level with name change

items = items.map(
function(source) {
return {
source: source,
target: U.extend({}, source, target)
}
});

} else {

// CASE: Refactor BEM entity in the context of the same level
Expand All @@ -98,14 +103,12 @@ module.exports = function() {

// Map BEM source and target BEM entities to paths
var paths = items.map(function(item) {

return {
source: getPath(sourceLevel, item.source),
target: getPath(targetLevel, item.target)
}

});

return checkExists(paths, force)
.then(function(exists) {

Expand All @@ -114,20 +117,27 @@ module.exports = function() {
// Move
return Q.all(
paths.map(function(p) {
return copy(p.source, p.target);
return Q.when(copy(p.source, p.target).then(

function () {
return removeEmptyDirectory (p.source);
}));
}))
.get(0);

});

});

};

function getItems(level, item, strict) {

return level.getItemsByIntrospection()
.filter(getBemEntityFilter(item, strict));
var exists = U.isDirectory (level.path);

// If level directory does not exists return empty item list
if (!exists) {
return [];
} else {
return level.getItemsByIntrospection()
.filter(getBemEntityFilter(item, strict));
}

}

Expand All @@ -141,7 +151,7 @@ function checkExists(paths, force) {

return exists.then(function(exists) {

// Do not move if there are existent target paths and --force in not specified
// Do not move if there are existent target paths and --force is not specified
if (!force && exists.length) {
return Q.reject('bem mv: Following target paths are exist, run with ' +
'--force to overwrite:\n' + exists.join('\n'));
Expand All @@ -150,7 +160,6 @@ function checkExists(paths, force) {
return exists;

});

}

function dryRun(paths) {
Expand All @@ -165,21 +174,58 @@ function dryRun(paths) {

function copy(source, target) {

// FIXME: implement
console.log('Moving %s to %s', source, target);
return QFS.makeTree(QFS.directory(target)).then (
function () {
return QFS.move (source, target);
}
);

}

function removeEmptyDirectory(source) {

var path = Q.when (QFS.isDirectory (source), function (isDirectory) {

if (!isDirectory) {
return QFS.directory(source);
} else {
return source;
}

});

return Q.when (path, function (path) {
return QFS.list (path).then (function (list) {

if (list.length == 0) {
return Q.when(QFS.removeTree (path)).then (function () {

//call clearEmptyDirectory for parent in case this is the
//last file in the directory so it should be removed
var parent = QFS.join (path, '..');

return Q.fcall(removeEmptyDirectory, parent);
}, function (err) {
// might not be able to remove directory be silent
});

} else {
// not an empty directory but be silent
return false;
}
});
});
}

function getPath(level, item) {
return level.getByObj(item) + item.suffix;
}

function getBemEntityFilter(filter, strict) {

var keys = Object.keys(filter);

return function(item) {

var res = true;

if (strict && U.bemKey(filter) !== U.bemKey(item)) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"q": "~0.8.11",
"qq": "~0.3.4",
"q-fs": "~0.1.33",
"q-io" : "~1.6",
"q-http": "~0.1.16",
"mime": "~1.2.5",
"underscore": "~1.3.1",
Expand Down

1 comment on commit 33d5fda

@bariscicek
Copy link
Owner

Choose a reason for hiding this comment

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

Fixes partially Issue bem-archive#317 ready for test.

Please sign in to comment.