files-import
v1.4.1
Published
Traverse all files in a folder
Downloads
5
Readme
LICENSE
MIT
GOAL
Traverse all files in a folder
INSTALL
npm i files-import
HOW TO USE
# Afolder
# ├ Afile
# └ Bfile
# Bfolder
# └ Cfolder
# └ Efile
# Cfile
# Dfile
const path = require('path');
const Factory = require('files-import');
const factory = new Factory('myFolder');
factory.map(file => {
console.log(
file.folders.join('/') + '|' + path.basename(file.path)
)
});
# |Cfile
# |Dfile
# Afolder|Afile
# Afolder|Bfile
# Bfolder/Cfolder|Efile
factory
.exclude('Cfile')
.exclude('Dfile')
.include('A', Factory.PATH_TYPE.FOLDER)
.exclude('Afile', Factory.PATH_TYPE.FILE)
.map(file => {
console.log( path.basename(file.path) );
});
# Bfile
INCLUDE
// fac.include = 'folder';
// fac.include = /folder/;
// fac.include('folder');
// fac.include(/folder/);
factory.include(function(f) {
return f.path.indexOf('folder') !== -1;
}).map(file => {
console.log(
file.folders.join('/') + '|' + path.basename(file.path)
)
});
# Afolder|Afile
# Afolder|Bfile
# Bfolder/Cfolder|Efile
EXCLUDE
// fac.exclude = 'folder';
// fac.exclude = /folder/;
// fac.exclude('folder');
// fac.exclude(/folder/);
factory.exclude(function(f) {
return f.path.indexOf('folder') !== -1;
}).map(file => {
console.log(
file.folders.join('/') + '|' + path.basename(file.path)
)
});
# |Cfile
# |Dfile
SUGGESTION
/** wrong */
factory.map(function() {
if(file.folders[0] === 'test') return;
// other code
});
/** ok */
factory.exclude(f => {
return f.folders[0] === 'test';
}).map(function() {
// other code
});
/** traverse files in folder exclude folder inside */
factory.exclude(f => {
return f.folders[0];
}).map(function() {
// other code
});
# |Cfile
# |Dfile