Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use growl #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ module.exports = gobble( 'src/js' )
.observeIf( gobble.env() !== 'production', 'eslint', {...});
```

Use the `growl` option to send a notification when the linting is done:

```js
```

See [the node-growl documentation](https://github.com/tj/node-growl) to know the
requirements for this node module.

## License

MIT. Copyright 2014 Rich Harris
MIT. Copyright 2014 Rich Harris
114 changes: 24 additions & 90 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,106 +1,40 @@
var path = require( 'path' );
var chalk = require( 'chalk' );
var linter = require( 'eslint' ).linter;
var sander = require( 'sander' );
var promiseMapSeries = require( 'promise-map-series' );
var findup = require( 'findup-sync' );
var sorcery = require( 'sorcery' );
var logSyntaxError = require( 'log-syntax-error' );

var eslintrc = findup( '.eslintrc' );
var defaultOptions;

if ( eslintrc ) {
try {
defaultOptions = JSON.parse( require( 'fs' ).readFileSync( eslintrc ) );
} catch ( err ) {
throw new Error( 'Could not parse .eslintrc file. It must be valid JSON' );
}
}
var Linter = require( 'eslint' ).CLIEngine;

module.exports = function eslint ( inputdir, options ) {
var log = this.log;

var reportOnly = options.reportOnly;
var reporter = options.reporter || defaultReporter;
var reporter = options.reporter;
var useGrowl = options.growl;

delete options.reporter;
delete options.reportOnly;
delete options.growl;

if ( !Object.keys( options ).length ) {
options = defaultOptions;
}

var reports = [];

return sander.lsr( inputdir ).then( function ( files ) {
return promiseMapSeries( files.filter( isJs ), function ( file ) {
var filename = path.join( inputdir, file );

return sander.readFile( filename )
.then( String )
.then( function ( code ) {
log( 'linting ' + file );
var messages = linter.verify( code, options, filename );

if ( messages.length ) {
reports.push({
filename: filename,
messages: messages.sort( bySeverity )
});
}
});
});
}).then( function () {
if ( reports.length ) {
reporter( reports );

if ( !reportOnly ) {
throw new Error( 'Linting failed' );
}
}
});
};

function isJs ( file ) {
return /\.js$/.test( file );
}

function bySeverity ( a, b ) {
return b.severity - a.severity;
}

function defaultReporter ( reports ) {
reports.forEach( function ( report ) {
var numErrors = report.messages.length;
var chain = sorcery.loadSync( report.filename );
// This is necessary for eslint to lint files in .gobble directories
options.dotfiles = true;

var source = sander.readFileSync( report.filename ).toString();
// This is necessary for eslint to lint files in .gobble directories
options.dotfiles = true;

console.log( '===\n\n%s %s in %s', numErrors, numErrors === 1 ? 'error' : 'errors', report.filename );
var linter = new Linter(options);
var reports = linter.executeOnFiles([ inputdir ]);

report.messages.forEach( function ( message ) {
var block, originalLocation, originalSource;
if ( reports.errorCount || reports.warningCount ) {
var formatter = Linter.getFormatter(reporter);
console.log(formatter( reports.results ));

console.log( '\n---\n' );
if ( !reportOnly && reports.errorCount ) {
throw new Error( 'Linting failed' );
}
}

console.log( message.message );
block = logSyntaxError( source, message.line, message.column );
console.log( block );
if (useGrowl) {
var notification = 'Result: ' + reports.errorCount + ' errors, ' +
reports.warningCount + ' warnings';

if ( chain ) {
originalLocation = chain.trace( message.line, message.column );
require('growl')(notification, { title: 'ESLint' });
}

if ( originalLocation ) {
originalSource = sander.readFileSync( originalLocation.source ).toString();

console.log( '\noriginal source: ' + originalLocation.source );
block = logSyntaxError( originalSource, originalLocation.line, originalLocation.column );
console.log( block );
}
}
});
});

console.log( '\n\n' );
}
return Promise.resolve();
};
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@
"license": "MIT",
"repository": "https://github.com/gobblejs/gobble-eslint",
"dependencies": {
"chalk": "^1.0.0",
"eslint": "^0.18.0",
"findup-sync": "^0.2.1",
"log-syntax-error": "^0.1.0",
"promise-map-series": "^0.2.1",
"sander": "^0.2.2",
"sorcery": "^0.3.4"
"eslint": "^2.0",
"growl": "^1.9.2"
},
"keywords": [
"gobble",
Expand Down