Git Product home page Git Product logo

groundskeeper's Introduction

groundskeeper

Current Version: 0.1.12

Build Status Dependencies Status

This is a small utility to remove forgotten console, debugger and specific blocks of code from Javascript files.

It just happens that I forget a lot to remove console statements when moving code to production... at the same time I like to do a lot of validations while in development enviroment, validations that are not really needed when in production mode.

This tool is exactly that tool that removes all those useless stuff.

Note: if you have any problems, take a look at the dev branch, but remember that branch is like pretty much beta.

Notice

If you're using UglifyJS2 then you most likely don't need this package at all. You can just use the drop_debugger and drop_console to achieve the same effect.

If you're using the pragmas function, you might achieve the same effect using conditional compilation.

On the other hand if you don't use UglifyJS2 then go ahead and keep reading :)

Requirements

  • nodejs
  • npm - If you're using a recent version of nodejs it should be already installed

Installation

The easiest way is to use npm

npm install groundskeeper -g

Usage

Pretty simple... dirty file goes in, clean file goes out:

in shell:

groundskeeper < dirty.js > clean.js

in javascript:

var fs = require('fs'),
    groundskeeper = require('groundskeeper'),
    file = fs.readFileSync('dirtyFile.js', 'utf8'),
    cleaner = groundskeeper(options);

cleaner.write(file);
fs.writeFileSync('cleanFile.js', cleaner.toString(), 'utf8');

Streams are supported by groundskeeper, but not by esprima, if you really want to use Streams, make sure that your files are below 40960 bytes, still... the example:

var fs = require('fs'),
    groundskeeper = require('groundskeeper'),
    dirty = fs.createReadStream('dirty.js'),
    clean = fs.createWriteStream('clean.js'),
    cleaner = groundskeeper(options),


dirty.setEncoding('utf8');
dirty.pipe(cleaner).pipe(clean);

By default groundskeeper removes all console, debugger; and pragmas that it founds, the following options allow you to specify what you want to keep:

in Javascript:

{
    console: true,                          // Keep console logs
    debugger: true                          // Keep debugger; statements
    pragmas: ['validation', 'development'], // Keep pragmas with the following identifiers
    namespace: ['App.logger', 'App.bucket'] // Besides console also remove function calls in the given namespace,
    replace: '0'                            // For the ones who don't know how to write Javascript...
}

in Shell:

-p, --pragmas <names>     comma-delimited <names> to keep, everything else is removed
-n, --namespace <names>   comma-delimited <names> to remove, e.g.: `App.logger,App.bucket`
-d, --debugger [boolean]  If true, it will keep `debbuger;` statements
-c, --console [boolean]   If true, it keeps `console` statements
-r, --replace <string>    If given it will replace every console with the given value

If you use your own logger utility, you can also remove those by specifying a namespace. Assuming your utility is App.logger.log('yeyy')

groundskeeper -n App.logger.log < dirty.js > clean.js

If you have multiple functions (warn, count...) in that namespace you can specify App.logger only to remove them all:

groundskeeper -n App.logger < dirty.js > clean.js

Note: In certain cases, you can't remove the console entirely, a pretty tipical case of that is:

if (condition) console.log("condition true");
else console.log("condition false")

// yeah... most cases happen when people don't use brackets...

After removing the console statements the previous code becomes:

if (condition)
else

... which is illegal.

That's when you should use the replace option by specifying a string, where the code becomes:

// assuming 'replace' = '0'
if (condition) '0'
else '0'

... which is harmless, not pretty, but harmless.

Pragmas

If you're wondering how to remove entire blocks of code, you can do that by using comments.

var clone = function (arr) {

    //<validation>
    if (Object.prototype.toString.call(arr) !== '[object Array]') {
        throw new Error('Invalid argument given');
    }
    //</validation>

    return arr.map(function (val) {});
};

Notice those comments? They specify a block code of validation, you can specify whatever name you wish, as long as you respect the format.

Tests

Tests are ran using mocha and jscoverage you can install mocha with npm install, but you'll need to clone and install jscoverage from this repository

To issue the tests, take a look at the Makefile, but in short, it's just a matter of doing:

make test

If you want to see the code coverage, just write:

make lib-cov && make test-cov

TODO

  • Finish tests

License

Copyright (c) 2014 Luís Couto Licensed under the ISC License

groundskeeper's People

Contributors

betaorbust avatar couto avatar jfhbrook avatar matthewrobb avatar mortonfox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

groundskeeper's Issues

Mismatched pragmas cause unpredictable output

Issue

