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

Commit

Permalink
refactor(nested): move eachDirItem to function
Browse files Browse the repository at this point in the history
  • Loading branch information
blond committed Apr 22, 2017
1 parent 8d6b3f9 commit e3e8ac2
Showing 1 changed file with 48 additions and 47 deletions.
95 changes: 48 additions & 47 deletions lib/walkers/nested.js
Expand Up @@ -10,6 +10,49 @@ const BemCell = require('@bem/cell');

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

/**
* Calls specified callback for each file or directory in specified directory.
*
* Each item is object with the following fields:
* * {string} path — the absolute path to file or directory.
* * {string} basename — the name of file or directory (the last portion of a path).
* * {string} stem - the name without tech name (complex extention).
* * {string} tech - the complex extention for the file or directory path.
*
* @param {string} dirname — the path to directory.
* @param {function} fn — the function that is called on each file or directory.
* @param {function} callback — the callback function.
*/
const eachDirItem = (dirname, fn, callback) => {
fs.readdir(dirname, (err, filenames) => {
if (err) {
return callback(err);
}

const files = filenames.map(basename => {
const dotIndex = basename.indexOf('.');

// has tech
if (dotIndex > 0) {
return {
path: path.join(dirname, basename),
basename: basename,
stem: basename.substring(0, dotIndex),
tech: basename.substring(dotIndex + 1)
};
}

return {
path: path.join(dirname, basename),
basename: basename,
stem: basename
};
});

each(files, fn, callback);
});
};

/**
* Helper to scan one level.
*/
Expand All @@ -27,55 +70,13 @@ class LevelWalker {

this.add = add;
}
/**
* Calls specified callback for each file or directory in specified directory.
*
* Each item is object with the following fields:
* * {string} path — the absolute path to file or directory.
* * {string} basename — the name of file or directory (the last portion of a path).
* * {string} stem - the name without tech name (complex extention).
* * {string} tech - the complex extention for the file or directory path.
*
* @param {string} dirname — the path to directory.
* @param {function} fn — the function that is called on each file or directory.
* @param {function} callback — the callback function.
*/
_eachDirItem (dirname, fn, callback) {
fs.readdir(dirname, (err, filenames) => {
if (err) {
return callback(err);
}

const files = filenames.map(basename => {
const dotIndex = basename.indexOf('.');

// has tech
if (dotIndex > 0) {
return {
path: path.join(dirname, basename),
basename: basename,
stem: basename.substring(0, dotIndex),
tech: basename.substring(dotIndex + 1)
};
}

return {
path: path.join(dirname, basename),
basename: basename,
stem: basename
};
});

each(files, fn, callback);
});
}
/**
* Scans the level fully.
*
* @param {function} callback — the callback function.
*/
scanLevel (callback) {
this._eachDirItem(this.levelpath, (item, cb) => {
eachDirItem(this.levelpath, (item, cb) => {
const entity = this.naming.parse(item.stem);
const type = entity && entity.type;

Expand All @@ -94,7 +95,7 @@ class LevelWalker {
* @param {function} callback — the callback function.
*/
scanBlockDir (dirname, blockname, callback) {
this._eachDirItem(dirname, (item, cb) => {
eachDirItem(dirname, (item, cb) => {
const filename = item.path;
const stem = item.stem;
const tech = item.tech;
Expand Down Expand Up @@ -136,7 +137,7 @@ class LevelWalker {
* @param {function} callback — the callback function.
*/
scanBlockModDir (dirname, scope, callback) {
this._eachDirItem(dirname, (item, cb) => {
eachDirItem(dirname, (item, cb) => {
const entity = this.naming.parse(item.stem);
const tech = item.tech;

Expand Down Expand Up @@ -164,7 +165,7 @@ class LevelWalker {
* @param {function} callback — the callback function.
*/
scanElemDir (dirname, scope, callback) {
this._eachDirItem(dirname, (item, cb) => {
eachDirItem(dirname, (item, cb) => {
const filename = item.path;
const stem = item.stem;
const tech = item.tech;
Expand Down Expand Up @@ -205,7 +206,7 @@ class LevelWalker {
* @param {function} callback — the callback function.
*/
scanElemModDir (dirname, scope, callback) {
this._eachDirItem(dirname, (item, cb) => {
eachDirItem(dirname, (item, cb) => {
const entity = this.naming.parse(item.stem);
const tech = item.tech;

Expand Down

0 comments on commit e3e8ac2

Please sign in to comment.