Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bem-sdk in bemjson-to-bemdecl #266

Merged
merged 1 commit into from
Oct 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@
"enb": ">=0.15.0 <2.0.0"
},
"dependencies": {
"@bem/sdk.decl": "0.1.0",
"@bem/sdk.deps": "0.1.0",
"@bem/sdk.naming.entity.stringify": "0.2.0",
"@bem/sdk.bemjson-to-decl": "^0.2.0",
"@bem/sdk.cell": "0.2.0",
"@bem/sdk.decl": "0.2.1",
"@bem/sdk.deps": "0.2.0",
"@bem/sdk.naming.entity.parse": "0.2.0",
"@bem/sdk.naming.entity.stringify": "0.2.0",
"@bem/sdk.naming.presets": "0.0.3",
"bem-walk": "1.0.0-1",
"file-eval": "1.0.0",
"inherit": "2.2.6",
"lodash": "4.16.4",
"node-eval": "1.1.1",
"vow": "0.4.12"
},
"devDependencies": {
Expand Down
119 changes: 32 additions & 87 deletions techs/bemjson-to-bemdecl.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
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'),
fileEval = require('file-eval'),
deps = require('../lib/deps/deps');
'use strict';

const inherit = require('inherit');
const enb = require('enb');
const vfs = enb.asyncFS || require('enb/lib/fs/async-fs');
const BaseTech = enb.BaseTech || require('enb/lib/tech/base-tech');
const fileEval = require('file-eval');
const nodeEval = require('node-eval');
const BemCell = require('@bem/sdk.cell');
const bemjsonToDecl = require('@bem/sdk.bemjson-to-decl');
const bemDecl = require('@bem/sdk.decl');

/**
* @class BemjsonToBemdeclTech
Expand Down Expand Up @@ -51,33 +56,40 @@ module.exports = inherit(BaseTech, {

build: function () {
var node = this.node,
logger = node.getLogger(),
target = this._target,
cache = node.getNodeCache(target),
bemdeclFilename = node.resolvePath(target),
bemjsonFilename = node.resolvePath(this._sourceTarget),
bemdeclFormat = this._bemdeclFormat;

const convertBemdeclFormatName = (formatName) => {
const convertedFormatName = {
bemdecl: 'v1',
deps: 'enb'
}[formatName];

if (convertedFormatName) {
const msg = `Deprecated format ${formatName}. Use ${convertedFormatName} instead.`;

logger.logWarningAction('deprecate', this._target, msg);
}

return convertedFormatName || formatName;
};

return this.node.requireSources([this._sourceTarget])
.then(function () {
if (cache.needRebuildFile('bemdecl-file', bemdeclFilename) ||
cache.needRebuildFile('bemjson-file', bemjsonFilename)
) {
return fileEval(bemjsonFilename)
.then(function (bemjson) {
var bemjsonDeps = getDepsFromBemjson(bemjson),
decl,
data,
str;

if (bemdeclFormat === 'deps') {
decl = bemjsonDeps;
data = { deps: decl };
str = 'exports.deps = ' + JSON.stringify(decl, null, 4) + ';\n';
} else {
decl = deps.toBemdecl(bemjsonDeps),
data = { blocks: decl };
str = 'exports.blocks = ' + JSON.stringify(decl, null, 4) + ';\n';
}
const entities = bemjsonToDecl.convert(bemjson);
const cells = entities.map(entity => new BemCell({ entity }));
const bemdeclFormatName = convertBemdeclFormatName(bemdeclFormat);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

нахрен тебе столько переменных, пятнашка

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ну либо называл бы уже format и дальше { format }

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

нахрен тебе столько переменных

чтобы во время отладки смотреть промежуточный результат + переменные дают название промежуточному результату и становится понятно, что в нем

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ну Вегед бы не одобрил)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а какие аргументы?

const str = bemDecl.stringify(cells, { format: bemdeclFormatName, exportType: 'commonjs' });
const data = nodeEval(str);

return vfs.write(bemdeclFilename, str, 'utf-8')
.then(function () {
Expand All @@ -99,70 +111,3 @@ module.exports = inherit(BaseTech, {
}
});

function getDepsFromBemjson(bemjson) {
var deps = [];

addDepsFromBemjson(bemjson, deps, {}, null);

return deps;
}

function addDepsFromBemjson(bemjson, deps, depsIndex, parentBlockName) {
if (!bemjson) { return; }
if (Array.isArray(bemjson)) {
bemjson.forEach(function (bemjsonItem) {
addDepsFromBemjson(bemjsonItem, deps, depsIndex, parentBlockName);
});
} else {
if (bemjson.block || bemjson.elem) {
if (bemjson.elem && !bemjson.block) {
bemjson.block = parentBlockName;
}
var dep = { block: bemjson.block };
if (bemjson.elem) {
dep.elem = bemjson.elem;
}
var itemKey = depKey(dep);
if (!depsIndex[itemKey]) {
deps.push(dep);
depsIndex[itemKey] = true;
}
if (bemjson.elemMods) {
bemjson.mods = bemjson.elemMods;
}
if (bemjson.mods) {
for (var j in bemjson.mods) {
if (bemjson.mods.hasOwnProperty(j)) {
var subDep = { block: bemjson.block };
if (bemjson.elem) {
subDep.elem = bemjson.elem;
}
subDep.mod = j;
subDep.val = bemjson.mods[j];
var subItemKey = depKey(subDep);
if (!depsIndex[subItemKey]) {
deps.push(subDep);
depsIndex[subItemKey] = true;
}
}
}
}
}
for (var i in bemjson) {
if (bemjson.hasOwnProperty(i)) {
if (i !== 'mods' && i !== 'js' && i !== 'attrs') {
var value = bemjson[i];
if (Array.isArray(value) || (typeof value === 'object' && value !== null)) {
addDepsFromBemjson(bemjson[i], deps, depsIndex, bemjson.block || parentBlockName);
}
}
}
}
}
}

function depKey(dep) {
return dep.block +
(dep.elem ? '__' + dep.elem : '') +
(dep.mod ? '_' + dep.mod + (dep.val ? '_' + dep.val : '') : '');
}
12 changes: 3 additions & 9 deletions test/techs/bemjson-to-bemdecl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ describe('techs: bemjson-to-bemdecl', function () {
it('must detect boolean mod of block', function () {
var bemjson = { block: 'block', mods: { mod: true } },
bemdecl = [
{ name: 'block' },
{ name: 'block', mods: [{ name: 'mod', vals: [{ name: true }] }] }
{ name: 'block', mods: [{ name: 'mod', vals: [] }] }
];

return assert(bemjson, bemdecl);
Expand All @@ -38,7 +37,6 @@ describe('techs: bemjson-to-bemdecl', function () {
it('must detect mod of block', function () {
var bemjson = { block: 'block', mods: { 'mod-name': 'mod-val' } },
bemdecl = [
{ name: 'block' },
{ name: 'block', mods: [{ name: 'mod-name', vals: [{ name: 'mod-val' }] }] }
];

Expand All @@ -55,17 +53,15 @@ describe('techs: bemjson-to-bemdecl', function () {
it('must detect boolean mod of elem', function () {
var bemjson = { block: 'block', elem: 'elem', elemMods: { mod: true } },
bemdecl = [
{ name: 'block', elems: [{ name: 'elem' }] },
{ name: 'block', elems: [{ name: 'elem', mods: [{ name: 'mod', vals: [{ name: true }] }] }] }
{ name: 'block', elems: [{ name: 'elem', mods: [{ name: 'mod', vals: [] }] }] }
];

return assert(bemjson, bemdecl);
});

it('must detect boolean mod of elem', function () {
it('must detect string mod of elem', function () {
var bemjson = { block: 'block', elem: 'elem', elemMods: { 'mod-name': 'mod-val' } },
bemdecl = [
{ name: 'block', elems: [{ name: 'elem' }] },
{ name: 'block', elems: [{ name: 'elem', mods: [
{ name: 'mod-name', vals: [{ name: 'mod-val' }] }]
}] }
Expand Down Expand Up @@ -140,7 +136,6 @@ describe('techs: bemjson-to-bemdecl', function () {
]
},
bemdecl = [
{ name: 'block' },
{ name: 'block', elems: [{ name: 'elem' }] }
];

Expand Down Expand Up @@ -177,7 +172,6 @@ describe('techs: bemjson-to-bemdecl', function () {
{ block: 'block', mods: { 'mod-name': 'mod-val' } }
],
bemdecl = [
{ name: 'block' },
{ name: 'block', mods: [{ name: 'mod-name', vals: [{ name: 'mod-val' }] }] }
];

Expand Down