Created
January 29, 2014 14:34
-
-
Save dkushnikov/8689231 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 path = require('path'), | |
fs = require('fs'), | |
vm = require('vm'), | |
bem = require('bem'), | |
bemLevel = bem.require('./level'), | |
vow = require('vow'); | |
/** | |
* @class Bundle | |
* @property {String} bundlesPath Путь до всех бандлов | |
* @property {String} bundlePath Путь до текущего бандла | |
* @property {String} BEMHTMLFile Путь до BEMHTML-файла бандла | |
* @property {String} BEMTREEFile Путь до BEMTREE-файла бандла | |
* @property {String} jsFileName Имя JS-файла бандла | |
* @property {String} cssFileName Имя CSS-файла бандла | |
* @property {String} jsFile Путь до JS-файла бандла | |
* @property {String} cssFile Путь до CSS-файла бандла | |
* @property {Object} BEMHTML Объект для работы с BEMHTML | |
* @property {Object} BEMTREE Объект для работы с BEMTREE | |
*/ | |
/** | |
* Добавляет новый бандл | |
* @param {String} name Имя бандла | |
* @param {Object} [context] Объект переменных, которые будут доступны глобально в BEMTREE | |
* по умолчанию в глобальный контекст пробрасываются: console, require, Vow | |
* @constructor | |
*/ | |
function Bundle(bundleDir, name, context) { | |
this.bundleDir = bundleDir; | |
this.name = name; | |
this.context = context || {}; | |
this._setPath(); | |
} | |
Bundle.prototype = { | |
/** | |
* Установить информацию по путям до директорий и файлов бандла | |
*/ | |
_setPath: function () { | |
var bundlesPath = path.join(__dirname, '../', this.bundleDir), | |
bundlePath = path.join(bundlesPath, this.name), | |
relativeBundlePath = path.join('/', this.bundleDir, this.name); | |
this.path = bundlePath; | |
this.BEMHTMLFile = path.join(bundlePath, this.name + '.bemhtml.js'); | |
this.BEMTREEFile = path.join(bundlePath, this.name + '.bemtree.js'); | |
this.depsFile = path.join(bundlePath, this.name + '.deps.js'); | |
this.jsFileName = '_' + this.name + '.js'; | |
this.cssFileName = '_' + this.name + '.css'; | |
this.jsFile = path.join(bundlePath, this.jsFileName); | |
this.cssFile = path.join(bundlePath, this.cssFileName); | |
this.jsFileRel = path.join(relativeBundlePath, this.jsFileName); | |
this.cssFileRel = path.join(relativeBundlePath, this.cssFileName); | |
}, | |
/** | |
* Установить информацию по собранным файлам | |
*/ | |
setInfo: function () { | |
this.BEMHTML = require(this.BEMHTMLFile).BEMHTML; | |
this.BEMTREE = this._getBEMTREE(); | |
}, | |
/** | |
* Собрать бандл | |
* @returns {Promise} | |
*/ | |
make: function () { | |
var deferred = vow.defer(); | |
if (process.env.NODE_ENV === 'production') { | |
this.setInfo(); | |
deferred.resolve(); | |
return deferred.promise(); | |
} | |
process.env.BEMHTML_ENV = 'development'; | |
bemLevel.resetLevelsCache(); | |
return bem.api.make({ verbosity: 'info' }, [this.path]) | |
.then( | |
function () { | |
this.setInfo(); | |
}.bind(this), | |
function (error) { | |
console.log(error); | |
this.setInfo(); | |
}.bind(this) | |
); | |
}, | |
/** | |
* Получить переменную BEMTREE с нужным контекстом | |
* @returns {Object} | |
* @private | |
*/ | |
_getBEMTREE: function () { | |
var BEMTREEContent = fs.readFileSync(this.BEMTREEFile, 'utf-8'), | |
context = bem.util.extend({ | |
console: console, | |
require: require, | |
Vow: vow | |
}, this.context); | |
vm.runInNewContext(BEMTREEContent, context); | |
return context.BEMTREE; | |
} | |
}; | |
module.exports = Bundle; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment