@@ -8,6 +8,12 @@ var yargs = require('./yargs');
88
99var defaultReporterName = 'angularity-jshint-reporter' ;
1010
11+ /**
12+ * Cache resolved reporter object or stream
13+ * @type {function }
14+ */
15+ var resolvedReporter ;
16+
1117/**
1218 * Dynamically load a JsHint reporter
1319 *
@@ -22,46 +28,57 @@ var defaultReporterName = 'angularity-jshint-reporter';
2228 * - The absolute path to a node package
2329 * @return {function } The `required`ed jshint report, ready to be piped a gulp stream
2430 */
25- var jsHintReporter ;
2631function getJsHintReporter ( reporterName ) {
27- if ( typeof reporterName !== 'string' ) {
28- throw 'Get JsHint Reporter: Reporter name is unspecified' ;
29- }
30- if ( jsHintReporter ) {
31- return jsHintReporter ; //cached copy
32- }
3332
34- if ( reporterName === defaultReporterName ) {
35- jsHintReporter = require ( defaultReporterName ) ;
36- }
37- else {
38- var reporterPath = ( path . dirname ( reporterName ) === '.' ) ?
39- path . resolve ( 'node_modules' , reporterName ) :
40- reporterName ;
41- try {
42- jsHintReporter = require ( reporterPath ) ;
43- if ( typeof jsHintReporter === 'string' ) {
44- //In JsHint convention, the `index.js` file exports a string which jshint itself should require
45- //e.g. `module.exports = require('path').join(__dirname, 'reporter.js');`
46- jsHintReporter = gulpJshint . reporter ( require ( jsHintReporter ) ) ;
47- }
33+ // establish a cached copy
34+ if ( ! resolvedReporter ) {
35+ if ( typeof reporterName !== 'string' ) {
36+ throw 'Get JsHint Reporter: Reporter name is unspecified' ;
4837 }
49- catch ( ex ) {
50- throw 'Get JsHint Reporter: Attempt to require reporter from path ' + reporterPath + ' with no success.' ;
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 ) {
54+ throw 'Get JsHint Reporter: Attempt to require reporter from path ' + reporterPath + ' with no success.' ;
55+ }
5156 }
5257 }
5358
54- return jsHintReporter ;
59+ // return cached copy
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+ }
5572}
5673
5774var yargsOptionDefiniton = {
5875 key : 'reporter' ,
5976 value : {
60- describe : 'Specify a custom JsHint reporter to use. ' +
61- 'Either a locally npm installed module, or an asolute path to one.' ,
77+ describe : 'Specify a custom JsHint reporter to use. Either a locally npm installed module, or the absolute path ' +
78+ 'to one.' ,
6279 alias : [ 'r' ] ,
6380 default : defaultReporterName ,
64- string : true ,
81+ string : true
6582 }
6683} ;
6784var checkJsHintReporter = yargs . createCheck ( )
@@ -77,15 +94,15 @@ var checkJsHintReporter = yargs.createCheck()
7794 getJsHintReporter ( value ) ;
7895 }
7996 catch ( ex ) {
80- return 'Illegal value for "reporter"\n' + ex ;
97+ return 'Illegal value for "reporter"\n' + ex ;
8198 }
82- } ,
99+ }
83100 } )
84101 . commit ( ) ;
85102
86103module . exports = {
87- get : getJsHintReporter ,
88- yargsCheck : checkJsHintReporter ,
89- yargsOption : yargsOptionDefiniton ,
104+ get : getJsHintReporter ,
105+ yargsCheck : checkJsHintReporter ,
106+ yargsOption : yargsOptionDefiniton ,
90107 defaultReporterName : defaultReporterName
91108} ;
0 commit comments