events.js:142 error in node.js - javascript

I have a file , in javascript , that find all the directories that match the parameter.
And i got this error:
my code:
function getUserHome() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}
var home_path=getUserHome();
var findit = require('findit'),
path = require('path'),
finder = findit(path.resolve(home_path));
var myArgs = process.argv.slice(2)[0];
var filter1 = new RegExp(myArgs);
//This listens for directories found
finder.on('directory', function (dir) {
var directories = dir.split('\\');
var last= directories[directories.length-1].toLowerCase();
if(filter1.test(last)){
console.log('Directory: ' + dir );
}
});
(My code is a mess, i will clean it later)
How to fix that?

Why you didn't user the fs from Node, and look for dirs recursively? I think the error should be on the findit module...
That a look on https://nodejs.org/api/fs.html#fs_fs_readdir_path_callback or try instead the https://www.npmjs.com/package/recursive-readdir that also does it. I think that the things you use from the module findit, will be available there (like ways to ignore files)...
EDIT1: Example using recursive-readdir:
var recursive = require('recursive-readdir');
var filter1 = new RegExp(myArgs);
function ignoreFunc(file, stats) {
return !(stats.isDirectory() && filter1.test(path.basename(file)));
}
recursive('directory', [ignoreFunc] ,function (err, files) {
// Files is an array of filename (only the ones that matched the condition)
console.log(files);
});

#Moran, can you add a console.log directly in the callback of you "directory" event ?
finder.on('directory', function (dir) {
// Here
console.log(dir);
var directories = dir.split('\\');
var last= directories[directories.length-1].toLowerCase();
if(filter1.test(last)){
console.log('Directory: ' + dir );
}
});
To see what directory is problematic ? Then compare the rights applied on this folder and a directory that work, as "comverse" for example. It would help to find your error

Related

Change the way my nodejs app activates

The code listed below searches for files that contains a specified string under it's directory/subdirectories.
to activate it, you type node [jsfilename] [folder] [ext] [term]
i would like to change it so it will search without the base folder, i don't want to type ./ , just node [jsfilename] [ext] [term]
so it already know to search from it's location.
i know it has something to do with the process.argv but it need a hint what should i do.
PS:.
I already tried to change the last raw to :
searchFilesInDirectory(__dirname, process.argv[3], process.argv[2]);
it giving me noting...
const path = require('path');
const fs = require('fs');
function searchFilesInDirectory(dir, filter, ext) {
if (!fs.existsSync(dir)) {
console.log(`Welcome! to start, type node search [location] [ext] [word]`);
console.log(`For example: node search ./ .txt myterm`);
return;
}
const files = fs.readdirSync(dir);
const found = getFilesInDirectory(dir, ext);
let printed = false
found.forEach(file => {
const fileContent = fs.readFileSync(file);
const regex = new RegExp('\\b' + filter + '\\b');
if (regex.test(fileContent)) {
console.log(`Your word has found in file: ${file}`);
}
if (!printed && !regex.test(fileContent)) {
console.log(`Sorry, Noting found`);
printed = true;
}
});
}
function getFilesInDirectory(dir, ext) {
if (!fs.existsSync(dir)) {
console.log(`Specified directory: ${dir} does not exist`);
return;
}
let files = [];
fs.readdirSync(dir).forEach(file => {
const filePath = path.join(dir, file);
const stat = fs.lstatSync(filePath);
if (stat.isDirectory()) {
const nestedFiles = getFilesInDirectory(filePath, ext);
files = files.concat(nestedFiles);
} else {
if (path.extname(file) === ext) {
files.push(filePath);
}
}
});
return files;
}
searchFilesInDirectory(process.argv[2], process.argv[4], process.argv[3]);
If I get what are you trying to achieve. You can do so by slightly changing your function call in the last line.
Change
searchFilesInDirectory(process.argv[2], process.argv[4], process.argv[3]);
to
searchFilesInDirectory(process.cwd(), process.argv[3], process.argv[2]);
Edit
As #Keith said in comments use process.cwd() to get the current working directory instead of __dirname
If you want it to work for both conditions then you need to do a conditional check...
if(process.argv.length === 5){
searchFilesInDirectory(process.argv[2], process.argv[4], process.argv[3]);
}else if(process.argv.length === 4){
searchFilesInDirectory(process.cwd(), process.argv[3], process.argv[2]);
}else{
throw new Error("Not enough arguments provided..");
}

How to Increment Version Number via Gulp Task?