Currently, groundskeeper just matches on /^[<]([/]([^\s])[>]$/ for any pragma and then does an every-other pass to decide what to strip out without looking at the content of the directives. This means that a malformed directive can cause very unpredictable output. This initially came up when somebody removed the opening tag of a pragma, but forgot to remove the closing one.

Example

Source:

console.log('keep me!');
//<pragma>
console.log('remove me!');
// <oops>
console.log('remove me, too!');
// </pragma>
console.log('keep me!'); 

Output:

console.log('keep me!');
console.log('remove me, too!');

Proposal

Keep track of current open pragmas, and ignore mismatched directives.

TypeError: Cannot read property 'type' of undefined at Groundskeeper.removeNamespace

Working against Groundskeeper 0.1.5 I'm running into a TypeError while Groundskeeper attempts namespace removal.

lgmac-mseeley1:working mseeley$ groundskeeper -n util.log.debug < test.js

/usr/local/lib/node_modules/groundskeeper/lib/groundskeeper.js:183
while ([type.CallExpression, type.AssignmentExpression].indexOf(node.type) ===
                                                                    ^
TypeError: Cannot read property 'type' of undefined
    at Groundskeeper.removeNamespace (/usr/local/lib/node_modules/groundskeeper/lib/groundskeeper.js:183:77)
    at Groundskeeper.clean (/usr/local/lib/node_modules/groundskeeper/lib/groundskeeper.js:116:18)
    at walk (/usr/local/lib/node_modules/groundskeeper/node_modules/falafel/index.js:60:9)
    at module.exports (/usr/local/lib/node_modules/groundskeeper/node_modules/falafel/index.js:57:17)
    at Array.forEach (native)
    at forEach (/usr/local/lib/node_modules/groundskeeper/node_modules/falafel/index.js:8:31)
    at walk (/usr/local/lib/node_modules/groundskeeper/node_modules/falafel/index.js:44:9)
    at module.exports (/usr/local/lib/node_modules/groundskeeper/node_modules/falafel/index.js:51:25)
    at Array.forEach (native)
    at forEach (/usr/local/lib/node_modules/groundskeeper/node_modules/falafel/index.js:8:31)

Where test.js contains:

var dbg = util.log.debug;
dbg('foo');

I've included a replacement character as well via -r '0' which yielded the same result.

Node version: 0.8.14
[email protected]

The console cleaner conflicts with some libraries

I had momentjs in my project and realised that groundskeeper just clears file except for the top comment.

I reduced the file to a small function to reproduce the error and the problem seems to occur because the author has put some console.* in an if-statement as this:

if (moment.suppressDeprecationWarnings === false &&
        typeof console !== 'undefined' && console.warn) {
    console.warn("Deprecation warning: " + msg);
}

Which cascades into the whole file getting cleared.

I made a branch with the failing test. See the console.if-statement.js here and what it actually ends up doing with the file.

I might be able to fix it, but going on holiday tomorrow so it'll take a few weeks before I get back on it.

Sadly, for now, my project will have some dirt in it's production code.

Partial matching console.log

It is skipping or partially matching some console.log

Replaced partially:

console.log("loadImageInCache():::::", index+1, index+1 % 2) ---> :::::", index+1, index+1 % 2);
console.log("external() open()", url, scrollee); ---> open()", url, scrollee);

Didn't match-skipped:

console.log('all duplicates, get more');

namespace options strips out entire files

Unfortunately, if I call groundskeeper with namespace set to "s5.Log" on the following file:

core.Class('boiler.App', {
    include: [],

    construct: function (settings) {
        s5.Log.test = true;
    }
});

it will strip out the entire file, as opposed to just the line.

Probably related, groundskeeper just errors out on this file:

function foo() {

    function bar() {
        s5.Log.test = true;
    };

};

any help is greatly appreciated!

Console removal fails with "Cannot read property 'type' of undefined"

[master//groundskeeper]% ./bin/groundskeeper < foo.js                                                                                                   (ark@xx:~/groundskeeper/)

TypeError: Cannot read property 'type' of undefined
    at Groundskeeper.removeConsole (/home/ark/groundskeeper/lib/groundskeeper.js:140:20)
    at Groundskeeper.<anonymous> (/home/ark/groundskeeper/lib/groundskeeper.js:119:18)
    at walk (/home/ark/groundskeeper/node_modules/falafel/index.js:60:9)
    at /home/ark/groundskeeper/node_modules/falafel/index.js:57:17
    at Array.forEach (native)
    at forEach (/home/ark/groundskeeper/node_modules/falafel/index.js:8:31)
    at walk (/home/ark/groundskeeper/node_modules/falafel/index.js:44:9)
    at /home/ark/groundskeeper/node_modules/falafel/index.js:57:17
    at Array.forEach (native)
    at forEach (/home/ark/groundskeeper/node_modules/falafel/index.js:8:31)

Looks like an ast node is empty? File passes esvalidate.

Add variables to namespace in runtime

Example:

var dbg = util.debug.log;
dbg('example');

In this case, groundskeeper should add dbg to the list of namespaces to be removed automatically.

Support for deep namespaces

I have tried this with the grunt plugin and it seems to remove everything. I suppose it could potentially be an issue with the plugin not the lib, but I'm reporting here as I assume the plugin simply proxies to the lib.

If i have a customer logger solution:

var logger = {
  log : function() {
    console && console.log && console.log.apply(console, arguments);
  }
}

It would be great if I could supply the namespace "logger" and it remove all references to logger, that is, calls to logger.log would be stripped.

Trailing directives break groundskeeper

Currently, groundskeeper just matches on /^[<]([/]([^\s])[>]$/ for any pragma and then does an every-other pass to decide what to strip out. If there's a trailing open directive, everything breaks because of the while(ranges.length) block calling ranges.shift twice.

console.log('keep me!');
if(foo){
    //<pragma>
    console.log('oops!'); 
}

Causes
turns into:

console.log('keep me!');
if(foo){

The fix will be to just change it to while(ranges.length > 1) and ignore the broken pragmas.

Alerts removal

alert removal could also be a possible removable, as Vishal pointed out here.

Allow the removal of personalized statements

as @inf0rmer stated, some people have to use APIs other the console API.

Allow the removal of personalized statements e.g.: myLib.log since many people develop for browsers that don't support the console API.

Maybe allow people to write their own regex's?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.