Skip to content
This repository has been archived by the owner on Oct 25, 2019. It is now read-only.

Commit

Permalink
Final version before tests
Browse files Browse the repository at this point in the history
    * Remove Namespaced utilities
    * Replace console with a string
    * Update README.md
    * Add travis support
  • Loading branch information
Couto committed Nov 14, 2012
1 parent 06af4cd commit 1f0a05d
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 248 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.DS_Storedocs
.DS_Store
docs
node_modules
Expand Down
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- 0.8
notifications:
email: false
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,4 @@ lib-cov:
@rm -rf coverage/*
@jscoverage lib coverage

version:
@sed -i '' 's/$(OLD_VERSION)/$(NEW_VERSION)/g' $$i $(shell ls $(JSFILES)) $(shell ls $(JSONFILES))

.PHONY: lib-cov test test-cov
113 changes: 72 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
groundskeeper
=============

__Current Version:__ 0.1.0

This is a small utility to remove forgotten `consoles` 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.
Expand All @@ -18,79 +20,108 @@ Instalation
The easiest way is to use [npm](https://github.com/isaacs/npm)

```shell
npm install groundskeeper -g
npm install groundskeeper -g
```

Notes
Usage
-----

* If you minify your code (and you should!). Please use this before the minification process since it will __unminify__ your code on the process.
Pretty simple... dirty file goes in, clean file goes out:

```shell
groundskeeper < dirty.js > clean.js
```

Usage
-----
```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.isString(), 'utf8');

```shell
groundskeeper [options] -i <input folder> -o <output folder>
```

Streams are supported by groundskeeper, but not by [esprima](http://code.google.com/p/esprima/issues/detail?id=92&q=Enhancement), if you really want to use Streams, make sure that your files are below 40960 bytes... still the example:

```javascript
var groundskeeper = require('groundskeeper').groundskeeper(options);
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);
```

I'm not really sure if the second way is useful, but it exists just in case.

Altough several options can be given, example:
By default `groundskeeper` removes all `console`, `debugger;` and pragmas that it founds, the following options allow you to specify what you want to __keep__:

- To remove specific blocks of code, just insert comments specifying which block do you want to remove. Those comments must resemble the xml/html tags. By default all blocks named `validation` and `development` are removed.
in Javascript:

```javascript
function merge(target, source) {
var k;

//<validation>
if (!is.object(target) || !is.object(source)) {
throw new Error('Argument given must be of type Object');
{
console: true, // Keep console logs
debugger: true // Keep debugger; statements
pragmas: ['validation', 'development'], // Keep pragmas with the following identifiers
namespace: 'App.logger' // Besides console also remove functions in the given namespace,
replace: '0' // For the ones who don't know how to write Javascript...
}
//</validation>
```

for (k in source) {
if (source.hasOwnProperty(k) && !target[k]) {
target[k] = source[k];
}
}
in Shell:

return target;
}
```shell
-p, --pragmas <names> comma-delimited <names> to keep, everything else is removed
-n, --namespace <string> If you use your own logger utility, specify here, e.g.: `App.logger`
-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
```

To remove the following block just type:
If you use your own logger utility, you can also remove those by specifying a namespace.
Assuming your utility is `App.logger.log('yeyy')`

```shell
groundskeeper -p validation -i file.js -o output.js
groundskeeper -n App.logger.log < dirty.js > clean.js
```

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

```shell
groundskeeper -c false -i file.js -o output.js
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:

Options
-------
```javascript
if (condition) console.log("condition true");
else console.log("condition false")

// yeah... most cases happen when people don't use brackets...
```
-h, --help output usage information
-V, --version output the version number
-i, --input <path> input folder, defaults to current folder
-o, --output <path> output folder, defaults to ./clean
-c, --console [boolean] enable the removal of console statements
-a, --ast [boolean] disable parse through ast, fallbacks to regex but keeps
newlines
-p, --pragmas <names> comma-delimited <names> to remove, defaults to validation, development
-v, --verbose [boolean] outputs current state of procedure
--list display list of console methods that will be removed

After removing the `console` statements the previous code becomes:

```javascript
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.


Tests
-----
Expand Down
64 changes: 15 additions & 49 deletions bin/groundskeeper
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
* @author Luis Couto
* @organization 15minuteslate.net
* @contact couto@15minuteslate.net
* @version 0.0.6
* @version 0.1.0
* @requires path, fs, exec, commander.js, colors, log.js
* @license http://couto.mit-license.org MIT
*
*
* @example
* $ groundskeeper -d true -n App.logger -p validation,development -i ./src -o ./dist
* $ groundskeeper -r 0 -dc true -n App.logger -p validation,development
*/

