How to fetch current filename that is being processed using gulp.src() - javascript

I have to run certain gulp task over multiple json files in a folder but the task would fetch files different location based on the filename.
I am able to run the task by passing the filename as an argument in cmd but I want to automate the script so that it would get executed for all the files in the src location.
gulp.task("writeJSON", function() {
dataObj = require("./src/data/" + argv["filename"] + ".json");
dataObjKeysList = require("./src/data/stats/" + argv["filename"] + ".json");
segregateData(dataObj, dataObjKeysList, tabspace, false);
gulp
.src("./src/metadata.html")
.pipe(rename(argv["filename"] + ".html"))
.pipe(gulp.dest("./src/output"));
});
Any help would be greatly appreciated.

I am able to resolve the above issue using node filestream. I found this useful article
Filewalker Source
Used the below utility function which take the directory path and callback as args.
function filewalker(dir, done) {
let results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file){
file = path.resolve(dir, file);
fs.stat(file, function(err, stat){
// If directory, execute a recursive call
if (stat && stat.isDirectory()) {
// Add directory to array [comment if you need to remove the directories from the array]
results.push(file);
filewalker(file, function(err, res){
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
results.push(file);
if (!--pending) done(null, results);
}
});
});
});
};
Added the below execution in my gulp task
filewalker("./src/data/stats/" , function(err, dataFilesList){
if(err){
throw err;
}
dataFilesList.map((name) => {
let fileName = path.basename(name);
fileName = fileName.split('.')[0];
gutil.log('Generating ' + fileName + '.html file.');
});
});

Related

Recursive directory search including files inside ZIP's and RAR's

i'm trying to find a solution for my problem. I need get all files inside a target directory, including files inside zip and rars. Is this possible? Currently i'm working with this version, that takes all files inside all directories, including zips and rars files, but not what is inside.
var fs = require('fs');
var path = require('path');
var walk = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = path.resolve(dir, file);
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
results.push(file);
if (!--pending) done(null, results);
}
});
});
});
};
Thanks in advance.
While recursively looking for file,if you get a zip file you can use module like adm-zip to getEntries of zip file and do the recursive search again.
You may look into some external library:
Like jszip: https://stuk.github.io/jszip/
Install via npm and when you're looking for file, if you get a .zip or .rar file you can use jszip to get the list of files inside of the .zip or .rar folder

Get file path with dynamic path inside

I need to create file from directory like following
application/userxxx.txt/manifest.txt
The path is constant except the userxxx which can be any alpha numeric
/application/
user12.txt
newfile.txt
newFile2.txt
There is only one file which start with user...
I think of using the which is currently not working..
fs.readdir('c://application', function (err, files) {
if (err) {
throw err;
}
and then get all the files under the application
and search for file which start with userabcd1234.txt and when I find it do the read file like following
readFile('application/userabcd1234/manifest.txt')
There is no two files inside application which start with /user. just one but after the user. and before the third '/manifest.txt' can be any random alpha numeric.
You can do something like
var filePath = path.join(__dirname, '../your path to application folder');
fs.readdir(filePath, function (err, files) {
if (err) {
return console.error(err);
}
files.forEach(function (file) {
if (file.indexOf('user') === 0) {
var relFilePath = filePath + '/' + file + '/manifest.txt';
fs.readFile(relFilePath,'utf8', function read(err, data) {
if (err) {
throw err;
}
console.log(data);
});
}
});
});

Node.js possible callback endless call?

I'm actually trying to learn a bit about node.js
At the moment I try to understand the principles about callbacks.
I've written a module that should filter me files from a given directory by a specified file extension. But it won't work. I've tested a bit and I noticed that the function 'getFiles' will be called more the ones. But why? I can't find the mistake, can someone help me, to understood my problem? If someone thinks my code is ugly, please give me a better example, thanks.
So here's my code:
//Returns a list of filtered files from a specified directory and extension
function getFilteredFiles(dir, ext, callback)
{
getFiles(dir, function(error, data){
if(error)
{
callback(error);
}
else
{
var result = getFilteredFiles(data, ext);
callback(null, result);
}
});
}
//Reading files from a given directory
function getFiles(dir, callback)
{
var fs = require('fs');
console.log(typeof dir);
fs.readdir(dir, function(err, data){
if(err)
{
callback(err);
}
else
{
callback(null, data);
}
});
}
//Filters a file by a specified extension
function filterFiles(data, extension)
{
var path = require('path');
return data.filter(function(file){
return path.extname(file) == '.' + extension;
});
}

Node: Traversing directories in a recursion

I'm pretty new to Node...I need to hammer Node's async behavior and callback structure into my mind. Here where I struggle right now:
// REQUIRE --------------------------------------------------------------------
fs = require('fs');
path = require('path');
// FUNCTION readAllDirs -------------------------------------------------------
function readAllDirs(dir, result) {
if (!result) {
result = function() {};
};
fs.readdir(dir, function(err, list) {
if(err) { return result(err) };
list.forEach(function(file) {
var fullpath = path.resolve(dir, file);
fs.stat(fullpath, function(err, stat) {
if(err) { return result(err) };
if(stat && stat.isDirectory()) {
readAllDirs(fullpath);
//console.log('In: ' + fullpath);
result(null, fullpath);
}
});
});
});
}
// MAIN -----------------------------------------------------------------------
readAllDirs('/somedir', function(err, dirs) {
console.log(dirs);
});
I try to traverse a tree of directories. In principle the function is working nice...as long I do not callback but print the directory names on the console. All folders and sub-folders come up as expected. BUT when I do callbacks the callback is not called for recursion deeper than first level.
Pleeeaaassee help! Thanks guys!
Your problem is here, inside the if (stat ...) branch:
readAllDirs(fullpath);
You need to pass the supplied callback again back into the recursion:
readAllDirs(fullpath, result);

How to filter result array from node-dir package

I am using the node-dir package (https://www.npmjs.org/package/node-dir) to list files recursively from a path but I cannot succeed to add a filter to my result.
For instance, I want in my result array only files with 'mp3' extension.
Does anyone know how I can do that?
dir.files(__dirname, function(err, files.filter(ismp3file)) {
if (err) throw err;
console.log(files);
});
function ismp3file(elmt){return element.substr((~-element.lastIndexOf(".") >>> 0) + 2) === "mp3";}
i tried to add filter, but i got an error.
Thanks for helping
Try this:
var dir = require('node-dir');
function isMp3File(file) {
return (file.indexOf(".mp3") > -1);
}
dir.files(__dirname, function(err, files) {
if (err) throw err;
files = files.filter(isMp3File);
console.log(files);
});
Or if you prefer to verify the file ends with .mp3, you could add string.js as a dependency:
var dir = require('node-dir'),
S = require('string');
function isMp3File(file) {
return (S(file).endsWith('.mp3'));
}
dir.files(__dirname, function(err, files) {
if (err) throw err;
files = files.filter(isMp3File);
console.log(files);
});

Categories