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

Commit

Permalink
Refactored walk arguments
Browse files Browse the repository at this point in the history
**1. The second argument now expects `config` instead of `options`.**

Options by default now need specify in `config.defaults` instead of
`options`.

*Before:*

```js
walk(['blocks'], { sheme: 'flat' });
```

*After:*

```js
var config = {
    defaults: { sheme: 'flat' }
};

walk(['blocks'], config);
```

**2. Info about levels (scheme and naming) now need specify in
`config.levels` instead of `levels` arguments.**

The `levels` argument as `Array` no longer supported.

*Before:*

```js
walk([{ path: 'blocks', sheme: 'flat' }]);
```

*After:*

```js
var config = {
    levels: {
        blocks: { sheme: 'flat' }
    }
};

walk(['blocks'], config);
```
  • Loading branch information
andrewblond committed Jul 2, 2015
1 parent 52e547b commit b1da862
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 31 deletions.
30 changes: 19 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@ var fs = require('fs'),
Readable = require('stream').Readable,
schemes = require('./schemes');

module.exports = function (levels, options) {
options || (options = {});
module.exports = function (levels, config) {
config || (config = {});

var output = new Readable({ objectMode: true }),
defaultScheme = options.scheme || schemes.nested,
defaultNaming = options.naming ? bemNaming(options.naming) : bemNaming;
defaults = config.defaults || {},
levelConfigs = config.levels || {},
defaultScheme = defaults.scheme || schemes.nested,
defaultNaming = defaults.naming ? bemNaming(defaults.naming) : bemNaming;

if (typeof defaultScheme === 'string') {
defaultScheme = schemes[defaultScheme];
}

levels = levels.map(function (level) {
var scheme = level && level.scheme,
naming = level && level.naming;
levels = levels.map(function (levelname) {
var config = levelConfigs[levelname],
scheme, naming;

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

return {
path: typeof level === 'string' ? level : level.path,
scheme: typeof scheme === 'string' ? schemes[scheme] : (scheme || defaultScheme),
path: levelname,
walk: typeof scheme === 'string' ? schemes[scheme] : (scheme || defaultScheme),
naming: naming ? bemNaming(naming) : defaultNaming
};
});
Expand All @@ -31,9 +38,10 @@ module.exports = function (levels, options) {
}

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

walk(level.path, 1, null, callback);
walk(levelname, 1, null, callback);

function walk(dir, depth, data, callback) {
fs.readdir(dir, function (err, items) {
Expand Down
4 changes: 2 additions & 2 deletions test/lib/assert.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var promisify = require('bluebird').promisify,
walk = require('../../lib/index');

function assert(levels, opts, expected, cb) {
function assert(levels, config, expected, cb) {
var buffer = [],
walker = walk(levels, opts),
walker = walk(levels, config),
hasError = false;

walker.on('data', function (obj) {
Expand Down
4 changes: 2 additions & 2 deletions test/lib/mock-and-assert.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var mock = require('mock-fs'),
assert = require('./assert');

module.exports = function (fs, levels, opts, expected) {
module.exports = function (fs, levels, config, expected) {
Object.keys(fs).forEach(function (level) {
var content = fs[level];

Expand All @@ -12,7 +12,7 @@ module.exports = function (fs, levels, opts, expected) {

mock(fs);

return assert(levels, opts, expected)
return assert(levels, config, expected)
.finally(function () {
mock.restore();
});
Expand Down
17 changes: 11 additions & 6 deletions test/lib/naming-assert.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var mockAndAssert = require('./mock-and-assert'),
opts = { scheme: 'flat' },
defaults = { scheme: 'flat' },
conventions = {
original: { elem: '__', mod: '_' },
csswizardry: { elem: '__', mod: '--' },
Expand All @@ -11,17 +11,22 @@ var mockAndAssert = require('./mock-and-assert'),
};

module.exports = function (fs, expected) {
var levels = Object.keys(fs).map(function (levelPath) {
var naming = levelPath.indexOf('.') !== -1 && levelPath.split('.')[0],
var config = {
levels: {},
defaults: defaults
};

Object.keys(fs).forEach(function (levelname) {
var naming = levelname.indexOf('.') !== -1 && levelname.split('.')[0],
convention = conventions[naming],
info = {
path: levelPath
path: levelname
};

convention && (info.naming = convention);

return info;
config.levels[levelname] = info;
});

return mockAndAssert(fs, levels, opts, expected);
return mockAndAssert(fs, Object.keys(config.levels), config, expected);
};
21 changes: 13 additions & 8 deletions test/lib/scheme-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ var mockAndAssert = require('./mock-and-assert'),
nested: true
};

module.exports = function (fs, opts, expected) {
module.exports = function (fs, defaults, expected) {
if (arguments.length === 2) {
expected = opts;
opts = {};
expected = defaults;
defaults = {};
}

var levels = Object.keys(fs).map(function (levelPath) {
var scheme = levelPath.indexOf('.') !== -1 && levelPath.split('.')[0],
var config = {
defaults: defaults,
levels: {}
};

Object.keys(fs).forEach(function (levelname) {
var scheme = levelname.indexOf('.') !== -1 && levelname.split('.')[0],
info = {
path: levelPath
path: levelname
};

schemes[scheme] && (info.scheme = scheme);

return info;
config.levels[levelname] = info;
});

return mockAndAssert(fs, levels, opts, expected);
return mockAndAssert(fs, Object.keys(config.levels), config, expected);
};
4 changes: 2 additions & 2 deletions test/schemes/flat.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var path = require('path'),
schemeAssert = require('../lib/scheme-assert'),
opts = { scheme: 'flat' },
defaults = { scheme: 'flat' },
assert = function (fs, expected) {
return schemeAssert(fs, opts, expected);
return schemeAssert(fs, defaults, expected);
};

describe('flat scheme', function () {
Expand Down

0 comments on commit b1da862

Please sign in to comment.