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

Refactored walk arguments #30

Merged
merged 1 commit into from
Jul 2, 2015
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
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