@@ -16,7 +16,68 @@ function quote(value) {
1616 return '"' + value + '"' ;
1717}
1818
19+ var defaultReporterName = 'angularity-karma-reporter' ;
1920var filesAppendRegex = / \/ \* + \s * A N G U L A R I T Y _ F I L E _ L I S T \s * \* + \/ / ;
21+ var reportersAppendRegex = / \/ \* + \s * A N G U L A R I T Y _ R E P O R T E R _ L I S T \s * \* + \/ / ;
22+ var pluginsAppendRegex = / \/ \* + \s * A N G U L A R I T Y _ P L U G I N _ L I S T \s * \* + \/ / ;
23+ var karmaReporterMatchNameRegex = / ^ k a r m a - ( .+ ) - r e p o r t e r $ / ;
24+
25+ /**
26+ * Determine the `require` path for a plugin.
27+ *
28+ * @param {string } reporterName Either a package name,
29+ * *OR* an absolute path, for a karma reporter.
30+ * @return {string } In karma config file, `plugins` will be inline `require`d
31+ * using this return value.
32+ */
33+ function getKarmaReporterPluginPath ( reporterName ) {
34+ if ( typeof reporterName !== 'string' ) {
35+ throw 'Get Karma Reporter Plugin Path: Reporter name is unspecified' ;
36+ }
37+
38+ if ( reporterName === defaultReporterName ) {
39+ return defaultReporterName ;
40+ }
41+ else {
42+ var reporterPath = ( path . dirname ( reporterName ) === '.' ) ?
43+ path . resolve ( 'node_modules' , 'karma-' + reporterName + '-reporter' ) :
44+ reporterName ;
45+ try {
46+ require ( reporterPath ) ;
47+ }
48+ catch ( ex ) {
49+ throw 'Get Karma Reporter Plugin Path: Attempt to require reporter from path ' + reporterPath + ' with no success.' ;
50+ }
51+ return reporterPath ;
52+ }
53+ }
54+
55+ /**
56+ * Determine the registered reporter name.
57+ *
58+ * @param {string } reporterName Either the registered reporter name,
59+ * *OR* an absolute path, for a karma reporter.
60+ * @return {string } The registered reporter name for use in
61+ * the `reporters` section of the karma config file.
62+ */
63+ function getKarmaReporterName ( reporterName ) {
64+ if ( typeof reporterName !== 'string' ) {
65+ throw 'Get Karma Reporter Name: Reporter name is unspecified' ;
66+ }
67+
68+ var name ;
69+ if ( path . dirname ( reporterName ) === '.' ) {
70+ name = reporterName ;
71+ }
72+ else {
73+ name = path . basename ( reporterName ) ;
74+ }
75+ var match = karmaReporterMatchNameRegex . exec ( name ) ;
76+ if ( ! ! match && match . length === 2 ) {
77+ name = match [ 1 ] ;
78+ }
79+ return name
80+ }
2081
2182/**
2283 * Create a through2 object stream.
@@ -27,13 +88,15 @@ var filesAppendRegex = /\/\*+\s*ANGULARITY_FILE_LIST\s*\*+\// ;
2788 * The new karma config file is added to the stream,
2889 * All input `*.js` files are filtered out of the stream
2990 *
30- * @param {string } configFileName The project local karma config file to augment
91+ * @param {Array.<string> } [reporters] The name of the karma reporter to use
92+ * @param {string } [configFileName] The project local karma config file to augment
3193 * Defaults to "karma.conf.js"
3294 * @return {stream.Through } The output of this stream is expected to contain
3395 * just one file: the augmented karma config file.
3496 */
35- function karmaCreateConfig ( configFileName ) {
36- configFileName = 'karma.conf.js' ;
97+ function karmaCreateConfig ( reporters , configFileName ) {
98+ reporters = reporters || [ ] ;
99+ configFileName = configFileName || 'karma.conf.js' ;
37100 var files = [ ] ;
38101
39102 function transformFn ( file , encoding , done ) {
@@ -47,13 +110,20 @@ function karmaCreateConfig(configFileName) {
47110 function flushFn ( done ) {
48111 var stream = this ;
49112 var filesAppend = JSON . stringify ( files , null , ' ' ) ;
113+ var reportersAppend = JSON . stringify ( reporters . map ( getKarmaReporterName ) , null , ' ' ) ;
114+ var pluginsAppend = '[\n' + reporters . map ( function ( reporter ) {
115+ return 'require("' + getKarmaReporterPluginPath ( reporter ) + '")' ;
116+ } ) . join ( ',\n ' ) + '\n]' ;
50117
51118 //aggregate and append to karma.conf.js in the project root folder
52119 gulp
53120 . src ( path . resolve ( configFileName ) )
54121 . on ( 'data' , function ( karmaConfigFile ) {
55122 var contents = karmaConfigFile . contents . toString ( ) ;
56123 contents = contents . replace ( filesAppendRegex , filesAppend ) ;
124+ contents = contents . replace ( reportersAppendRegex , reportersAppend ) ;
125+ contents = contents . replace ( reportersAppendRegex , reportersAppend ) ;
126+ contents = contents . replace ( pluginsAppendRegex , pluginsAppend ) ;
57127 karmaConfigFile . contents = new Buffer ( contents ) ;
58128 stream . push ( karmaConfigFile ) ;
59129 } )
@@ -70,13 +140,15 @@ function karmaCreateConfig(configFileName) {
70140 * No output. Ends when the Karma process ends.
71141 * Runs karma in a child process, to avoid `process.exit()` called by karma.
72142 *
143+ * @param {Array.<string> } [reporters] The name of the karma reporter to use
73144 * @param {number } [bannerWidth] The width of banner comment, zero or omitted for none
74145 * @returns {stream.Through } A through strconcateam that performs the operation of a gulp stream
75146 */
76- function karmaRun ( bannerWidth ) {
147+ function karmaRun ( reporters , bannerWidth ) {
77148 var options = {
78149 configFile : undefined
79150 } ;
151+ reporters = reporters || [ ] ;
80152
81153 return through . obj ( function transformFn ( file , encoding , done ) {
82154 options . configFile = file . path ;
@@ -87,23 +159,32 @@ function karmaRun(bannerWidth) {
87159 var data = querystring . escape ( JSON . stringify ( options ) ) ;
88160 var command = [ 'node' , quote ( appPath ) , data ] . join ( ' ' ) ;
89161
90- //TODO @bguiz replace reporter function with a standard karma reporter,
91- //and extract it to own module
92- //perhaps extend the spec reporter to do what is being done here, instead of post processing its output here
93- //check if there is a webstorm/ teamcity reporter
94162 childProcess . exec ( command , { cwd : process . cwd ( ) } , function reporter ( stderr , stdout ) {
163+ if ( reporters . length > 0 ) {
164+ if ( stdout ) {
165+ process . stdout . write ( stdout ) ;
166+ }
167+ if ( stderr ) {
168+ process . stderr . write ( stderr ) ;
169+ }
170+ done ( ) ;
171+ return ;
172+ }
173+
174+ //TODO @bguiz replace reporter function with a standard karma reporter,
175+ //and extract it to own module
176+
177+ //default reporter by parsing the stdout and stderr of karma
95178 var report ;
96179 if ( stdout ) {
97- console . log ( stdout ) ;
98180 report = stdout
99181 . replace ( / ^ \s + / gm, '' ) // remove leading whitespace
100182 . replace ( / ^ ( L O G .* \n $ ) / gm, options . silent ? '' : '$1' ) // remove logging
101183 . replace ( / a t \s + n u l l \. .* / gm, '' ) // remove file reference
102184 . replace ( / \n \n / gm, '\n' ) // consolidate consecutive line breaks
103185 . replace ( / ^ \n | \n $ / g, '' ) ; // remove leading and trailing line breaks overall
104- } else if ( stderr ) {
105- console . log ( stderr ) ;
106-
186+ }
187+ else if ( stderr ) {
107188 var analysis = / $ E r r o r \: \s * ( .* ) $ / mg. exec ( stderr ) ;
108189 report = analysis ? analysis [ 1 ] : stderr ;
109190 }
0 commit comments