-
-
Save xdghcnt/191b24d4b9f43997d331 to your computer and use it in GitHub Desktop.
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
/** | |
* bemjson-from-datajson-i18n | |
* ====================== | |
* | |
* Собирает *bemjson*-файл с помощью *datajson.{lang}*, *bemtree*, *lang.all* и *lang.{lang}*. | |
* | |
* **Опции** | |
* | |
* * *String* **bemtreeFile** — Исходный BEMTREE-файл. По умолчанию — `?.bemtree.js`. | |
* * *String* **datajsonFiles** — Хэш с исходными datajson-файлами. ({d1: "path/to/json1", d2: "path/to/json2}) | |
* * *String* **langAllFile** — Исходный langAll-файл. По умолчанию — `?.lang.all.js`. | |
* * *String* **langFile** — Исходный lang-файл. По умолчанию — `?.lang.{lang}.js`. | |
* Если параметр lang не указан, берется первый из объявленных в проекте языков | |
* * *String* **target** — Результирующий HTML-файл. По умолчанию — `?.{lang}.html`. | |
* | |
*/ | |
var Vow = require("../../node_modules/enb/node_modules/vow/lib/vow"); | |
var vm = require("vm"); | |
var vfs = require("../../node_modules/enb/lib/fs/async-fs"); | |
var requireOrEval = require("../../node_modules/enb/lib/fs/require-or-eval"); | |
var asyncRequire = require("../../node_modules/enb/lib/fs/async-require"); | |
var dropRequireCache = require("../../node_modules/enb/lib/fs/drop-require-cache"); | |
var path = require("path"), | |
fs = require("fs"); | |
module.exports = require("enb/lib/build-flow").create() | |
.name("bemjson-from-datajson-i18n") | |
.target("target", "?.{lang}.html") | |
.useSourceFilename("bemtreeFile", "?.bemtree.js") | |
.useSourceFilename("langAllFile", "?.lang.all.js") | |
.useSourceFilename("langFile", "?.lang.{lang}.js") | |
.prepare(function () { | |
this._datajsonSources = this.getOption("datajsonFiles"); | |
this._dataJsonArray = Object.keys(this._datajsonSources).map(function (key) { | |
return { | |
name: key, | |
file: this._datajsonSources[key] | |
}; | |
}, this); | |
}) | |
.needRebuild(function (cache) { | |
var dataFilesChanged = this._dataJsonArray.filter(function (item) { | |
return cache.needRebuildFile("datajson-file-" + item.name, this.node.resolvePath(item.file)); | |
}, this).length > 0; | |
return cache.needRebuildFile("bemtree-file", this.node.resolvePath(this._bemtreeFile)) || | |
dataFilesChanged || | |
cache.needRebuildFile("allLang-file", this.node.resolvePath(this._allLangFile)) || | |
cache.needRebuildFile("lang-file", this.node.resolvePath(this._langFile)) || | |
cache.needRebuildFile("html-file", this.node.resolvePath(this._target)); | |
}) | |
.saveCache(function (cache) { | |
cache.cacheFileInfo("bemtree-file", this.node.resolvePath(this._bemtreeFile)); | |
cache.cacheFileInfo("allLang-file", this.node.resolvePath(this._allLangFile)); | |
cache.cacheFileInfo("lang-file", this.node.resolvePath(this._langFile)); | |
cache.cacheFileInfo("html-file", this.node.resolvePath(this._target)); | |
this._dataJsonArray.forEach(function (dataSource) { | |
cache.cacheFileInfo("datajson-file-" + dataSource.name, dataSource.file); | |
}, this); | |
}) | |
.builder(function (bemtreeFilename, allLangFilename, langFilename) { | |
var self = this, | |
dataFiles = this._dataJsonArray.map(function (dataSource) { | |
dropRequireCache(require, self.node.resolvePath(dataSource.file)); | |
dataSource.file = self.node.resolvePath(dataSource.file); | |
return dataSource; | |
}), | |
promises = dataFiles.map(function (dataFile) { | |
return requireOrEval(dataFile.file).then(function (data) { | |
dataFile.data = data; | |
return dataFile; | |
}); | |
}); | |
return Vow.all(promises).then(function (dataSources) { | |
var datajson = {}; | |
dataSources.forEach(function (item) { | |
datajson[item.name] = item.data; | |
}); | |
return Vow.all([ | |
asyncRequire(bemtreeFilename), | |
vfs.read(allLangFilename), | |
vfs.read(langFilename) | |
]).spread(function (bemtree, allLangSource, langSource) { | |
vm.runInThisContext(allLangSource, allLangFilename); | |
vm.runInThisContext(langSource, langFilename); | |
return bemtree.BEMTREE.apply(datajson).then(function (bemjson) { | |
return "module.exports = " + JSON.stringify(bemjson, null, self.getOption("debug") ? 4 : 0) + ";"; | |
}); | |
}); | |
}); | |
}) | |
.createTech(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment