npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

groundskeeper

v0.1.12

Published

Pragmas and console statements remover

Downloads

140

Readme

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

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