Skip to content

Commit

Permalink
fix(levels-to-bemdecl): should rebuild if levels was changed
Browse files Browse the repository at this point in the history
  • Loading branch information
L0stSoul authored and blond committed Dec 3, 2016
1 parent 6d1d7b4 commit ed4bba2
Showing 1 changed file with 33 additions and 41 deletions.
74 changes: 33 additions & 41 deletions techs/levels-to-bemdecl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ var inherit = require('inherit'),
enb = require('enb'),
vfs = enb.asyncFS || require('enb/lib/fs/async-fs'),
BaseTech = enb.BaseTech || require('enb/lib/tech/base-tech'),
deps = require('../lib/deps/deps'),
asyncRequire = require('enb-async-require'),
clearRequire = require('clear-require');
deps = require('../lib/deps/deps');

/**
* @class LevelsToBemdeclTech
Expand Down Expand Up @@ -58,50 +56,44 @@ module.exports = inherit(BaseTech, {
cache = node.getNodeCache(target);

return node.requireSources([this._source]).spread(function (levels) {
if (cache.needRebuildFile('bemdecl-file', bemdeclFilename)) {
var resDeps = [],
decl = [],
data,
str;

levels.items.forEach(function (level) {
Object.keys(level.blocks).forEach(function (name) {
var block = level.blocks[name];

resDeps.push({
block: name
});

processMods(resDeps, name, block.mods);
processElems(resDeps, name, block.elements);
var resDeps = [],
decl = [],
data,
str;

levels.items.forEach(function (level) {
Object.keys(level.blocks).forEach(function (name) {
var block = level.blocks[name];

resDeps.push({
block: name
});

processMods(resDeps, name, block.mods);
processElems(resDeps, name, block.elements);
});
});

if (bemdeclFormat === 'deps') {
decl = resDeps;
data = { deps: decl };
str = 'exports.deps = ' + JSON.stringify(decl, null, 4) + ';\n';
} else {
decl = deps.toBemdecl(resDeps);
data = { blocks: decl };
str = 'exports.blocks = ' + JSON.stringify(decl, null, 4) + ';\n';
}

return vfs.write(bemdeclFilename, str, 'utf8')
.then(function () {
cache.cacheFileInfo('bemdecl-file', bemdeclFilename);
node.resolveTarget(target, data);
});
if (bemdeclFormat === 'deps') {
decl = resDeps;
data = { deps: decl };
str = 'exports.deps = ' + JSON.stringify(decl, null, 4) + ';\n';
} else {
node.isValidTarget(target);
clearRequire(bemdeclFilename);
decl = deps.toBemdecl(resDeps);
data = { blocks: decl };
str = 'exports.blocks = ' + JSON.stringify(decl, null, 4) + ';\n';
}

return asyncRequire(bemdeclFilename)
.then(function (result) {
node.resolveTarget(target, result);
return null;
});
if (cache.get('bemdecl') === str) {
node.isValidTarget(target);
node.resolveTarget(target, data);
}

return vfs.write(bemdeclFilename, str, 'utf8')
.then(function () {
cache.set('bemdecl', str);
node.resolveTarget(target, data);
});
});
}
});
Expand Down

0 comments on commit ed4bba2

Please sign in to comment.