GithubHelp home page GithubHelp logo

web5design / groundskeeper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from couto/groundskeeper

0.0 3.0 0.0 313 KB

node.js package to remove forgotten console statements and specific blocks of code from you Javascript files

groundskeeper's Introduction

groundskeeper

Current Version: 0.1.5

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.

Requirements

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

Instalation

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) 2013 Luís Couto Licensed under the MIT License

groundskeeper's People

Contributors

couto avatar jfhbrook avatar

Watchers

JT5D avatar James Cloos avatar  avatar

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.