@@ -9,10 +9,10 @@ var yargs = require('./yargs');
99var defaultReporterName = 'angularity-jshint-reporter' ;
1010
1111/**
12- * Cached copy of the reporter
12+ * Cache resolved reporter object or stream
1313 * @type {function }
1414 */
15- var jsHintReporter ;
15+ var resolvedReporter ;
1616
1717/**
1818 * Dynamically load a JsHint reporter
@@ -29,33 +29,46 @@ var jsHintReporter;
2929 * @return {function } The `required`ed jshint report, ready to be piped a gulp stream
3030 */
3131function getJsHintReporter ( reporterName ) {
32- if ( typeof reporterName !== 'string' ) {
33- throw 'Get JsHint Reporter: Reporter name is unspecified' ;
34- }
3532
3633 // establish a cached copy
37- if ( ! jsHintReporter ) {
38- if ( reporterName === defaultReporterName ) {
39- jsHintReporter = require ( defaultReporterName ) ;
40- } else {
41- var reporterPath = ( path . dirname ( reporterName ) === '.' ) ?
42- path . resolve ( 'node_modules' , reporterName ) :
43- reporterName ;
44- try {
45- jsHintReporter = require ( reporterPath ) ;
46- if ( typeof jsHintReporter === 'string' ) {
47- //In JsHint convention, the `index.js` file exports a string which jshint itself should require
48- //e.g. `module.exports = require('path').join(__dirname, 'reporter.js');`
49- jsHintReporter = gulpJshint . reporter ( require ( jsHintReporter ) ) ;
50- }
51- } catch ( ex ) {
34+ if ( ! resolvedReporter ) {
35+ if ( typeof reporterName !== 'string' ) {
36+ throw 'Get JsHint Reporter: Reporter name is unspecified' ;
37+ }
38+ else {
39+ // first check angularity installed modules, then check project locally installed modules
40+ [ path . resolve ( 'node_modules' ) , null ] . forEach ( function ( base ) {
41+ var reporterPath = base ? path . join ( base , reporterName ) : reporterName ;
42+ try {
43+ // In JsHint convention, the `index.js` file exports a string which jshint itself should require
44+ // e.g. `module.exports = require('path').join(__dirname, 'reporter.js');`
45+ // this is the indirect case.
46+ // However in some cases it may be the reporter itself
47+ var indirect = require ( reporterPath ) ;
48+ resolvedReporter = ( typeof indirect === 'string' ) ? require ( indirect ) : indirect ;
49+ } catch ( ex ) {
50+ /* do nothing */
51+ }
52+ } ) ;
53+ if ( ! resolvedReporter ) {
5254 throw 'Get JsHint Reporter: Attempt to require reporter from path ' + reporterPath + ' with no success.' ;
5355 }
5456 }
5557 }
5658
5759 // return cached copy
58- return jsHintReporter ;
60+ // closure that returns a stream
61+ if ( typeof resolvedReporter === 'function' ) {
62+ return resolvedReporter ( ) ;
63+ }
64+ // jshint plugin object with reporter field
65+ else if ( ! ! ( resolvedReporter ) && ( typeof resolvedReporter . reporter === 'function' ) ) {
66+ return gulpJshint . reporter ( resolvedReporter ) ;
67+ }
68+ // unsupported
69+ else {
70+ throw 'Get JsHint Reporter: Given reporter is badly formed' ;
71+ }
5972}
6073
6174var yargsOptionDefiniton = {
0 commit comments