Skip to content

Commit

Permalink
Fixes Issue bem-archive#317:
Browse files Browse the repository at this point in the history
	* Adds getSelector() to tech.js to change content of target file
	* Support changing tech of BEM entities
	* Couple of fixes to previous patch
  • Loading branch information
Baris Cicek committed Apr 9, 2013
1 parent 3934ab9 commit fbbc2a4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
50 changes: 42 additions & 8 deletions lib/commands/mv.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var Q = require('q'),
QFS = require('q-io/fs'),
UTIL = require('util'),
TECH = require('../tech'),
LEVEL = require('../level'),
U = require('../util');

module.exports = function() {
Expand Down Expand Up @@ -60,8 +62,8 @@ module.exports = function() {
force = opts.force,
source = args.source,
target = args.target || source,
sourceLevel = typeof opts.sourceLevel == 'string' ? require('../level').createLevel(opts.sourceLevel) : opts.defaultLevel,
targetLevel = typeof opts.targetLevel == 'string' ? require('../level').createLevel(opts.targetLevel) : opts.defaultLevel,
sourceLevel = typeof opts.sourceLevel == 'string' ? LEVEL.createLevel(opts.sourceLevel) : opts.defaultLevel,
targetLevel = typeof opts.targetLevel == 'string' ? LEVEL.createLevel(opts.targetLevel) : opts.defaultLevel,
items = getItems(sourceLevel, source, strict),
sameTech = true; // move between same techs

Expand Down Expand Up @@ -105,11 +107,11 @@ module.exports = function() {
// CASE: Refactor BEM entity in the context of the same level
// CASE: Move BEM entity to another level with refactor
// FIXME: Support changing of BEM entity type
console.log (items);
return Q.reject('bem mv: Changing of BEM entity type is not yet implemented');

}

console.log (items);
// Map BEM source and target BEM entities to paths
var paths = items.map(function(item) {
return {
Expand All @@ -125,12 +127,44 @@ module.exports = function() {

// Move
return Q.all(
paths.map(function(p) {
return Q.when(copy(p.source, p.target).then(

items.map(function(p) {

p = U.extend (true, p, {
sourcePath: getPath(sourceLevel, p.source),
targetPath: getPath(targetLevel, p.target),
target: {
BlockName: p.target.block,
ElemName: p.target.elem,
ModName: p.target.mod,
ModVal: p.target.modval,
},
source: {
BlockName: p.source.block,
ElemName: p.source.elem,
ModName: p.source.mod,
ModVal: p.source.modval,
},

});
return Q.when(copy(p.sourcePath, p.targetPath).then(
function () {
return removeEmptyDirectory (p.source);
}));

var targetTech = targetLevel.getTech(p.target.tech);
var sourceTech = sourceLevel.getTech(p.source.tech);

// read the content of targetPath then replace all occurences of sourceSelectors with targetSelectors
return Q.when (sourceTech.readContent(p.targetPath, p.target.suffix), function (content) {

var sourceSelector = sourceTech.getSelector(p.source);
var targetSelector = targetTech.getSelector(p.target);
var newContent = content.replace(sourceSelector, targetSelector);
return targetTech.storeBuildResult(p.targetPath, p.target.suffix, newContent);

});
}).then(
function () {
return removeEmptyDirectory (p.sourcePath);
}));
}))
.get(0);
});
Expand Down
14 changes: 13 additions & 1 deletion lib/tech.js
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,19 @@ var Q = require('qq'),
*/
getDependencies: function() {
return [];
}
},

/**
* Selector function used to prepare selector in tech file.
*
* @param {Object} vars Variables to use in template.
* @return {String}
*/
getSelector: function(vars) {
return vars.BlockName +
(vars.ElemName? '__' + vars.ElemName : '') +
(vars.ModName? '_' + vars.ModName + (vars.ModVal? '_' + vars.ModVal : '') : '');
}

}, {

Expand Down
16 changes: 12 additions & 4 deletions lib/techs/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@ exports.Tech = INHERIT(Tech, {
return '@import url(' + relPath + ');\n';
},

getCreateResult: function(path, suffix, vars) {

vars.Selector = '.' + vars.BlockName +
getSelector: function(vars) {
return '.' + vars.BlockName +
(vars.ElemName? '__' + vars.ElemName : '') +
(vars.ModName? '_' + vars.ModName + (vars.ModVal? '_' + vars.ModVal : '') : '');

},

getCreateResult: function(path, suffix, vars) {

vars.Selector = this.getSelector(vars);

return Template.process([
'{{bemSelector}}',
'{',
'}'],
vars);

}
},



});

0 comments on commit fbbc2a4

Please sign in to comment.