@@ -59,27 +59,18 @@ function getTargetedProjectOptions(project: WorkspaceProject<ProjectType>, targe
59
59
throw new SchematicsException ( `Cannot determine the project's configuration for: ${ target } ` ) ;
60
60
}
61
61
62
- export function getConfigFile ( project : WorkspaceProject < ProjectType > , option : string , configSection : string ) : string {
63
- switch ( configSection ) {
64
- case 'build' :
65
- const buildOptions = getTargetedProjectOptions ( project , 'build' ) ;
66
- if ( ! buildOptions [ option ] ) {
67
- throw new SchematicsException ( `Could not find the project ${ option } file inside of the ` +
68
- `workspace config ${ project . sourceRoot } ` ) ;
69
- }
70
- return buildOptions [ option ] ;
71
-
72
- case 'test' :
73
- const testOptions = getTargetedProjectOptions ( project , 'test' ) ;
74
- if ( ! testOptions [ option ] ) {
75
- throw new SchematicsException ( `Could not find the project ${ option } file inside of the ` +
76
- `workspace config ${ project . sourceRoot } ` ) ;
77
- }
78
- return testOptions [ option ] ;
79
- default :
80
- throw new SchematicsException ( `Could not find matching ${ configSection } section` +
62
+ export function getConfigFile ( project : WorkspaceProject < ProjectType > , option : string , configSection : string = 'build' ) : string {
63
+ const options = getTargetedProjectOptions ( project , configSection ) ;
64
+ if ( ! options ) {
65
+ throw new SchematicsException ( `Could not find matching ${ configSection } section` +
81
66
`inside of the workspace config ${ project . sourceRoot } ` ) ;
82
67
}
68
+ if ( ! options [ option ] ) {
69
+ throw new SchematicsException ( `Could not find the project ${ option } file inside of the ` +
70
+ `workspace config ${ project . sourceRoot } ` ) ;
71
+ }
72
+ return options [ option ] ;
73
+
83
74
}
84
75
export function overwriteJsonFile ( tree : Tree , targetFile : string , data : any ) {
85
76
tree . overwrite ( targetFile , JSON . stringify ( data , null , 2 ) + '\n' ) ;
@@ -145,6 +136,20 @@ export function getPropertyFromWorkspace(targetProp: string, workspace: any, cur
145
136
return null ;
146
137
}
147
138
139
+ function addHammerToConfig ( project : WorkspaceProject < ProjectType > , tree : Tree , config : string ) {
140
+ const projectOptions = getTargetedProjectOptions ( project , config ) ;
141
+ const tsPath = getConfigFile ( project , 'main' , config ) ;
142
+ const hammerImport = 'import \'hammerjs\';\n' ;
143
+ const tsContent = tree . read ( tsPath ) . toString ( ) ;
144
+ // if there are no elements in the architect[config]options.scripts array that contain hammerjs
145
+ // and the "main" file does not contain an import with hammerjs
146
+ if ( ! projectOptions . scripts . some ( el => el . includes ( 'hammerjs' ) ) && ! tsContent . includes ( hammerImport ) ) {
147
+ // import hammerjs in the specified by config main file
148
+ const mainContents = hammerImport + tsContent ;
149
+ tree . overwrite ( tsPath , mainContents ) ;
150
+ }
151
+ }
152
+
148
153
function includeDependencies ( pkgJson : any , context : SchematicContext , tree : Tree ) {
149
154
Object . keys ( pkgJson . dependencies ) . forEach ( pkg => {
150
155
const version = pkgJson . dependencies [ pkg ] ;
@@ -156,30 +161,10 @@ function includeDependencies(pkgJson: any, context: SchematicContext, tree: Tree
156
161
case 'hammerjs' :
157
162
logIncludingDependency ( context , pkg , version ) ;
158
163
addPackageToPkgJson ( tree , pkg , version , entry . target ) ;
159
-
160
164
const workspace = getWorkspace ( tree ) ;
161
165
const project = workspace . projects [ workspace . defaultProject ] ;
162
- const projectOptions = getTargetedProjectOptions ( project , 'build' ) ;
163
- const projectTestOptions = getTargetedProjectOptions ( project , 'test' ) ;
164
- const mainTsPath = getConfigFile ( project , 'main' , 'build' ) ;
165
- const testTsPath = getConfigFile ( project , 'main' , 'test' ) ;
166
- const hammerImport = 'import \'hammerjs\';\n' ;
167
- const mainTsContent = tree . read ( mainTsPath ) . toString ( ) ;
168
- const testTsContent = tree . read ( testTsPath ) . toString ( ) ;
169
- // if there are no elements in the architect.build.options.scripts array that contain hammerjs
170
- // and main.ts does not contain an import with hammerjs
171
- if ( ! projectOptions . scripts . some ( el => el . includes ( 'hammerjs' ) ) && ! mainTsContent . includes ( hammerImport ) ) {
172
- // import hammerjs in the main.ts file
173
- const mainContents = hammerImport + mainTsContent ;
174
- tree . overwrite ( mainTsPath , mainContents ) ;
175
- }
176
- // make sure test.ts has hammerjs import even for projects already containing it in main.ts
177
- // if there are no elements in the architect.test.options.scripts array that contain hammerjs
178
- // and test.ts does not contain an import with hammerjs
179
- if ( ! projectTestOptions . scripts . some ( el => el . includes ( 'hammerjs' ) ) && ! testTsContent . includes ( hammerImport ) ) {
180
- const testContents = hammerImport + testTsContent ;
181
- tree . overwrite ( testTsPath , testContents ) ;
182
- }
166
+ addHammerToConfig ( project , tree , 'build' ) ;
167
+ addHammerToConfig ( project , tree , 'test' ) ;
183
168
break ;
184
169
default :
185
170
logIncludingDependency ( context , pkg , version ) ;
0 commit comments