I would like to replace a string indicating version number in a javascript file (myConstantsFile.js), with another string. So, for example, my version number looks like this: "01.11.15", written like this in myConstantsFile.js with other constants:
.constant('productVersion', '1.11.15');
Right now, my task looks like this:
gulp.task('increment-version', function(){
gulp.src(['./somedir/myConstantsFile.js'])
.pipe(replace(/'productVersion', '(.*)'/g, '99.99.99'))
.pipe(gulp.dest('./somedir/'));
});
As you can see, I am using a constant, not running incrementation code, which would look like this:
var numberString = '0.0.1';
var versionParts = numberString.split('.');
var vArray = {
vMajor : versionParts[0],
vMinor : versionParts[1],
vPatch : versionParts[2]
}
vArray.vPatch = parseFloat(vArray.vPatch) + 1;
var periodString = ".";
var newVersionNumberString = vArray.vMajor + periodString +
vArray.vMinor+ periodString +
vArray.vPatch;
I need:
A way to select the current version number via regex via the file.
To know where I can put the logic in the last code block to increment the number and build the new string.
Install gulp-bump
npm install gulp-bump --save-dev
Install yargs
npm install yargs --save-dev
Require gulp-bump
var bump = require('gulp-bump');
Require yargs
var args = require('yargs').argv;
Your bump task
gulp.task('bump', function () {
/// <summary>
/// It bumps revisions
/// Usage:
/// 1. gulp bump : bumps the package.json and bower.json to the next minor revision.
/// i.e. from 0.1.1 to 0.1.2
/// 2. gulp bump --version 1.1.1 : bumps/sets the package.json and bower.json to the
/// specified revision.
/// 3. gulp bump --type major : bumps 1.0.0
/// gulp bump --type minor : bumps 0.1.0
/// gulp bump --type patch : bumps 0.0.2
/// gulp bump --type prerelease : bumps 0.0.1-2
/// </summary>
var type = args.type;
var version = args.version;
var options = {};
if (version) {
options.version = version;
msg += ' to ' + version;
} else {
options.type = type;
msg += ' for a ' + type;
}
return gulp
.src(['Path to your package.json', 'path to your bower.json'])
.pipe(bump(options))
.pipe(gulp.dest('path to your root directory'));
});
VSO Note: I believe a lot of people coming to this thread will be looking exactly for the answer above. The code below is to edit a version number stored somewhere BESIDES the npm/bower package files, such as in angular constants:
gulp.task('increment-version', function(){
//docString is the file from which you will get your constant string
var docString = fs.readFileSync('./someFolder/constants.js', 'utf8');
//The code below gets your semantic v# from docString
var versionNumPattern=/'someTextPreceedingVNumber', '(.*)'/; //This is just a regEx with a capture group for version number
var vNumRexEx = new RegExp(versionNumPattern);
var oldVersionNumber = (vNumRexEx.exec(docString))[1]; //This gets the captured group
//...Split the version number string into elements so you can bump the one you want
var versionParts = oldVersionNumber.split('.');
var vArray = {
vMajor : versionParts[0],
vMinor : versionParts[1],
vPatch : versionParts[2]
};
vArray.vPatch = parseFloat(vArray.vPatch) + 1;
var periodString = ".";
var newVersionNumber = vArray.vMajor + periodString +
vArray.vMinor+ periodString +
vArray.vPatch;
gulp.src(['./someFolder/constants.js'])
.pipe(replace(/'someTextPreceedingVNumber', '(.*)'/g, newVersionNumber))
.pipe(gulp.dest('./someFolder/'));
});
I ommitted some mumbo-jumbo that writes my constant in a pretty string, but that's the gist and it works.
Started looking into gulp since past 5 hours,as I had a task to fix the requirement. So, being a definite noob to gulp I came out with the below code which is without the regex expression. Thanks to #VSO and #Wilmer Saint for a quick start. Might be a tiny change, but this helped me.
gulp.task('version', function(){
var fs = require('fs');
//docString is the file from which you will get your constant string
var docString = fs.readFileSync('app/scripts/version/version.js', 'utf8'); //type of docString i an object here.
var versionParts = docString.split('.');
var vArray = {
vMajor : versionParts[0],
vMinor : versionParts[1],
vPatch : versionParts[2]
};
vArray.vPatch = parseFloat(vArray.vPatch) + 1;
var periodString = ".";
var newVersionNumber = vArray.vMajor + periodString +
vArray.vMinor+ periodString +
vArray.vPatch;
require('fs').writeFileSync('app/scripts/version/version.js', newVersionNumber + "'");
return gulp.src(['app/scripts/version/version.js'])
.pipe(gulp.dest('app/scripts/version/new_version'));//creates version.js file in the directory
});
or the return code could be as below to override the number in version.js file
return gulp.src(['app/scripts/version/version.js'],
{base: './app/scripts/version/version.js'})
.pipe((gulp.dest('./app/scripts/version/version.js')))
My version.js has only below code
versionBuild = '1.0.8'
I used the below in my main function(loads on loading the app)
var versionBuild=parseInt(1000*Math.random());
var random = function(digs){
var rndn;
if(window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1") {
rndn = Math.random();
if(digs != undefined && !isNaN(digs)){
rndn = parseInt(Math.pow(10, digs)*rndn)
}
return rndn;
}
else {
return versionBuild;
}
}
you can use gulp-bump, it's very easy and sweet :)
npm install --save gulp-bump
const bump = require('gulp-bump');
gulp.task('bump', async () => {
gulp.src('./package.json')
.pipe(bump({key: "version"}))
.pipe(gulp.dest('./'));
});
Note: use async before a function. it's a requirement.
gulp.task('bump', function() {
var vers = JSON.parse(fs.readFileSync(__dirname + '/package.json')).version.split('.');
var second = parseInt(vers[1]);
var third = parseInt(vers[2]);
var options = { key: 'version' };
if(third == 9 && second != 9) {
third = 0;
options.type = 'minor';
} else if (second == 9 && third == 9) {
second = 0;
options.type = 'major';
}
gulp.src(__dirname + '/package.json')
.pipe(bump(options))
.pipe(gulp.dest('./'));
});

