empty array on gulpjs watch - node.js

i am a nodejs newbie
i need to watch files changes on two subdirectories, when any change occur i need to update a service worker
all works fine, but why lista array is empty?
var gulp = require('gulp');
var watch = require('gulp-watch');
var path = require('path');
var fs = require('fs');
root = '/var/www/website.it';
dest = root + '/var/cache/misc/assets/'
var dests = [dest + 'js/tygh/*.js', dest + 'design/themes/responsive/css/*.css'];
gulp.task('default', function(){
watch(dests, function () {
var lista = [];
dests.forEach(function(dirname){
dirname = path.dirname(dirname);
fs.readdir(dirname, (err, files) => {
files.forEach(file => {
lista.push(dirname + '/' + file);
});
});
console.log(lista); // here print list of files
});
console.log(lista); // here print empty array []
});
});

Related

Stop nodejs child_process with browser api call

I have vue (axios) making a get call to an express route which triggers a child_process of ffmpeg in an infinite loop. ffmpeg streams one file over udp , on close it re calls itself and streams another file.
I'd like to be able to kill this process from a button on a web page, but can't seem to work it out.
This is my express route code
router.get('/test', function(req, res) {
const childProcess = require('child_process');
const fs = require('fs')
const path = require('path')
//Grabs a random index between 0 and length
function randomIndex(length) {
return Math.floor(Math.random() * (length));
}
function Stream () {
const FILE_SRC = '/path/to/file'
//Read the directory and get the files
const dirs = fs.readdirSync(FILE_SRC)
.map(file => {
return path.join(FILE_SRC, file);
});
const srcs_dup = [];
const hashCheck = {}; //used to check if the file was already added to srcs_dup
var numberOfFiles = dirs.length - 1; //OR whatever # you want
console.log(numberOfFiles)
//While we haven't got the number of files we want. Loop.
while (srcs_dup.length < numberOfFiles) {
var fileIndex = randomIndex(dirs.length-1);
//Check if the file was already added to the array
if (hashCheck[fileIndex] == true) {
continue; //Already have that file. Skip it
}
//Add the file to the array and object
srcs_dup.push(dirs[fileIndex]);
hashCheck[fileIndex] = true;
}
var chosen = "'" + srcs_dup[0] + "'"
var call = "ffmpeg -re -i " + chosen + " -content_type audio/mpeg -f mp3 udp://224.1.2.3:1234"
const stop = childProcess.exec(call, { shell: true });
stop.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
stop.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
stop.on('close', (code) => {
console.log ('child exited with code ' + code)
Stream();
});
stop.on('error', function(err) {
console.log('sh error' + err)
});
}

I have a directory with the list of files. I have to list files and find the file with maximum file size and insert the largest file in the mongodb

I have a directory with the list of files. I have to list files and find the file with maximum file size and insert the largest file in the mongodb.I have found out the maximum size file,but unable to print in console.It displays undefined.
const path = require('path');
const fs = require('fs');
var sleep = require('system-sleep');
var fsCompare = require('fs-compare');
var statssize = 0;
var foundFile;
var directorypath = path.join(__dirname, 'directory');
var fsfiles = fs.readdir(directorypath, function(err, data) {
var temp = [];
if (err) {
return console.error(err);
}
data.forEach(function(file) {
var fileSizeInMegabytes;
var pathtofiles = 'directory' + '\\' + file;
fs.stat(pathtofiles, function(err, stats) {
// sleep(5000);
if (err) {
return console.error(err);
}
if (statssize < stats.size) {
statssize = stats.size;
foundFile = pathtofiles;
}
fileSizeInMegabytes = statssize / 1000000.0;
});
});
console.log(foundFile + "foundfile");
console.log(statssize + "stats of file");
});
The code is asynchronous, i.e your console.log get executed before fs.stat() function returns any value.
console.log(foundFile + "foundfile");
console.log(statssize + "stats of file");
These should only be written inside the callback func of fs.stat() i.e right below this line
fileSizeInMegabytes = statssize / 1000000.0;
(y)

Node js App to display folder files

I am trying to display file list from folders .
my folder structure is like below
Invoices
1. error
2. processed
3. unprocessed
I have created node api for same which i am calling on my html page. code for the same is as below
const fs = require('fs');
var express = require('express');
var cors = require('cors');
var app = express();
app.use(cors());
app.use(express.static(__dirname));
var flength;
var filename;
var currentFile;
var items = [];
var dir1 = 'Invoices';
var filepath = [];
var readFolder = function(dir1) {
var countt = function(filename) {
var currentFile = dir1 + '/' + filename;
fs.readdir(currentFile, (err, files) => {
flength = files.length;
var fileArrayList = [];
for (var f in files) {
var record = {
filename: files[f],
filepath: dir1 + '/' + filename + '/' + files[f]
}
fileArrayList.push(record);
}
items.push({
'file': filename,
'count': flength,
'files': fileArrayList
});
});
}
var ReadFirst = function(dir1) {
fs.readdir(dir1, (err, files) => {
for (var i in files) {
var filename = files[i];
var currentFile = dir1 + '/' + filename;
var stats = fs.statSync(currentFile);
if (stats.isDirectory()) {
countt(filename);
}
}
});
}
ReadFirst(dir1);
}
setTimeout(function(str1, str2) {
readFolder(dir1);
}, 1000);
app.get('/FileCount', function(req, res) {
res.send(items);
});
app.listen(4000);
console.log('Listening on port 4000');
When i add or delete files from any folder then its not reflecting on my html page.need help for this.
thank you.
This is happening because of the way you've implemented this.
Your client (the HTML page) requests the server (NodeJS) API for
some data. In this case, it is the list of files in a folder. The server sends the response based on the state of files at the time (plus ∆).
You display those results in the HTML page. Now, there is no live link between your HTML page and your backend server. This means any changes that happen after this point won't be automatically reflected in the page.
You can do two things here:
Call your API repeatedly after an interval of few seconds. (If you're using AngularJS, look into setTimeout function.
Use sockets for having a real-time link. One good documentation here:
https://loopback.io/doc/en/lb2/Realtime-socket-io.html
This is more of a design issue and your NodeJS API looks fine.

Integrating GULP with node.js

I have a project in node.js for which I want to automate some backup and revision tasks in GULP.
I am able to successfully test following gulp code in terminal and everything working perfectly. The issue comes when I run gulp task from node.js code.
Gulp code:
var gulp = require('gulp');
var runSequence = require('run-sequence');
var rev = require('gulp-rev');
var format = require('date-format');
var dt = (new Date());
var gBkup = __dirname + '/backup/' + format.asString('ddMMyyyy_hhmm', dt);
var config = __dirname + '/gHelper.json', mnf = __dirname + '/rev-manifest.json';
var cssSrc = [], cssSrcO = [], cssSrcI = [];
var dirSrc = [], dirSrcO = [], dirSrcI = [];
gulp.task('init', function (){ // Initialize paths and all arrays containing file paths
var fexists = require('file-exists');
//console.log('Config exists: ' + fexists(config));
if (fexists(config)) {
config = require(config);
}
//console.log('Config object: ' + config);
if (fexists(mnf)) {
mnf = require(mnf);
}
for (var file in config.revision.ext_css) {
var fnm = __dirname + '/preview/' + config.revision.ext_css[file];
cssSrc.push(fnm);
if (mnf[config.revision.ext_css[file]] != "") {
var hnm = __dirname + '/live/' + mnf[config.revision.ext_css[file]];
cssSrcO.push(hnm);
console.log("Manifest: " + hnm);
}
}
for (var dir in config.revision.dir) {
dirSrc.push(__dirname + '/preview/' + config.revision.dir[dir]);
var dirnm = __dirname + '/live/' + config.revision.dir[dir];
dirnm = dirnm.substr(0, dirnm.length-3);
dirSrcO.push(dirnm);
console.log("Directory: " + dirnm);
}
// Files and directories will be ignored in revision
for (var file in config.revision.ext_css) {
cssSrcI.push('!' + __dirname + '/preview/' + config.revision.ext_css[file]);
}
for (var dir in config.revision.dir) {
dirSrcI.push('!' + __dirname + './preview/' + config.revision.dir[dir]);
}
//console.log('Ignore CSS: ' + cssSrcI);
//console.log('Ignore DIR: ' + dirSrcI);
});
// Revisioning Files
gulp.task('revisionCSS', function() { // Revise CSS scripts
var cssDest = __dirname + config.revision.ext_css_dest;
console.log('cssDestination: ' + cssDest);
return gulp.src(cssSrc)
.pipe(rev())
.pipe(gulp.dest(cssDest))
.pipe(rev.manifest({base: cssDest, merge: true}))
.pipe(gulp.dest(cssDest))
});
gulp.task('revInnerScripts', function () { // Revise javascripts
var dirDest = __dirname + config.revision.ext_dir_dest;
var cssDest = __dirname + config.revision.ext_css_dest;
console.log('dirInner: ' + dirDest);
console.log('cssInner: ' + cssDest);
return gulp.src(dirSrc)
.pipe(rev())
.pipe(gulp.dest(dirDest))
.pipe(rev.manifest({base: cssDest, merge: true}))
.pipe(gulp.dest(cssDest));
});
gulp.task('copyIgnoreRevision', function() { // Simply copy other/ignored files from array
var src = [__dirname + '/preview/**']
src = src.concat(cssSrcI);
src = src.concat(dirSrcI);
console.log(src)
return gulp.src(src)
.pipe(gulp.dest(__dirname + '/live'));
});
gulp.task('removeLive', function(callback) { // Removing files
var del = require('del');
var src = cssSrcO.concat(dirSrcO);
console.log("Removing Files: " + src);
return del(src);
});
gulp.task('backupLive', function() { // Backing up revision files before taking revision
// var src = ['./Live/**'];
gulp.src(cssSrcO).pipe(gulp.dest(gBkup));
return gulp.src(dirSrcO).pipe(gulp.dest(gBkup + "/js"));;
/* return gulp.src(cssSrcO, {read: false})
.pipe(clean());*/
});
gulp.task('backup', function(callback) { // Backup tasks list
runSequence('backupLive', 'removeLive', callback);
});
gulp.task('revise', ['copyIgnoreRevision', 'revisionCSS', 'revInnerScripts']);
gulp.task('revback', function (callback) {
runSequence('init', 'backup', 'revreplace', callback);
});
// Replacing references
gulp.task('revreplace', ['revise'], function(callback) { // In callback replace references for revised files
var revReplace = require('gulp-rev-replace');
var mReps = gulp.src(__dirname + '/rev-manifest.json');
return gulp.src(__dirname + '/preview/*.html')
.pipe(revReplace({manifest: mReps}))
.pipe(gulp.dest(__dirname + '/live'));
});
gHelper.json: Listing files which needs to be revised. Everything else will be copied to destination directory.
{
"revision": {
"ext_css" : [
"extie.css",
"responsive.css",
"style.css"
],
"ext_css_dest": "/live",
"dir": [
"js/*.js"
],
"ext_dir_dest": "/live/js"
}
}
Basic folder structure:
MainFolder/
gHelper.json
gulpfile.js
preview/
HTML files which contains references to revision files
Revision files (CSS and JS). CSS files are mentioned in gHelper.json
js/
Revision files (mainly js) which are to be revised as this folder is mentioned in gHelper.json and all files from the folder will be revised
When gulp task revback is invoked a folder live will be generated and added inside MainFolder. Again when revback is invoked, first, backup/{timestamp} folder will be generated taking backup of revised files only and then revision is made for live folder.
Lets see code from Node.js:
/* Publish client */
var gulp = require('gulp');
router.post('/api/:clientName/publish', function(req, res, next) {
var clientName = req.params.clientName;
var filePath = '/gulpfile'; // Full path for gulp file
console.log("Publish client: " + filePath);
try {
var gtask = require(filePath);
if (gulp.tasks.revback) {
console.log('gulp file contains task!');
gulp.start('revback');
}
} catch (err) {
return console.error(err);
}
});
Now the problem comes that sometimes gulp tasks are not being completed, rev-manifest.json is not created at proper position means inside MainFolder but created outside in the folder where this node.js lies.
Please let me know how to resolve the issue, Thanks.
Below is content of rev-manifest.json:
{
"dctConf.js": "dctConf-7c467cb7cb.js",
"extie.css": "extie-a8724bfb0c.css",
"responsive.css": "responsive-76492b9ad4.css",
"style.css": "style-770db73beb.css",
"translation.js": "translation-9687245bfb.js"
}
You could try changing the working directory in your gulpfile.js to the location of the gulpfile. Just add this at the top
process.chdir(__dirname);
Docs
https://nodejs.org/docs/latest/api/process.html#process_process_chdir_directory
https://nodejs.org/docs/latest/api/globals.html#globals_dirname
I have used gulp's native callbacks and removed run-sequence module.
E.g.:
gulp.task('revback', ['revise'], function(callback) {
var revReplace = require('gulp-rev-replace');
var mReps = gulp.src(__dirname + '/rev-manifest.json');
console.log('Manifest content: ' + mReps + ' && ' + __dirname + '/rev-manifest.json');
return gulp.src(__dirname + '/preview/*.html')
.pipe(revReplace({manifest: mReps}))
.pipe(gulp.dest(__dirname + '/live'))
.once('error', function(e) {
console.log('Error at revback: ' + e);
callback(e);
process.exit(1);
})
.once('end', function() {
console.log('Ending process at revback!');
callback();
process.exit();
});
});
gulp.task('revise', ['copyIgnoreRevision', 'revisionCSS', 'revInnerScripts']);
gulp.task('backupLive', ['init'], function() {
// var src = ['./Live/**'];
gulp.src(cssSrcO).pipe(gulp.dest(gBkup));
return gulp.src(dirSrcO).pipe(gulp.dest(gBkup + "/js"));
/* return gulp.src(cssSrcO, {read: false})
.pipe(clean());*/
});
This way, reverse chained to init function.

nodeschool learnyounode node.js module FILTER LS exercise

Below is the exercise 5 of nodeschool learnyounode module
Create a program that prints a list of files in a given directory, filtered by he extension of the files. You will be provided a directory name as the first agument to your program (e.g. /path/to/dir/) and a file extension to filter by as the second argument.
For example, if you get 'txt' as the second argument then you will need to filter the list to only files that end with .txt.
The list of files should be printed to the console, one file per line and have to use asynchronous I/O.
var fs = require('fs');
var path = require('path');
var mydir = process.argv[2];
var ext1 = process.argv[3]
fs.readdir(mydir, function(err, files){
if(err){
throw err
}
//console.log(files);
files.forEach(function(filename){
var ext = path.extname(filename);
if(ext == ext1){
console.log(filename);
}
});
});
When i run this i got the correct output, But when i verify output using learnyounode actual result not matching with expected result
Dont know where i went wrong. Can someone give me the solution plz???
Here's the official solution:
var fs = require('fs')
var path = require('path')
fs.readdir(process.argv[2], function (err, list) {
list.forEach(function (file) {
if (path.extname(file) === '.' + process.argv[3])
console.log(file)
})
})
Your problem is just a typo. You're doing this:
if(ext == ext){ // you're comparing the same variable
console.log(filename);
}
, but you should be doing this:
if(ext === ext1){ // try to use '==='
console.log(filename);
}
Other thing: they're not considering the . of .txt in the input, so you have to append this in your variable ext1 because .extname(file) returns the extention with the .:
var ext1 = '.' + process.argv[3];
You can try this code to solve this exercise :
var fs = require('fs');
function endsWith(str, suffix) {
var s = str.slice(str.length - suffix.length - 1);
if (s == "." + suffix)
return true;
else
return false;
};
fs.readdir(process.argv[2], function (err, list) {
if (process.argv[3]) {
for (var i = 0; i < list.length; i++) {
if (endsWith(list[i], process.argv[3]))
console.log(list[i]);
}
}
});
Here is what I came up with:
var fs = require('fs');
var filePath = process.argv[2];
var fileType = '.' + process.argv[3];
fs.readdir(filePath, function(err, list) {
for(var i=0; i<list.length; i++){
if (list[i].match(fileType)) {
console.log(list[i]);
}
}
});
Heres what I came up with, if you want other solutions to problem:
var fs = require('fs');
var path = process.argv[2]; //first argument
var extension = process.argv[3]; //second argument
var re = new RegExp("."+extension, "g"); //a regexp that matches every string that begins with a dot and is followed by the extension, i.e. .txt
fs.readdir(path, function callback(err, list){ //read the directory
if (!err) { //if no errors occur run next funtion
list.forEach(function(val) { //take the list and check every value with the statement below
if(re.test(val)) { //if the .test() rexexp-function does not match it will return a false, if it does it will return true
console.log(val); //if it matches console log the value
}
});
}
});
The only thing missing in your code is the concatenation of '.' before the file extension type.
var extension = '.'+ process.argv[3];
You can then do the comparison and printing.
thats how i solved it
var fs = require('fs');
const path = require("path")
var dir = process.argv[2],
ext = "."+process.argv[3];
function borer(callback){
fs.readdir(dir,function(err,list){
if(err){
console.log(err)
}else{
var row = list.filter((a)=>{
var regexp = new RegExp(ext+"$","ig")
if( a.search(regexp) > -1 ){
callback(a)
}
})
}
})
}
function print(f){
console.log(f)
}
borer(print)
The solution given uses the path module from Node JS package. The solution below doesn't use path, instead relies on simple deconstruction of given filename and using the parts needed.
-Import fs module
var fs = require('fs');
-Extract the path and ext name required from cmd line
let filePath = process.argv[2];
let extName = process.argv[3];
-Use (readdir) method to read the contents of a directory. The names of files inside the directory will be returned in the form of an array.
fs.readdir(filePath, 'utf-8', function(err, data) {
if (err) throw err;
data.forEach(element => {
-Take each element and split it into filename and extension name
let temp = element.split('.');
let tempSplit = temp[1];
if(tempSplit === extName) {
console.log(temp[0] + '.' + temp[1]);
}
});
Whole code for reference:
var fs = require('fs');
let filePath = process.argv[2];
let extName = process.argv[3];
fs.readdir(filePath, 'utf-8', function(err, data) {
if (err) throw err;
data.forEach(element => {
let temp = element.split('.');
let tempSplit = temp[1];
if(tempSplit === extName) {
console.log(temp[0] + '.' + temp[1]);
}
});

Resources