Skip to content
This repository has been archived by the owner on Feb 6, 2018. It is now read-only.

Commit

Permalink
Merge 135ee58 into 39011e9
Browse files Browse the repository at this point in the history
  • Loading branch information
blond committed Feb 1, 2017
2 parents 39011e9 + 135ee58 commit c591094
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 100 deletions.
35 changes: 22 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ Readable stream (`stream.Readable`) that has the following events:

| Event | Description |
|----------|-----|
|'data'|Returns a JavaScript object with information about a found file. </br></br>The example below shows a JSON interface with elements that are in the response for the `walk` method. Objects and keys have sample values.</br></br> **Example** </br></br><code> {</code></br><code>"entity": { "block": "page" },</code></br><code> "level": "libs/bem-core/desktop.blocks",</code></br><code>"tech": "bemhtml",</code></br><code>"path": "libs/bem-core/desktop.blocks/page/page.bemhtml.js"</code></br><code>}</code></br></br>`entity` — BEM entity.</br>`level` — Directory path.</br>`tech` — Implementation technology.</br>`path` — Relative path to the file.|
|'data'|Returns a JavaScript object with information about a found file. <br><br>The example below shows a JSON interface with elements that are in the response for the `walk` method. Objects and keys have sample values.<br><br> **Example** <br><br><code>{</code><br><code>  "cell": {</code><br><code>    "entity": { "block": "page" },</code><br><code>    "layer": "libs/bem-core/desktop.blocks",</code><br><code>    "tech": "bemhtml"</code><br><code>  },</code><br><code>  "path": "libs/bem-core/desktop.blocks/page/page.bemhtml.js"</code><br><code>}</code><br><br>`cell` — BEM cell instance.<br>`entity` — BEM entity name instance.<br>`layer` — Directory path.<br>`tech` — Implementation technology.<br>`path` — Relative path to the file.|
| 'error' | Generated if an error occurred while traversing the levels. Returns an object with the error description.|
| 'end' | Generated when `bem-walk` finishes traversing the levels defined in the `levels` object. |

