Skip to content
Advertisement

How do I ‘skip’ minified JavaScript files while searching through a tree of js files?

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?

Advertisement

Answer

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;
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement