Skip to content

Instantly share code, notes, and snippets.

@xdghcnt
Created May 21, 2014 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xdghcnt/98f97216d4211df4ea87 to your computer and use it in GitHub Desktop.
Save xdghcnt/98f97216d4211df4ea87 to your computer and use it in GitHub Desktop.
var Vow = require('vow');
var vowFs = require('../lib/fs/async-fs');
var inherit = require('inherit');
var requireOrEval = require('../lib/fs/require-or-eval');
var asyncRequire = require('../lib/fs/async-require');
var dropRequireCache = require('../lib/fs/drop-require-cache');
module.exports = inherit(require('../lib/tech/base-tech'), {
getName: function () {
return 'html-from-bemtree-i18n';
},
configure: function () {
this._bemhtmlSource = this.node.unmaskTargetName(
this.getOption('bemhtmlTarget', this.node.getTargetName('bemhtml.js'))
);
this._bemtreeSource = this.node.unmaskTargetName(
this.getOption('bemtreeTarget', this.node.getTargetName('bemtree.js'))
);
this._datajsonSource = this.node.unmaskTargetName(
this.getOption('datajsonTarget', this.node.getTargetName('datajson.' + this.getOption('lang', this.node.getLanguages()[0]) + '.js'))
);
this._allLangSource = this.node.unmaskTargetName(
this.getOption('langAllTarget', this.node.getTargetName('lang.all.js'))
);
this._langSource = this.node.unmaskTargetName(
this.getOption(
'langTarget',
this.node.getTargetName('lang.' + this.getOption('lang', this.node.getLanguages()[0]) + '.js')
)
);
this._target = this.node.unmaskTargetName(
this.getOption(
'destTarget',
this.node.getTargetName(this.getOption('lang', this.node.getLanguages()[0]) + '.html')
)
);
},
getTargets: function () {
return [this.node.unmaskTargetName(this._target)];
},
getBuildResult: function (target, bemhtmlFile, bemtreeFile, datajson, allLangFile, langFile) {
var _this = this;
dropRequireCache(require, bemtreeFile);
dropRequireCache(require, bemhtmlFile);
dropRequireCache(require, allLangFile);
return Vow.all([
asyncRequire(bemtreeFile),
asyncRequire(bemhtmlFile),
asyncRequire(allLangFile)
]).spread(function (bemtree, bemhtml, i18n) {
console.log(bemtree);
return bemtree.BEMTREE.apply(datajson).then(function (viewjson) {
dropRequireCache(require, langFile);
return asyncRequire(langFile).then(function (keysets) {
if ((typeof i18n === 'function' || typeof keysets === 'function') && bemhtml.lib) {
if (typeof keysets === 'function') {
i18n = keysets(i18n);
}
bemhtml.lib.i18n = i18n;
}
var global = bemhtml.lib && bemhtml.lib.global;
if (global) {
global.lang = _this.getOption('lang');
global.setTld(_this.getOption('lang'));
}
console.log(langFile);
console.log('DATAJSON-------------------------\n', datajson);
console.log('BEMTREE result-------------------\n', viewjson);
if (!bemhtml.BEMHTML && bemhtml.lib) {
return bemhtml.apply(viewjson);
} else {
return bemhtml.BEMHTML.apply(viewjson);
}
});
});
});
},
isRebuildRequired: function (target) {
var cache = this.node.getNodeCache(target);
return cache.needRebuildFile('bemhtml-file', this.node.resolvePath(this._bemhtmlSource)) ||
cache.needRebuildFile('bemtree-file', this.node.resolvePath(this._bemtreeSource)) ||
cache.needRebuildFile('datajson-file', this.node.resolvePath(this._datajsonSource)) ||
cache.needRebuildFile('allLang-file', this.node.resolvePath(this._allLangSource)) ||
cache.needRebuildFile('lang-file', this.node.resolvePath(this._langSource)) ||
cache.needRebuildFile('html-file', this.node.resolvePath(target));
},
storeCache: function (target) {
var cache = this.node.getNodeCache(target);
cache.cacheFileInfo('bemhtml-file', this.node.resolvePath(this._bemhtmlSource));
cache.cacheFileInfo('bemtree-file', this.node.resolvePath(this._bemtreeSource));
cache.cacheFileInfo('datajson-file', this.node.resolvePath(this._datajsonSource));
cache.cacheFileInfo('allLang-file', this.node.resolvePath(this._allLangSource));
cache.cacheFileInfo('lang-file', this.node.resolvePath(this._langSource));
cache.cacheFileInfo('html-file', this.node.resolvePath(target));
},
build: function () {
var _this = this;
return this.node.requireSources(
[this._bemhtmlSource, this._bemtreeSource, this._datajsonSource, this._allLangSource, this._langSource]
).then(function () {
return Vow.when(_this.getTargets()).then(function (targets) {
var targetsToBuild = [];
return Vow.when(targets.map(function (target) {
return Vow.when(_this.isRebuildRequired(target)).then(function (rebuildRequired) {
if (!rebuildRequired) {
_this.node.isValidTarget(target);
_this.node.resolveTarget(target);
} else {
targetsToBuild.push(target);
}
});
})).then(function () {
if (targetsToBuild.length) {
dropRequireCache(require, _this.node.resolvePath(_this._datajsonSource));
return requireOrEval(_this.node.resolvePath(_this._datajsonSource)).then(function (datajson) {
return Vow.all(targetsToBuild.map(function (target) {
return Vow.when(_this.getBuildResult(
target,
_this.node.resolvePath(_this._bemhtmlSource),
_this.node.resolvePath(_this._bemtreeSource),
datajson,
_this.node.resolvePath(_this._allLangSource),
_this.node.resolvePath(_this._langSource)
)).then(function (res) {
return vowFs.write(_this.node.resolvePath(target), res, 'utf8');
}).then(function () {
_this.node.resolveTarget(target);
_this.storeCache(target);
});
}));
});
}
return null;
});
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment