Last active
December 12, 2015 12:29
-
-
Save awinogradov/fdf0eb4ab205c8f575ae to your computer and use it in GitHub Desktop.
Resolve deps by existing files on fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var walk = require('bem-walk'), | |
Promise = require('bluebird'), | |
toArray = require('stream-to-array'), | |
walkConfig = { | |
levels: { | |
'lib/bem-core/common.blocks': { scheme: 'nested' }, | |
'lib/bem-core/desktop.blocks': { scheme: 'nested' }, | |
'lib/bem-components/common.blocks': { scheme: 'nested' }, | |
'lib/bem-components/desktop.blocks': { scheme: 'nested' }, | |
'common.blocks': { scheme: 'nested' }, | |
'desktop.blocks': { scheme: 'flat' } | |
} | |
}; | |
var LEVELS = [ | |
'libs/bem-core/common.blocks', | |
'libs/bem-core/desktop.blocks', | |
'libs/bem-components/common.blocks', | |
'libs/bem-components/desktop.blocks', | |
'common.blocks', | |
'desktop.blocks' | |
], | |
DEPS = [ | |
{ block: 'control' }, | |
{ block: 'link', modName: 'pseudo', modVal: true }, | |
{ block: 'button' } | |
], | |
TECH = 'js'; | |
toArray(walk(LEVELS, walkConfig), | |
function (err, fsEntities) { | |
// TODO: move to gulp plugin | |
filterDeps(DEPS, fsEntities, TECH) | |
.then(function (readyToConcat) { | |
console.log(readyToConcat); | |
}); | |
}); | |
/** | |
* map bem-deps by bem-walk-entities | |
* @param {Array} deps – bem-deps [{ block, elem, modName, modVal }, ...] | |
* @param {Array} fsEntities – bem-walk [{ entity: { block, elem, modName, modVal }, tech }, ...] | |
* @param {String} tech - tech name: 'js' || 'css' || 'bemhtml' || ... | |
* @return {Array} - filtred deps with files | |
*/ | |
function filterDeps(deps, fsEntities, tech) { | |
var entitiesWithTech = []; | |
return new Promise(function (resolve, reject) { | |
deps.forEach(function (entity) { | |
var ewt = fsEntities.filter(function(o) { | |
if(o.tech !== tech) return; | |
if(o.entity.block !== entity.block) return; | |
if(o.entity.elem !== entity.elem) return; | |
if(o.entity.modName !== entity.modName) return; | |
if(o.entity.modVal !== entity.modVal) return; | |
return true; | |
}); | |
entitiesWithTech = [].concat(entitiesWithTech, ewt); | |
}); | |
resolve(entitiesWithTech); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ { entity: { block: 'control' }, | |
level: 'libs/bem-components/common.blocks', | |
tech: 'js', | |
path: 'libs/bem-components/common.blocks/control/control.js' }, | |
{ entity: { block: 'control' }, | |
level: 'libs/bem-components/desktop.blocks', | |
tech: 'js', | |
path: 'libs/bem-components/desktop.blocks/control/control.js' }, | |
{ entity: { block: 'link', modName: 'pseudo', modVal: true }, | |
level: 'libs/bem-components/common.blocks', | |
tech: 'js', | |
path: 'libs/bem-components/common.blocks/link/_pseudo/link_pseudo.js' }, | |
{ entity: { block: 'button' }, | |
level: 'libs/bem-components/common.blocks', | |
tech: 'js', | |
path: 'libs/bem-components/common.blocks/button/button.js' } ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment