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

Commit

Permalink
Merge a83a9cc into 21d02fb
Browse files Browse the repository at this point in the history
  • Loading branch information
blond committed Apr 14, 2016
2 parents 21d02fb + a83a9cc commit 75b5119
Show file tree
Hide file tree
Showing 16 changed files with 526 additions and 204 deletions.
3 changes: 0 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,3 @@ globals:
bench: false

extends: pedant

rules:
no-shadow: off
33 changes: 16 additions & 17 deletions benchmark/README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
benchmarks
==========

**2015-07-03** Latest results, using `Node.js@0.12` and latest versions of modules:
**2015-07-03** Latest results, using `Node.js@4` and latest versions of modules:

* `enb@0.15.0`
* `scan-level@0.0.4`

```
bem-walk
13,913 op/s » `flat` level
4,860 op/s » `nested` level
223 op/s » `bem-bl`
152 op/s » `bem-core` + `bem-components`
14,232 op/s » `flat` level
3,948 op/s » `nested` level
238 op/s » `bem-bl`
153 op/s » `bem-core` + `bem-components`
enb@0.x
8,894 op/s » `flat` level
5,829 op/s » `nested` level
150 op/s » `bem-bl`
86 op/s » `bem-core` + `bem-components`
scan-level
11,317 op/s » `flat` level
4,909 op/s » `nested` level
140 op/s » `bem-bl`
84 op/s » `bem-core` + `bem-components`
enb@0.x
9,879 op/s » `flat` level
6,567 op/s » `nested` level
146 op/s » `bem-bl`
87 op/s » `bem-core` + `bem-components`
scan-level
11,055 op/s » `flat` level
4,528 op/s » `nested` level
139 op/s » `bem-bl`
79 op/s » `bem-core` + `bem-components`
Suites: 3
Benches: 12
Elapsed: 17,992.13 ms
Elapsed: 19,547.69 ms
```
8 changes: 4 additions & 4 deletions benchmark/bem-walk.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ suite('bem-walk', function () {
set('mintime', 1000);

bench('`flat` level', function (done) {
run(fixtures.levels.flat, { scheme: 'flat' }, done);
run(fixtures.levels.flat, { defaults: { scheme: 'flat' } }, done);
});

bench('`nested` level', function (done) {
run(fixtures.levels.nested, { scheme: 'nested' }, done);
run(fixtures.levels.nested, { defaults: { scheme: 'nested' } }, done);
});

bench('`bem-bl`', function (done) {
run(fixtures.libs['bem-bl'], { scheme: 'nested' }, done);
run(fixtures.libs['bem-bl'], { defaults: { scheme: 'nested' } }, done);
});

bench('`bem-core` + `bem-components`', function (done) {
run(fixtures.libs.o2, { scheme: 'nested' }, done);
run(fixtures.libs.o2, { defaults: { scheme: 'nested' } }, done);
});
});

Expand Down
6 changes: 3 additions & 3 deletions benchmark/fixtures/bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "enb-bem-techs-bench-fixtures",
"private": true,
"dependencies": {
"bem-bl": "2.10.0",
"bem-core": "2.6.0",
"bem-components": "2.2.1"
"bem-bl": "2.11.0",
"bem-core": "2.9.0",
"bem-components": "2.5.0"
}
}
5 changes: 4 additions & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"dependencies": {
"enb": "0.15.0",
"scan-level": "0.0.4",
"vow": "0.4.10"
"vow": "0.4.12"
},
"devDependencies": {
"bower": "1.7.9"
}
}
111 changes: 33 additions & 78 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,95 +1,50 @@
'use strict';

const fs = require('fs');
const path = require('path');
const bemNaming = require('bem-naming');
const Readable = require('stream').Readable;
const schemes = require('./schemes');

const async = require('async');

const walkers = require('./walkers');

