11import * as path from 'path' ;
22import * as fs from 'fs' ;
3+ import * as glob from 'glob' ;
34
45import { CliConfig } from '../models/config' ;
56import { Pattern } from './glob-copy-webpack-plugin' ;
@@ -16,6 +17,29 @@ function isDirectory(path: string) {
1617 }
1718}
1819
20+ // Add files to the Karma files array.
21+ function addKarmaFiles ( files : any [ ] , newFiles : any [ ] , prepend = false ) {
22+ const defaults = {
23+ included : true ,
24+ served : true ,
25+ watched : true
26+ } ;
27+
28+ const processedFiles = newFiles
29+ // Remove globs that do not match any files, otherwise Karma will show a warning for these.
30+ . filter ( file => glob . sync ( file . pattern , { nodir : true } ) . length != 0 )
31+ // Fill in pattern properties with defaults.
32+ . map ( file => ( { ...defaults , ...file } ) ) ;
33+
34+ // It's important to not replace the array, because
35+ // karma already has a reference to the existing array.
36+ if ( prepend ) {
37+ files . unshift ( ...processedFiles ) ;
38+ } else {
39+ files . push ( ...processedFiles ) ;
40+ }
41+ }
42+
1943const init : any = ( config : any ) => {
2044 const apps = CliConfig . fromProject ( ) . config . apps ;
2145 const appConfig = getAppFromConfig ( apps , config . angularCli . app ) ;
@@ -42,12 +66,7 @@ const init: any = (config: any) => {
4266 // Build karma file pattern.
4367 const assetPath = path . join ( pattern . input , pattern . glob ) ;
4468 const filePattern = isDirectory ( assetPath ) ? assetPath + '/**' : assetPath ;
45- config . files . push ( {
46- pattern : filePattern ,
47- included : false ,
48- served : true ,
49- watched : true
50- } ) ;
69+ addKarmaFiles ( config . files , [ { pattern : filePattern , included : false } ] ) ;
5170
5271 // The `files` entry serves the file from `/base/{asset.input}/{asset.glob}`.
5372 // We need to add a URL rewrite that exposes the asset as `/{asset.output}/{asset.glob}`.
@@ -99,31 +118,15 @@ const init: any = (config: any) => {
99118 const globalScriptPatterns = extraEntryParser ( appConfig . scripts , appRoot , 'scripts' )
100119 // Neither renamed nor lazy scripts are currently supported
101120 . filter ( script => ! ( script . output || script . lazy ) )
102- . map ( script => ( {
103- pattern : path . resolve ( appRoot , script . input ) ,
104- included : true ,
105- served : true ,
106- watched : true
107- } ) ) ;
108-
109- // Unshift elements onto the beginning of the files array.
110- // It's important to not replace the array, because
111- // karma already has a reference to the existing array.
112- config . files . unshift ( ...globalScriptPatterns ) ;
121+ . map ( script => ( { pattern : path . resolve ( appRoot , script . input ) } ) ) ;
122+ addKarmaFiles ( config . files , globalScriptPatterns , true ) ;
113123 }
114124
115125 // Add polyfills file before everything else
116126 if ( appConfig . polyfills ) {
117127 const polyfillsFile = path . resolve ( appRoot , appConfig . polyfills ) ;
118- const polyfillsPattern = {
119- pattern : polyfillsFile ,
120- included : true ,
121- served : true ,
122- watched : true
123- } ;
124128 config . preprocessors [ polyfillsFile ] = [ 'webpack' , 'sourcemap' ] ;
125- // Same as above.
126- config . files . unshift ( polyfillsPattern ) ;
129+ addKarmaFiles ( config . files , [ { pattern : polyfillsFile } ] , true ) ;
127130 }
128131} ;
129132
0 commit comments