Passing argument array to CasperJS from node.js

I am using CasperJS to test a website. A part of the test is resource check.
What I want to do:
Pass array or object array to CasperJS and iterate through them.
The first step is one array then object array. both have same issue.
Node.js code:
require('child_process').exec('/usr/local/bin/casperjs script.js [url,regex]' , function(err, stdout, stderr) {
err && console.log(err);
stderr && console.log(stderr.toString());
stdout && console.log(stdout.toString());
})
Casperjs script:
var casper = require('casper').create(),
a = casper.cli.args[0],
// we need something here to string to js array
w=a[0],
r=a[1];
casper.start(w, function() {
if (this.resourceExists(r)) {
console.log("PASS\t" +r+ "\t"+ w);
} else {
console.log("FAIL\t" +r+ "\t"+ w);
}
});
casper.run();
The problem is CasperJS takes args as string.
When you call it like that:
'/usr/local/bin/casperjs script.js "[\''+yourURL+'\',\''+yourRegex+'\']"'
You could simply use
a = JSON.parse(casper.cli.args[0]),
w = a[0],
r = new RegExp(a[1]);
If casper.cli.args[0] is actually JSON, then it can be parsed as such. resourceExists() takes regular expressions only as RegExp objects.
A better way if the data that you pass gets too long, then you should write the data to a temporary file with node.js' fs module and read it with PhantomJS' fs module, parsing it along the way.
Better approach to problem:
https://groups.google.com/forum/#!topic/casperjs/bhA81OyHA7s
Passing variables as options. Works like a charm
My Final nodejs:
var exec = require('child_process'),
array = [
{url:"",regex:""}
];
for (var i = 0; i < array.length; i++) {
var url = array[i]["url"];
var regex = array[i]["regex"];
exec.exec('/usr/local/bin/casperjs casper2.js --url="'+url+'" --regex="'+regex+'" ' , function(err, stdout, stderr) {
err && console.log(err);
stderr && console.log(stderr.toString());
stdout && console.log(stdout.toString());
});
};
In CasperJS
w=casper.cli.get("url"),
reg= casper.cli.get("regex"),
rpart = reg.split("/"),
r=new RegExp(rpart[1],rpart[2]);
casper.start(w, function() {
if (this.resourceExists(r)) {
console.log("PASS\t" +r+ "\t"+ w);
} else {
console.log("FAIL\t" +r+ "\t"+ w);
}
});
casper.run();

How to return array from module in NodeJS

I'm writing a program that should filter and print the files of the given directory from the command line based on the file type.
mymodule.js :
var fs = require('fs');
var result=[];
exports.name = function() {
fs.readdir(process.argv[2], function (err, list) {
for(var file in list){
if(list[file].indexOf('.'+process.argv[3]) !== -1){
result.push(list[file]);
}
}
});
};
And in my actual file
index.js:
var mymodule = require('./mymodule')
console.log(mymodule.name());
When the run the command
> node index.js SmashingNodeJS js //In SmashingNodeJS folder print all the .js files
The console.log is printing undefined, please let me know what I am doing wrong here and how to return/bind the content to exports.
I fixed it by following Bergi's comment above.
mymodule.js :
var fs = require('fs');
exports.name = function(print) { // Added print callback here as param
var result = [];
fs.readdir(process.argv[2], function (err, list) {
for (var file=0; file<list.length; file++) {
if (list[file].indexOf('.'+process.argv[3]) !== -1) {
result.push(list[file]);
}
}
print(result); // Calling back instead of return
});
};
And in index.js file:
var mymodule = require('./mymodule')
mymodule.name(console.log); // pass the console.log function as callback