// Dependencies
Expand All @@ -25,67 +25,33 @@ var fs = require('fs'),
colors = require('colors'),
groundskeeper = require('../index'),
// Variables
options = {};
buf = '';

// Options
// .option(-shortcut, --name, "description", functionToParse, defaultValue)
program
.version("0.1.0")
.usage(' -i <path> -o <path> -c [false] -p validation,test,development')
.option('-i, --input <path>', 'input folder, ' + 'folder or glob to read files' + ' defaults to stdin'.grey)
.option('-o, --output <path>', 'output folder, ' + 'folder/file where cleaned files will be placed ' + 'defaults to stdout'.grey)
.usage(' -c [false] -p validation,test,development')
.option('-p, --pragmas <names>', 'comma-delimited <names> to keep, everything else is removed')
.option('-n, --namespace <string>', 'If you use your own logger utility, specify here, e.g.: `App.logger` ' + 'defaults to `console`'.grey)
.option('-n, --namespace <string>', 'If you use your own logger utility, specify here, e.g.: `App.logger`')
.option('-d, --debugger [boolean]', 'If true, it will keep `debbuger;` statements', false)
.option('-c, --console [boolean]', 'If true, it keeps `console` statements', false)
.option('-v, --verbose [boolean]', 'outputs current state of procedure', false)
.option('--list', 'display list of console methods that will be removed');
.option('-r, --replace <string>', 'If given it will replace every console with the given value')
.option('-v, --verbose [boolean]', 'outputs current state of procedure', false);

program.name = 'groundskeeper';

// --list
program.on('list', function () {
console.log();
console.log(' assert '.bold + ' console.assert(undefined === true)'.grey);
console.log(' clear '.bold + ' console.clear()'.grey);
console.log(' count '.bold + ' console.count("hard loop")'.grey);
console.log(' debug '.bold + ' console.debug(String)'.grey);
console.log(' dir '.bold + ' console.dir(Object)'.grey);
console.log(' dirxml '.bold + ' console.dirxml(document.body)'.grey);
console.log(' error '.bold + ' console.error("Argument not given")'.grey);
console.log(' group '.bold + ' console.group("groupName")'.grey);
console.log(' groupCollapsed '.bold + ' console.groupCollapsed("collapsedGroupName")'.grey);
console.log(' groupEnd '.bold + ' console.groupEnd("groupName")'.grey);
console.log(' info '.bold + ' console.info(variableName)'.grey);
console.log(' log '.bold + ' console.log()'.grey);
console.log(' markTimeline '.bold + ' console.markTimeline("string")'.grey);
console.log(' profile '.bold + ' console.profile("profileName")'.grey);
console.log(' profileEnd '.bold + ' console.profileEnd("profileName")'.grey);
console.log(' time '.bold + ' console.time("forEach loop")'.grey);
console.log(' timeEnd '.bold + ' console.timeEnd("forEach loop")'.grey);
console.log(' trace '.bold + ' console.trace()'.grey);
console.log(' warn '.bold + ' console.warn("exception found")'.grey);
console.log();
process.exit();
});

program.parse(process.argv);

// --pragmas
if (program.pragmas) {
program.pragmas = [].concat(program.pragmas.split(/ *, */));
console.log(program.pragmas)
}

var input;

if (program.input) {
console.log(program.input)
input = fs.createReadStream(program.input);
} else {
input = process.stdin;
program.pragmas = program.pragmas.split(/ *, */);
}

var cleaner = groundskeeper(program);
input.setEncoding('utf8');
input.pipe(cleaner).pipe(process.stdout);
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (chunk) { buf += chunk; });
process.stdin.on('end', function () {
cleaner = groundskeeper(program);
cleaner.write(buf);
process.stdout.write(cleaner.toString());
}).resume();
Loading

0 comments on commit 1f0a05d

Please sign in to comment.