/**
* Scans levels in file system.
*
* If file or directory is valid BEM entity then will be called `add` with info about this file.
*
* @param {string[]} levels The paths to levels.
* @param {object} options The options.
* @param {object} options.levels The level map. The key is path to level, the value is options for this level.
* @param {object} options.defaults The options for levels by default.
* @param {object} options.defaults.naming Any options for `bem-naming`.
* @param {string} options.defaults.scheme The name of level scheme. Available values: `flat` or `nested`.
*
* @returns {module:stream.Readable} stream with info about found files and directories.
*/
module.exports = function (levels, options) {
options || (options = {});

const output = new Readable({ objectMode: true });
const defaults = options.defaults || {};
const levelConfigs = options.levels || {};
const defaultNaming = defaults.naming ? bemNaming(defaults.naming) : bemNaming;
const defaultScheme = (typeof defaults.scheme === 'string' ? schemes[defaults.scheme] : defaults.scheme)
|| schemes.nested;

levels = levels.map(levelname => {
let scheme, naming;

const config = levelConfigs[levelname];
const defaultNaming = defaults.naming;
const defaultWalker = (typeof defaults.scheme === 'string' ? walkers[defaults.scheme] : defaults.scheme)
|| walkers.nested;

if (config) {
scheme = config.scheme;
naming = config.naming;
}

return {
path: levelname,
walk: typeof scheme === 'string' ? schemes[scheme] : (scheme || defaultScheme),
naming: naming ? bemNaming(naming) : defaultNaming
};
});

function add(obj) {
output.push(obj);
}
const output = new Readable({ objectMode: true, read: () => {} });
const add = (obj) => output.push(obj);

function scan(level, callback) {
const levelname = level.path;
const step = level.walk(levelname, level.naming);

walk(levelname, 1, null, callback);

function walk(dir, depth, data, callback) {
fs.readdir(dir, (err, items) => {
if (err) { return callback(err); }
const config = levelConfigs[level];
const scheme = config && config.scheme;
const naming = config && config.naming || defaultNaming;
const walk = typeof scheme === 'string' ? walkers[scheme] : (scheme || defaultWalker);

let n = 0;
let l = items.length;

if (l === 0) {
callback();
} else {
for (let i = 0; i < l; ++i) {
const basename = items[i];
const filename = path.join(dir, basename);

step({
basename: basename,
dirname: dir,
filename: filename,
depth: depth,
data: data
}, add, deepWalk, next);
}
}

function deepWalk(dir, data) {
walk(dir, depth + 1, data, next);
}

function next(err) {
err && callback(err);

++n === l && callback();
}
});
}
walk({ path: level, naming: naming }, add, callback);
}

let n = levels.length;

output._read = function () {
n || output.push(null);

levels.length && scan(levels.shift(), err => {
err && output.emit('error', err);
--n || output.push(null);
});
};
async.each(levels, scan, err => {
err
? output.emit('error', err)
: output.push(null);
});

return output;
};
22 changes: 0 additions & 22 deletions lib/schemes/flat.js

This file was deleted.

74 changes: 0 additions & 74 deletions lib/schemes/nested.js

This file was deleted.

47 changes: 47 additions & 0 deletions lib/walkers/flat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const fs = require('fs');
const path = require('path');

const bemNaming = require('bem-naming');

/**
* Plugin to scan flat levels.
*
* @param {object} info The info about scaned level.
* @param {string} info.path The level path to scan.
* @param {object|string} info.naming The naming options.
* @param {function} add The function to provide info about found files.
* @param {function} callback The callback function.
*/
module.exports = function (info, add, callback) {
const levelpath = info.path;
// Create `bem-naming` instance for specified options.
const parseEntityName = bemNaming(info.naming).parse;

fs.readdir(levelpath, (err, files) => {
if (err) {
return callback(err);
}

files.forEach(basename => {
const splited = basename.split('.');

// has tech
if (splited.length > 1) {
const entity = parseEntityName(splited.shift());

if (entity) {
add({
path: path.join(levelpath, basename),
entity: entity,
tech: splited.join('.'),
level: levelpath
});
}
}
});

callback();
});
};
File renamed without changes.
Loading

0 comments on commit 75b5119

Please sign in to comment.