@@ -22,31 +22,31 @@ const schematicsPackage = '@igniteui/angular-schematics';
2222 * unnecessary packages to the consuming project's deps
2323 */
2424export const DEPENDENCIES_MAP : PackageEntry [ ] = [
25- // dependencies
26- { name : 'hammerjs' , target : PackageTarget . REGULAR } ,
27- { name : 'jszip' , target : PackageTarget . REGULAR } ,
28- { name : 'tslib' , target : PackageTarget . NONE } ,
29- { name : 'resize-observer-polyfill' , target : PackageTarget . REGULAR } ,
30- { name : '@types/hammerjs' , target : PackageTarget . DEV } ,
31- { name : 'igniteui-trial-watermark' , target : PackageTarget . NONE } ,
32- { name : 'lodash.mergewith' , target : PackageTarget . NONE } ,
33- { name : 'uuid' , target : PackageTarget . NONE } ,
34- { name : 'web-animations-js' , target : PackageTarget . REGULAR } ,
35- { name : '@igniteui/material-icons-extended' , target : PackageTarget . REGULAR } ,
36- // peerDependencies
37- { name : '@angular/forms' , target : PackageTarget . NONE } ,
38- { name : '@angular/common' , target : PackageTarget . NONE } ,
39- { name : '@angular/core' , target : PackageTarget . NONE } ,
40- { name : '@angular/animations' , target : PackageTarget . NONE } ,
41- // igxDevDependencies
42- { name : '@igniteui/angular-schematics' , target : PackageTarget . DEV }
25+ // dependencies
26+ { name : 'hammerjs' , target : PackageTarget . REGULAR } ,
27+ { name : 'jszip' , target : PackageTarget . REGULAR } ,
28+ { name : 'tslib' , target : PackageTarget . NONE } ,
29+ { name : 'resize-observer-polyfill' , target : PackageTarget . REGULAR } ,
30+ { name : '@types/hammerjs' , target : PackageTarget . DEV } ,
31+ { name : 'igniteui-trial-watermark' , target : PackageTarget . NONE } ,
32+ { name : 'lodash.mergewith' , target : PackageTarget . NONE } ,
33+ { name : 'uuid' , target : PackageTarget . NONE } ,
34+ { name : 'web-animations-js' , target : PackageTarget . REGULAR } ,
35+ { name : '@igniteui/material-icons-extended' , target : PackageTarget . REGULAR } ,
36+ // peerDependencies
37+ { name : '@angular/forms' , target : PackageTarget . NONE } ,
38+ { name : '@angular/common' , target : PackageTarget . NONE } ,
39+ { name : '@angular/core' , target : PackageTarget . NONE } ,
40+ { name : '@angular/animations' , target : PackageTarget . NONE } ,
41+ // igxDevDependencies
42+ { name : '@igniteui/angular-schematics' , target : PackageTarget . DEV }
4343] ;
4444
4545function logIncludingDependency ( context : SchematicContext , pkg : string , version : string ) : void {
4646 context . logger . info ( `Including ${ pkg } - Version: ${ version } ` ) ;
4747}
4848
49- function getTargetedProjectOptions ( project : WorkspaceProject < ProjectType > , target : string ) {
49+ function getTargetedProjectOptions ( project : WorkspaceProject < ProjectType > , target : string , context : SchematicContext ) {
5050 if ( project . targets &&
5151 project . targets [ target ] &&
5252 project . targets [ target ] . options ) {
@@ -59,22 +59,29 @@ function getTargetedProjectOptions(project: WorkspaceProject<ProjectType>, targe
5959 return project . architect [ target ] . options ;
6060 }
6161
62- throw new SchematicsException ( `Cannot determine the project's configuration for: ${ target } ` ) ;
62+ context . logger . warn ( `Could not find matching ${ target } options ` +
63+ `in Angular workspace ${ project . sourceRoot } . ` +
64+ `It could require you to manually add and update the ${ target } section.` ) ;
6365}
6466
65- export function getConfigFile ( project : WorkspaceProject < ProjectType > , option : string , configSection : string = 'build' ) : string {
66- const options = getTargetedProjectOptions ( project , configSection ) ;
67+ export function getConfigFile (
68+ project : WorkspaceProject < ProjectType > , option : string , context : SchematicContext , configSection : string = 'build' ) : string {
69+ const options = getTargetedProjectOptions ( project , configSection , context ) ;
6770 if ( ! options ) {
68- throw new SchematicsException ( `Could not find matching ${ configSection } section` +
69- `inside of the workspace config ${ project . sourceRoot } ` ) ;
71+ context . logger . warn ( `Could not find matching ${ configSection } options in Angular workspace. ` +
72+ `It could require you to manually add and update the ${ configSection } options.` ) ;
73+
7074 }
71- if ( ! options [ option ] ) {
72- throw new SchematicsException ( `Could not find the project ${ option } file inside of the ` +
73- `workspace config ${ project . sourceRoot } ` ) ;
75+ if ( options ) {
76+ if ( ! options [ option ] ) {
77+ context . logger . warn ( `Could not find a matching ${ option } property under ${ configSection } options in Angular workspace. ` +
78+ `Some updates may not execute correctly.` ) ;
79+ } else {
80+ return options [ option ] ;
81+ }
7482 }
75- return options [ option ] ;
76-
7783}
84+
7885export function overwriteJsonFile ( tree : Tree , targetFile : string , data : any ) {
7986 tree . overwrite ( targetFile , JSON . stringify ( data , null , 2 ) + '\n' ) ;
8087}
@@ -139,35 +146,40 @@ export function getPropertyFromWorkspace(targetProp: string, workspace: any, cur
139146 return null ;
140147}
141148
142- function addHammerToConfig ( project : WorkspaceProject < ProjectType > , tree : Tree , config : string ) {
143- const projectOptions = getTargetedProjectOptions ( project , config ) ;
144- const tsPath = getConfigFile ( project , 'main' , config ) ;
149+ const addHammerToConfig = ( workspace , project : WorkspaceProject < ProjectType > , tree : Tree , config : string , context : SchematicContext ) => {
150+ const projectOptions = getTargetedProjectOptions ( project , config , context ) ;
151+ const tsPath = getConfigFile ( project , 'main' , context , config ) ;
145152 const hammerImport = 'import \'hammerjs\';\n' ;
146- const tsContent = tree . read ( tsPath ) . toString ( ) ;
153+ const tsContent = tree . read ( tsPath ) ? .toString ( ) ;
147154 // if there are no elements in the architect[config]options.scripts array that contain hammerjs
148155 // and the "main" file does not contain an import with hammerjs
149- if ( ! projectOptions . scripts . some ( el => el . includes ( 'hammerjs' ) ) && ! tsContent . includes ( hammerImport ) ) {
150- // import hammerjs in the specified by config main file
151- const mainContents = hammerImport + tsContent ;
152- tree . overwrite ( tsPath , mainContents ) ;
156+ if ( ! projectOptions ?. scripts ?. some ( el => el . includes ( 'hammerjs' ) ) && ! tsContent ?. includes ( hammerImport ) ) {
157+ const hammerjsFilePath = './node_modules/hammerjs/hammer.min.js' ;
158+ if ( projectOptions ?. scripts ) {
159+ projectOptions . scripts . push ( hammerjsFilePath ) ;
160+ overwriteJsonFile ( tree , 'angular.json' , workspace ) ;
161+ return ;
162+ }
163+ context . logger . warn ( `Could not find a matching scripts array property under ${ config } options. ` +
164+ `It could require you to manually update it to 'scripts': [ ${ hammerjsFilePath } ] ` ) ;
153165 }
154- }
166+ } ;
155167
156168function includeDependencies ( pkgJson : any , context : SchematicContext , tree : Tree ) {
157169 Object . keys ( pkgJson . dependencies ) . forEach ( pkg => {
158170 const version = pkgJson . dependencies [ pkg ] ;
159171 const entry = DEPENDENCIES_MAP . find ( e => e . name === pkg ) ;
172+ const workspace = getWorkspace ( tree ) ;
173+ const defaultProject = workspace . projects [ workspace . defaultProject ] ;
160174 if ( ! entry || entry . target === PackageTarget . NONE ) {
161175 return ;
162176 }
163177 switch ( pkg ) {
164178 case 'hammerjs' :
165179 logIncludingDependency ( context , pkg , version ) ;
166180 addPackageToPkgJson ( tree , pkg , version , entry . target ) ;
167- const workspace = getWorkspace ( tree ) ;
168- const project = workspace . projects [ workspace . defaultProject ] ;
169- addHammerToConfig ( project , tree , 'build' ) ;
170- addHammerToConfig ( project , tree , 'test' ) ;
181+ addHammerToConfig ( workspace , defaultProject , tree , 'build' , context ) ;
182+ addHammerToConfig ( workspace , defaultProject , tree , 'test' , context ) ;
171183 break ;
172184 default :
173185 logIncludingDependency ( context , pkg , version ) ;
0 commit comments