Expand Down Expand Up @@ -221,11 +221,14 @@ const stream = walk(levels, {

/*
{ button:
[ { entity: { block: 'button', mod: { name: 'togglable', val: 'radio' } },
tech: 'spec.js',
path: 'libs/bem-components/common.blocks/button/_togglable/
button_togglable_radio.spec.js',
level: 'libs/bem-components/common.blocks' } ],
[ BemFile {
cell: BemCell {
entity: BemEntityName { block: 'button', mod: { name: 'togglable', val: 'radio' } },
tech: 'spec.js',
layer: 'libs/bem-components/common.blocks'
},
path: 'libs/bem-components/common.blocks/button/_togglable/
button_togglable_radio.spec.js' } ] },
...
}
*/
Expand Down Expand Up @@ -260,10 +263,14 @@ const stream = walk(levels, {
.on('end', () => console.log(files));

/*
[{ entity: { block: 'popup', mod: { name: 'target', val: true } },
tech: 'js',
path: 'libs/bem-components/common.blocks/popup/_target/popup_target.js',
level: 'libs/bem-components/common.blocks' },
[BemFile {
cell: BemCell {
entity: BemEntityName { block: 'popup', mod: { name: 'target', val: true } },
tech: 'js',
layer: 'libs/bem-components/common.blocks'
},
path: 'libs/bem-components/common.blocks/popup/_target/popup_target.js'
},
...
]
*/
Expand Down Expand Up @@ -304,10 +311,12 @@ const stream = walk(levels, {
.pipe(process.stdout);

/*
[{"entity":{"block":"search","elem":"header"},
"tech":"css",
[{"cell":{
"entity":{"block":"search","elem":"header"},
"tech":"css",
"layer":"common.blocks"
},
"path":"common.blocks/search/__header/search__header.css",
"level":"common.blocks",
"source":".search__header {\n\tdisplay: block;\n\tfont-size: 20px;\n\tcolor:
rgba(0,0,0,0.84);\n\tmargin: 0;\n\tpadding: 0 0 16px;\n\n}\n\n"},
...
Expand Down
24 changes: 24 additions & 0 deletions lib/bem-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

module.exports = class BemFile {
constructor(cell, path) {
this.cell = cell;
this.path = path;
}

get entity() {
return this.cell.entity;
}

get tech() {
return this.cell.tech;
}

get layer() {
return this.cell.layer;
}

get level() {
return this.cell.layer;
}
};
18 changes: 12 additions & 6 deletions lib/walkers/flat.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const path = require('path');

const bemNaming = require('bem-naming');
const BemEntityName = require('@bem/entity-name');
const BemCell = require('@bem/cell');

const BemFile = require('../bem-file');

/**
* Plugin to scan flat levels.
Expand Down Expand Up @@ -32,12 +35,15 @@ module.exports = (info, add, callback) => {
if (dotIndex > 0) {
const entity = parseEntityName(basename.substring(0, dotIndex));

entity && add({
path: path.join(levelpath, basename),
entity: new BemEntityName(entity),
tech: basename.substring(dotIndex + 1),
level: levelpath
});
if (entity) {
const cell = new BemCell({
entity: new BemEntityName(entity),
tech: basename.substring(dotIndex + 1),
layer: levelpath
});

add(new BemFile(cell, path.join(levelpath, basename)));
}
}
});

Expand Down
59 changes: 35 additions & 24 deletions lib/walkers/nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const path = require('path');
const each = require('async-each');
const bemNaming = require('bem-naming');
const BemEntityName = require('@bem/entity-name');
const BemCell = require('@bem/cell');

const BemFile = require('../bem-file');

/**
* Helper to scan one level.
Expand Down Expand Up @@ -98,12 +101,14 @@ class LevelWalker {

if (tech) {
if (blockname === stem) {
this.add({
entity: new BemEntityName({ block: blockname }),
tech: tech,
path: filename,
level: this.levelpath
});
this.add(new BemFile(
new BemCell({
entity: new BemEntityName({ block: blockname }),
tech: tech,
layer: this.levelpath
}),
filename
));
}

return cb();
Expand Down Expand Up @@ -137,12 +142,14 @@ class LevelWalker {

// Find file with same modifier name.
if (tech && entity && scope.block === entity.block && scope.modName === entity.modName) {
this.add({
entity: new BemEntityName(entity),
tech: tech,
path: item.path,
level: this.levelpath
});
this.add(new BemFile(
new BemCell({
entity: new BemEntityName(entity),
tech: tech,
layer: this.levelpath
}),
item.path
));
}

cb();
Expand All @@ -166,12 +173,14 @@ class LevelWalker {
if (this.naming.stringify(scope) === stem) {
const entity = this.naming.parse(stem);

this.add({
entity: new BemEntityName(entity),
tech: tech,
path: filename,
level: this.levelpath
});
this.add(new BemFile(
new BemCell({
entity: new BemEntityName(entity),
tech: tech,
layer: this.levelpath
}),
item.path
));
}

return cb();
Expand Down Expand Up @@ -205,12 +214,14 @@ class LevelWalker {
&& scope.elem === entity.elem
&& scope.modName === entity.modName
) {
this.add({
entity: new BemEntityName(entity),
tech: tech,
path: item.path,
level: this.levelpath
});
this.add(new BemFile(
new BemCell({
entity: new BemEntityName(entity),
tech: tech,
layer: this.levelpath
}),
item.path
));
}

cb();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"lib/**"
],
"dependencies": {
"@bem/cell": "0.2.1",
"@bem/entity-name": "1.1.0",
"async-each": "1.0.1",
"bem-naming": "1.0.1"
Expand Down
25 changes: 25 additions & 0 deletions test/core/bem-file.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const test = require('ava');

const BemEntityName = require('@bem/entity-name');
const BemCell = require('@bem/cell');

const BemFile = require('../../lib/bem-file');

test('should provide classic bem-file fields', t => {
const cell = new BemCell({
entity: new BemEntityName({ block: 'b', elem: 'e' }),
tech: 'css',
layer: 'bem-components/desktop'
});
const path = 'bem-components/desktop.blocks/b/__e/b__e.css';
const file = new BemFile(cell, path);

t.is(file.cell, cell);
t.is(file.entity, cell.entity);
t.is(file.tech, 'css');
t.is(file.level, cell.layer);
t.is(file.layer, cell.layer);
t.is(file.path, path);
});
8 changes: 4 additions & 4 deletions test/naming/naming.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test('should support original naming', t => {

return toArray(walk(['blocks'], options))
.then(files => {
const entities = files.map(file => file.entity.valueOf());
const entities = files.map(file => file.cell.entity.valueOf());

t.deepEqual(entities, [{
block: 'block',
Expand Down Expand Up @@ -56,7 +56,7 @@ test('should support Convention by Harry Roberts', t => {

return toArray(walk(['blocks'], options))
.then(files => {
const entities = files.map(file => file.entity.valueOf());
const entities = files.map(file => file.cell.entity.valueOf());

t.deepEqual(entities, [{
block: 'block',
Expand Down Expand Up @@ -88,7 +88,7 @@ test('should support custom naming', t => {

return toArray(walk(['blocks'], options))
.then(files => {
const entities = files.map(file => file.entity.valueOf());
const entities = files.map(file => file.cell.entity.valueOf());

t.deepEqual(entities, [{
block: 'block',
Expand Down Expand Up @@ -123,7 +123,7 @@ test('should support several naming', t => {

return toArray(walk(['original.blocks', 'csswizardry.blocks'], options))
.then(files => {
const entities = files.map(file => file.entity.valueOf());
const entities = files.map(file => file.cell.entity.valueOf());

t.deepEqual(entities, [
{
Expand Down
12 changes: 6 additions & 6 deletions test/schemes/flat/detect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('should detect block', t => {

return toArray(walk(['blocks'], options))
.then(files => {
const entities = files.map(file => file.entity.valueOf());
const entities = files.map(file => file.cell.entity.valueOf());

t.deepEqual(entities, [{ block: 'block' }]);
});
Expand All @@ -40,7 +40,7 @@ test('should detect bool mod of block', t => {

return toArray(walk(['blocks'], options))
.then(files => {
const entities = files.map(file => file.entity.valueOf());
const entities = files.map(file => file.cell.entity.valueOf());

t.deepEqual(entities, [{
block: 'block',
Expand All @@ -58,7 +58,7 @@ test('should detect key-val mod of block', t => {

return toArray(walk(['blocks'], options))
.then(files => {
const entities = files.map(file => file.entity.valueOf());
const entities = files.map(file => file.cell.entity.valueOf());

t.deepEqual(entities, [{
block: 'block',
Expand All @@ -76,7 +76,7 @@ test('should detect elem', t => {

return toArray(walk(['blocks'], options))
.then(files => {
const entities = files.map(file => file.entity.valueOf());
const entities = files.map(file => file.cell.entity.valueOf());

t.deepEqual(entities, [{ block: 'block', elem: 'elem' }]);
});
Expand All @@ -91,7 +91,7 @@ test('should detect bool mod of elem', t => {

return toArray(walk(['blocks'], options))
.then(files => {
const entities = files.map(file => file.entity.valueOf());
const entities = files.map(file => file.cell.entity.valueOf());

t.deepEqual(entities, [{
block: 'block',
Expand All @@ -110,7 +110,7 @@ test('should detect key-val mod of elem', t => {

return toArray(walk(['blocks'], options))
.then(files => {
const entities = files.map(file => file.entity.valueOf());
const entities = files.map(file => file.cell.entity.valueOf());

t.deepEqual(entities, [{
block: 'block',
Expand Down
30 changes: 15 additions & 15 deletions test/schemes/flat/levels.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ test('should support level name with extension', t => {
.then(files => {
const file = files[0];

t.deepEqual(file.entity.valueOf(), { block: 'block' });
t.is(file.level, 'name.blocks');
t.deepEqual(file.cell.entity.valueOf(), { block: 'block' });
t.is(file.cell.layer, 'name.blocks');
t.is(file.path, path.join('name.blocks', 'block.tech'));
t.is(file.tech, 'tech');
t.is(file.cell.tech, 'tech');
});
});

Expand All @@ -58,15 +58,15 @@ test('should support few levels', t => {
const file1 = files[0];
const file2 = files[1];

t.deepEqual(file1.entity.valueOf(), { block: 'block-1' });
t.is(file1.level, 'level-1');
t.deepEqual(file1.cell.entity.valueOf(), { block: 'block-1' });
t.is(file1.cell.layer, 'level-1');
t.is(file1.cell.tech, 'tech');
t.is(file1.path, path.join('level-1', 'block-1.tech'));
t.is(file1.tech, 'tech');

t.deepEqual(file2.entity.valueOf(), { block: 'block-2' });
t.is(file2.level, 'level-2');
t.deepEqual(file2.cell.entity.valueOf(), { block: 'block-2' });
t.is(file2.cell.layer, 'level-2');
t.is(file2.cell.tech, 'tech');
t.is(file2.path, path.join('level-2', 'block-2.tech'));
t.is(file2.tech, 'tech');
});
});

Expand All @@ -92,14 +92,14 @@ test('should detect entity with the same name on every level', t => {
const file1 = files[0];
const file2 = files[1];

t.deepEqual(file1.entity.valueOf(), { block: 'block' });
t.is(file1.level, 'level-1');
t.deepEqual(file1.cell.entity.valueOf(), { block: 'block' });
t.is(file1.cell.layer, 'level-1');
t.is(file1.cell.tech, 'tech');
t.is(file1.path, path.join('level-1', 'block.tech'));
t.is(file1.tech, 'tech');

t.deepEqual(file2.entity.valueOf(), { block: 'block' });
t.is(file2.level, 'level-2');
t.deepEqual(file2.cell.entity.valueOf(), { block: 'block' });
t.is(file2.cell.layer, 'level-2');
t.is(file2.cell.tech, 'tech');
t.is(file2.path, path.join('level-2', 'block.tech'));
t.is(file2.tech, 'tech');
});
});
Loading

0 comments on commit c591094

Please sign in to comment.