From 33d5fda3758bf0fb1ff86d52add2793dbee9457f Mon Sep 17 00:00:00 2001 From: Baris Cicek Date: Mon, 8 Apr 2013 18:34:04 +0300 Subject: [PATCH] Implemented moving the file and added removeEmptryDirectory function --- lib/commands/mv.js | 90 ++++++++++++++++++++++++++++++++++------------ package.json | 1 + 2 files changed, 69 insertions(+), 22 deletions(-) diff --git a/lib/commands/mv.js b/lib/commands/mv.js index 5fea67c5..b0c1e6f4 100644 --- a/lib/commands/mv.js +++ b/lib/commands/mv.js @@ -1,5 +1,5 @@ var Q = require('q'), - QFS = require('q-fs'), + QFS = require('q-io/fs'), UTIL = require('util'), U = require('../util'); @@ -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') @@ -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'); } @@ -72,13 +80,11 @@ 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 { @@ -86,7 +92,6 @@ module.exports = function() { target: U.extend({}, source, target) } }); - } else { // CASE: Refactor BEM entity in the context of the same level @@ -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) { @@ -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)); + } } @@ -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')); @@ -150,7 +160,6 @@ function checkExists(paths, force) { return exists; }); - } function dryRun(paths) { @@ -165,11 +174,50 @@ 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; } @@ -177,9 +225,7 @@ function getPath(level, item) { function getBemEntityFilter(filter, strict) { var keys = Object.keys(filter); - return function(item) { - var res = true; if (strict && U.bemKey(filter) !== U.bemKey(item)) { diff --git a/package.json b/package.json index a6659e6e..1c1b7a0d 100644 --- a/package.json +++ b/package.json @@ -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",