@@ -60,16 +60,19 @@ function getTargetedProjectOptions(project: WorkspaceProject<ProjectType>, targe
6060 throw new SchematicsException ( `Cannot determine the project's configuration for: ${ target } ` ) ;
6161}
6262
63- export function getConfigFile ( project : WorkspaceProject < ProjectType > , option : string ) : string {
64- const buildOptions = getTargetedProjectOptions ( project , 'build' ) ;
65- if ( ! buildOptions [ option ] ) {
63+ export function getConfigFile ( project : WorkspaceProject < ProjectType > , option : string , configSection : string = 'build' ) : string {
64+ const options = getTargetedProjectOptions ( project , configSection ) ;
65+ if ( ! options ) {
66+ throw new SchematicsException ( `Could not find matching ${ configSection } section` +
67+ `inside of the workspace config ${ project . sourceRoot } ` ) ;
68+ }
69+ if ( ! options [ option ] ) {
6670 throw new SchematicsException ( `Could not find the project ${ option } file inside of the ` +
67- `workspace config ( ${ project . sourceRoot } ) ` ) ;
71+ `workspace config ${ project . sourceRoot } ` ) ;
6872 }
73+ return options [ option ] ;
6974
70- return buildOptions [ option ] ;
7175}
72-
7376export function overwriteJsonFile ( tree : Tree , targetFile : string , data : any ) {
7477 tree . overwrite ( targetFile , JSON . stringify ( data , null , 2 ) + '\n' ) ;
7578}
@@ -134,6 +137,20 @@ export function getPropertyFromWorkspace(targetProp: string, workspace: any, cur
134137 return null ;
135138}
136139
140+ function addHammerToConfig ( project : WorkspaceProject < ProjectType > , tree : Tree , config : string ) {
141+ const projectOptions = getTargetedProjectOptions ( project , config ) ;
142+ const tsPath = getConfigFile ( project , 'main' , config ) ;
143+ const hammerImport = 'import \'hammerjs\';\n' ;
144+ const tsContent = tree . read ( tsPath ) . toString ( ) ;
145+ // if there are no elements in the architect[config]options.scripts array that contain hammerjs
146+ // and the "main" file does not contain an import with hammerjs
147+ if ( ! projectOptions . scripts . some ( el => el . includes ( 'hammerjs' ) ) && ! tsContent . includes ( hammerImport ) ) {
148+ // import hammerjs in the specified by config main file
149+ const mainContents = hammerImport + tsContent ;
150+ tree . overwrite ( tsPath , mainContents ) ;
151+ }
152+ }
153+
137154function includeDependencies ( pkgJson : any , context : SchematicContext , tree : Tree ) {
138155 Object . keys ( pkgJson . dependencies ) . forEach ( pkg => {
139156 const version = pkgJson . dependencies [ pkg ] ;
@@ -145,20 +162,10 @@ function includeDependencies(pkgJson: any, context: SchematicContext, tree: Tree
145162 case 'hammerjs' :
146163 logIncludingDependency ( context , pkg , version ) ;
147164 addPackageToPkgJson ( tree , pkg , version , entry . target ) ;
148-
149165 const workspace = getWorkspace ( tree ) ;
150166 const project = workspace . projects [ workspace . defaultProject ] ;
151- const projectOptions = getTargetedProjectOptions ( project , 'build' ) ;
152- const mainTsPath = getConfigFile ( project , 'main' ) ;
153- const hammerImport = 'import \'hammerjs\';\n' ;
154- const mainTsContent = tree . read ( mainTsPath ) . toString ( ) ;
155- // if there are no elements in the architect.build.options.scripts array that contain hammerjs
156- // and main.ts does not contain an import with hammerjs
157- if ( ! projectOptions . scripts . some ( el => el . includes ( 'hammerjs' ) ) && ! mainTsContent . includes ( hammerImport ) ) {
158- // import hammerjs in the main.ts file
159- const contents = hammerImport + mainTsContent ;
160- tree . overwrite ( mainTsPath , contents ) ;
161- }
167+ addHammerToConfig ( project , tree , 'build' ) ;
168+ addHammerToConfig ( project , tree , 'test' ) ;
162169 break ;
163170 default :
164171 logIncludingDependency ( context , pkg , version ) ;
0 commit comments