1

I search a large js tree frequently for certain Tokens. Consider you were looking for all of the '.prototype' tokens in your js files. I would do something like this:

find . -name "*.js" |xargs grep "\.prototype"

Of coarse this produces a boat-load of hits on minified vendor libs. Obscuring the real results I am after. i.e:

this.animate(b,a,c,d)}}),p.speed=function(a,b,c)
{var d=a&&typeof a=="object"?p.extend({},a{com
plete:c||!c&&b||p.isFunction(a)&&a,duration:a,eas
ing:c&&b||b&&!p.isFunction(b)&&b};d.duration=p.fx.of
f?0:typeof d.duration=="number"?d.duration:d.durat
ion in p.fx.speedsp.fx.speeds[d.duration]:p.fx.speeds._defa
ult;if(d.queue==null||d.queue===!0)d.queue="fx";return 

results like that Ad nauseam.

So how can I detect and 'skip' the minified libs?

Note: I usually store both the minified and non-minified versions of the vendor libs so I will get the hits in the vendor libs that I am looking for.

Is there some utility out there that does it?

Jeff Sheffield
  • 5,768
  • 3
  • 25
  • 32

5 Answers5

2

The not so perfect Solution: I just wrote a little bash script to do it. This script leaves off some corner cases for sure. However it works in my environment, and I have quite a few minified vendor libs that I don't want to trip over.

#!/bin/bash

my_magic_number=300

for fname in `find . -name "*.js"`; do
   # Test the file to see if the last line is longer than... 300
   character_count=$(tail -1 $fname |wc -m)
   if [ $character_count -gt $my_magic_number ]; then
      #echo " --> compressed: $fname"       
      :
   else
      grep --with-filename $1 $fname
   fi
done
  • Put that in a text file jsgrep
  • chmod 755 the file
  • Drop it in your path.

Wala:

$ cd /home/user/workspace/path/with/js/
$ jsgrep "\.prototype"

Now gets you some sensible output:

./vendor/json2.js:        String.prototype.toJSON      =
./vendor/json2.js:            Number.prototype.toJSON  =
./vendor/bootstrap.js:  Modal.prototype.escape = function () {
./vendor/bootstrap.js:  Modal.prototype.removeBackdrop = function () {
./vendor/bootstrap.js:  Modal.prototype.backdrop = function (callback) {
./vendor/backbone-1.0.0.js:    if (protoProps) _.extend(child.prototype, protoProps);
./vendor/backbone-1.0.0.js:    child.__super__ = parent.prototype;
./vendor/backbone.localStorage.js:_.extend(Backbone.LocalStorage.prototype, {
./vendor/underscore.js:  var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
Jeff Sheffield
  • 5,768
  • 3
  • 25
  • 32
1
find -name '*.js' | xargs wc -cl | head -n -1 | awk '{ avg = int($2 / ($1+1)) ; if ( avg < 100 ) { print $3; } }'

Fresh from the console.

  • xargs is over ten times faster than find -exec, because of the latter starting wc for every line
  • head -n -1 cuts summary line
  • $1+1 is against division by zero
0

Is there a utility to do this?

Maybe.

If the minified versions are conveniently named *.min.js (as is typical) then you can simply find only those files.

I think checking for spaces in the file is even more brittle given that it is easily broken if the file contains a string such as " " - or even just an error message or other output string.

verdammelt
  • 922
  • 10
  • 22
  • alot of the libs in my tree are named properly and a nifty find command will do the trick. It is not enough. I concur that your checking for spaces is brittle. However I do think an fairly accurate algorithm could be written to solve the issue, and I plan on doing so if I cant find the solution in the wild. However I did find a solution that gets me real close here. http://stackoverflow.com/questions/1341467/unix-find-for-finding-file-names-not-ending-in-specific-extensions $ find . -name "*.js" -not -name "*.min.js" -not -name "*min.js" -not -name "*built*.js" | xargs grep "\.prototype" – Jeff Sheffield Jan 13 '14 at 02:55
  • Yes, that question will help you to find files `*.js` but not `*.min.js`. Sorry I should have put that into my answer - assumed more familiarity with `find`. – verdammelt Jan 13 '14 at 03:01
  • I just modified my question to be a bit more clear: I include the fact that I was searching for tokens in the js files, and getting false positives in the minified versions. – Jeff Sheffield Jan 13 '14 at 03:06
0

So this gets me real close to a solution:

$ find . -name "*.js" -not -name "*.min.js" -not -name "*min.js" -not -name "*built*.js"| xargs grep "\.prototype" |less

I still envision a solution where you type:

<findjstok> ".prototype"

findjstok is the magic I am looking for in the wild.

Jeff Sheffield
  • 5,768
  • 3
  • 25
  • 32
0

I know it's an old question but the simplest solution straight out of the find man page is

find . \! -name "*.min.js" -print
acharlop
  • 309
  • 4
  • 12