Skip to content
This repository has been archived by the owner on Apr 9, 2019. It is now read-only.

Commit

Permalink
fix file find and file find spec
Browse files Browse the repository at this point in the history
  • Loading branch information
naxmefy committed Apr 6, 2016
1 parent ba5d7b1 commit fc62310
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 47 deletions.
83 changes: 39 additions & 44 deletions lib/file/find.js
Expand Up @@ -3,95 +3,90 @@
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const util = require('util');
const _ = require('lodash');

const debug = global.utils.debug.childLogger('file:find');

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

const find = exports = module.exports = function(target, config, currentDepth) {

console.log(target);
const f = glob.sync('*/', {
cwd: target,
ignore: config.exclude,
realpath: true
});
console.log(f);
return;



const find = exports = module.exports = function (target, config) {
// setup default config
const defaultConfig = {
patterns: ['*'],
recursive: false,
asArray: false,
onFound: x => x,
matcher: exports.Matcher.withoutDirectories,
depth: -1,
exclude: [
globConfig: {},
ignore: [
'.*'
]
};

config = _.merge({}, defaultConfig, config);
currentDepth = currentDepth || 0;

var foundFiles = config.asArray ? [] : {};

if(fs.existsSync(target) === false) {
if (fs.existsSync(target) === false) {
debug(`find failed. ${target} not exists`);
return foundFiles;
}

const add = (object, result) => {
if(result == null) {
if (result == null) {
result = object;
}

if(config.asArray) {
debug('trigger onFound action for: %s', object.name);

if (config.asArray) {
foundFiles.push(config.onFound(object));
} else {
foundFiles[object.path] = config.onFound(result);
}
};

fs.readdirSync(target).forEach(file => {
const fileObj = object(path.resolve(target, file));
if (config.recursive) {
config.patterns.push('**');
}

let pattern = config.patterns && config.patterns.length < 1 ? '*' : config.patterns[0];
if(config.patterns.length > 1) {
pattern = util.format('{%s}', config.patterns.join(','));
}
const files = glob.sync(pattern, _.merge({
cwd: target,
dot: true,
mark: true
}, config.globConfig));

// if it is a directory and config recursive is true, make
// deep search (but check currentDepth to config.depth)
if (fileObj.stats.isDirectory() && config.recursive) {
// if we would call this in same line with "isDirectory"
// directories would also add to foundFiles
if (config.depth === -1 || config.depth > currentDepth) {
add(fileObj, find(fileObj.path, config, currentDepth + 1));
}
files.forEach(file => {
const fileObj = object(path.resolve(target, file));
// Setup match results for matcher
var matchResult = false;
if (_.isFunction(config.matcher)) {
matchResult = config.matcher(fileObj, config);
}
else {
// Setup match results for matcher
var matchResult = false;
if (_.isFunction(config.matcher)) {
matchResult = config.matcher(fileObj, config);
}
else {
matchResult = config.matcher;
}
// if matchResult is true, save fileObj.path
if (matchResult) {
console.log(f);
debug("Found File: "+fileObj.path);
add(fileObj);
}
matchResult = config.matcher;
}
// if matchResult is true, save fileObj.path
if (matchResult) {
debug("Found File: " + fileObj.path);
add(fileObj);
}
});

return foundFiles;
};

exports.Matcher = {
onlyDirectories: function(fileObj) {
onlyDirectories: function (fileObj) {
return fileObj.stats.isDirectory();
},
withoutDirectories: function(fileObj) {
withoutDirectories: function (fileObj) {
return !fileObj.stats.isDirectory();
}
};
28 changes: 25 additions & 3 deletions test/file/find.spec.js
@@ -1,8 +1,30 @@
'use strict';

const path = require('path');

describe('file:find', function() {
it.skip('should return an object with files in fixtures folder', function () {
//const files = this.lib.file.find(`${__dirname}/fixtures`);
//console.log(files);
const fixturesPath = path.resolve(__dirname, 'fixtures');

const fixtureFiles = [
path.resolve(fixturesPath, '.foo'),
path.resolve(fixturesPath, '.bar'),
path.resolve(fixturesPath, 'foobar.js'),
path.resolve(fixturesPath, 'foobar.json'),

path.resolve(fixturesPath, 'sub', '.foo'),
path.resolve(fixturesPath, 'sub', '.foo'),

path.resolve(fixturesPath, 'sub', 'sub', '.bar'),
path.resolve(fixturesPath, 'sub', 'sub', '.bar')
];

it('should return an object with files in fixtures folder', function () {
const files = this.lib.file.find(fixturesPath, {
recursive: true
});

fixtureFiles.forEach(file => {
files.should.have.property(file);
});
});
});
Empty file added test/file/fixtures/sub/.bar
Empty file.
Empty file added test/file/fixtures/sub/.foo
Empty file.
Empty file added test/file/fixtures/sub/sub/.bar
Empty file.
Empty file added test/file/fixtures/sub/sub/.foo
Empty file.

0 comments on commit fc62310

Please sign in to comment.