Uglify JS - compressing unused variables

Uglify has a "compression" option that can remove unused variables...
However, if I stored some functions in an object like this....
helpers = {
doSomething: function () { ... },
doSomethingElese: function () { ... }
}
... is there a way to remove helpers.doSomething() if it's never accessed?
Guess I want to give the compressor permission to change my object.
Any ideas if it's possible? Or any other tools that can help?
Using a static analyzer like Uglify2 or Esprima to accomplish this task is somewhat nontrivial, because there are lots of situations that will call a function that are difficult to determine. To show the complexity, there's this website:
http://sevinf.github.io/blog/2012/09/29/esprima-tutorial/
Which attempts to at least identify unused functions. However the code as provided on that website will not work against your example because it is looking for FunctionDeclarations and not FunctionExpressions. It is also looking for CallExpression's as Identifiers while ignoring CallExpression's that are MemberExpression's as your example uses. There's also a problem of scope there, it doesn't take into account functions in different scopes with the same name - perfectly legal Javascript, but you lose fidelity using that code as it'll miss some unused functions thinking they were called when they were not.
To handle the scope problem, you might be able to employ ESTR (https://github.com/clausreinke/estr), to help figure out the scope of the variables and from there the unused functions. Then you'll need to use something like escodegen to remove the unused functions.
As a starting point for you I've adapted the code on that website to work for your very specific situation provided, but be forwarned, it will have scope issue.
This is written for Node.js, so you'll need to get esprima with npm to use the example as provided, and of course execute it with node.
var fs = require('fs');
var esprima = require('esprima');
if (process.argv.length < 3) {
console.log('Usage: node ' + process.argv[1] + ' <filename>');
process.exit(1);
}
notifydeadcode = function(data){
function traverse(node, func) {
func(node);
for (var key in node) {
if (node.hasOwnProperty(key)) {
var child = node[key];
if (typeof child === 'object' && child !== null) {
if (Array.isArray(child)) {
child.forEach(function(node) {
traverse(node, func);
});
} else {
traverse(child, func);
}
}
}
}
}
function analyzeCode(code) {
var ast = esprima.parse(code);
var functionsStats = {};
var addStatsEntry = function(funcName) {
if (!functionsStats[funcName]) {
functionsStats[funcName] = {calls: 0, declarations:0};
}
};
var pnode = null;
traverse(ast, function(node) {
if (node.type === 'FunctionExpression') {
if(pnode.type == 'Identifier'){
var expr = pnode.name;
addStatsEntry(expr);
functionsStats[expr].declarations++;
}
} else if (node.type === 'FunctionDeclaration') {
addStatsEntry(node.id.name);
functionsStats[node.id.name].declarations++;
} else if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
addStatsEntry(node.callee.name);
functionsStats[node.callee.name].calls++;
}else if (node.type === 'CallExpression' && node.callee.type === 'MemberExpression'){
var lexpr = node.callee.property.name;
addStatsEntry(lexpr);
functionsStats[lexpr].calls++;
}
pnode = node;
});
processResults(functionsStats);
}
function processResults(results) {
//console.log(JSON.stringify(results));
for (var name in results) {
if (results.hasOwnProperty(name)) {
var stats = results[name];
if (stats.declarations === 0) {
console.log('Function', name, 'undeclared');
} else if (stats.declarations > 1) {
console.log('Function', name, 'decalred multiple times');
} else if (stats.calls === 0) {
console.log('Function', name, 'declared but not called');
}
}
}
}
analyzeCode(data);
}
// Read the file and print its contents.
var filename = process.argv[2];
fs.readFile(filename, 'utf8', function(err, data) {
if (err) throw err;
console.log('OK: ' + filename);
notifydeadcode(data);
});
So if you plop that in a file like deadfunc.js and then call it like so:
node deadfunc.js test.js
where test.js contains:
helpers = {
doSomething:function(){ },
doSomethingElse:function(){ }
};
helpers.doSomethingElse();
You will get the output:
OK: test.js
Function doSomething declared but not called
One last thing to note: attempting to find unused variables and functions might be a rabbit hole because you have situations like eval and Functions created from strings. You also have to think about apply and call etc, etc. Which is why, I assume, we don't have this capability in the static analyzers today.

Categories

Resources