@@ -60,16 +60,19 @@ function getTargetedProjectOptions(project: WorkspaceProject<ProjectType>, targe
60
60
throw new SchematicsException ( `Cannot determine the project's configuration for: ${ target } ` ) ;
61
61
}
62
62
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 ] ) {
66
70
throw new SchematicsException ( `Could not find the project ${ option } file inside of the ` +
67
- `workspace config ( ${ project . sourceRoot } ) ` ) ;
71
+ `workspace config ${ project . sourceRoot } ` ) ;
68
72
}
73
+ return options [ option ] ;
69
74
70
- return buildOptions [ option ] ;
71
75
}
72
-
73
76
export function overwriteJsonFile ( tree : Tree , targetFile : string , data : any ) {
74
77
tree . overwrite ( targetFile , JSON . stringify ( data , null , 2 ) + '\n' ) ;
75
78
}
@@ -134,6 +137,20 @@ export function getPropertyFromWorkspace(targetProp: string, workspace: any, cur
134
137
return null ;
135
138
}
136
139
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
+
137
154
function includeDependencies ( pkgJson : any , context : SchematicContext , tree : Tree ) {
138
155
Object . keys ( pkgJson . dependencies ) . forEach ( pkg => {
139
156
const version = pkgJson . dependencies [ pkg ] ;
@@ -145,20 +162,10 @@ function includeDependencies(pkgJson: any, context: SchematicContext, tree: Tree
145
162
case 'hammerjs' :
146
163
logIncludingDependency ( context , pkg , version ) ;
147
164
addPackageToPkgJson ( tree , pkg , version , entry . target ) ;
148
-
149
165
const workspace = getWorkspace ( tree ) ;
150
166
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' ) ;
162
169
break ;
163
170
default :
164
171
logIncludingDependency ( context , pkg , version ) ;
0 commit comments