11import { workspaces } from '@angular-devkit/core' ;
2- import { SchematicContext , Rule , SchematicsException , Tree } from '@angular-devkit/schematics' ;
2+ import { SchematicContext , Rule , Tree } from '@angular-devkit/schematics' ;
33import { Options } from '../interfaces/options' ;
44import { createHost } from './util' ;
55
@@ -47,39 +47,44 @@ export const getWorkspacePath = (host: Tree): string => {
4747const logIncludingDependency = ( context : SchematicContext , pkg : string , version : string ) : void =>
4848 context . logger . info ( `Including ${ pkg } - Version: ${ version } ` ) ;
4949
50- const getTargetedProjectOptions = ( project : workspaces . ProjectDefinition , target : string ) => {
50+ const getTargetedProjectOptions = ( project : workspaces . ProjectDefinition , target : string , context : SchematicContext ) => {
5151 if ( project . targets &&
5252 project . targets [ target ] &&
5353 project . targets [ target ] . options ) {
5454 return project . targets [ target ] . options ;
5555 }
5656
57- const projectTarget = project . targets . get ( target ) ;
57+ const projectTarget = project . targets ? .get ( target ) ;
5858 if ( projectTarget ) {
5959 return projectTarget . 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 const getConfigFile = ( project : workspaces . ProjectDefinition , option : string , configSection : string = 'build' ) : string => {
66- const options = getTargetedProjectOptions ( project , configSection ) ;
67- if ( ! options ) {
68- throw new SchematicsException ( `Could not find matching ${ configSection } section` +
69- `inside of the workspace config ${ project . sourceRoot } ` ) ;
70- }
71- if ( ! options [ option ] ) {
72- throw new SchematicsException ( `Could not find the project ${ option } file inside of the ` +
73- `workspace config ${ project . sourceRoot } ` ) ;
74- }
75- return options [ option ] ;
76-
77- } ;
67+ export const getConfigFile =
68+ ( project : workspaces . ProjectDefinition , option : string , context : SchematicContext , configSection : string = 'build' ) : string => {
69+ const options = getTargetedProjectOptions ( project , configSection , context ) ;
70+ if ( ! options ) {
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.` ) ;
7873
74+ }
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+ }
82+ }
83+ } ;
7984export const overwriteJsonFile = ( tree : Tree , targetFile : string , data : any ) =>
8085 tree . overwrite ( targetFile , JSON . stringify ( data , null , 2 ) + '\n' ) ;
8186
82-
87+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
8388export const logSuccess = ( options : Options ) : Rule => ( tree : Tree , context : SchematicContext ) => {
8489 context . logger . info ( '' ) ;
8590 context . logger . warn ( 'Ignite UI for Angular installed' ) ;
@@ -140,24 +145,29 @@ export const getPropertyFromWorkspace = (targetProp: string, workspace: any, cur
140145 return null ;
141146} ;
142147
143- const addHammerToConfig = async ( project : workspaces . ProjectDefinition , tree : Tree , config : string ) : Promise < void > => {
144- const projectOptions = getTargetedProjectOptions ( project , config ) ;
145- const tsPath = getConfigFile ( project , 'main' , config ) ;
146- const hammerImport = 'import \'hammerjs\';\n' ;
147- const tsContent = tree . read ( tsPath ) . toString ( ) ;
148- // if there are no elements in the architect[config]options.scripts array that contain hammerjs
149- // and the "main" file does not contain an import with hammerjs
150- if ( ! projectOptions . scripts . some ( el => el . includes ( 'hammerjs' ) ) && ! tsContent . includes ( hammerImport ) ) {
151- // import hammerjs in the specified by config main file
152- const mainContents = hammerImport + tsContent ;
153- tree . overwrite ( tsPath , mainContents ) ;
154- }
155- } ;
148+ const addHammerToConfig =
149+ async ( project : workspaces . ProjectDefinition , tree : Tree , config : string , context : SchematicContext ) : Promise < void > => {
150+ const projectOptions = getTargetedProjectOptions ( project , config , context ) ;
151+ const tsPath = getConfigFile ( project , 'main' , context , config ) ;
152+ const hammerImport = 'import \'hammerjs\';\n' ;
153+ const tsContent = tree . read ( tsPath ) ?. toString ( ) ;
154+ // if there are no elements in the architect[config]options.scripts array that contain hammerjs
155+ // and the "main" file does not contain an import with hammerjs
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+ return ;
161+ }
162+ context . logger . warn ( `Could not find a matching scripts array property under ${ config } options. ` +
163+ `It could require you to manually update it to 'scripts': [ ${ hammerjsFilePath } ] ` ) ;
164+ }
165+ } ;
156166
157167const includeDependencies = async ( pkgJson : any , context : SchematicContext , tree : Tree ) : Promise < void > => {
158168 const workspaceHost = createHost ( tree ) ;
159169 const { workspace } = await workspaces . readWorkspace ( tree . root . path , workspaceHost ) ;
160- const project = workspace . projects . get ( workspace . extensions [ 'defaultProject' ] as string ) ;
170+ const defaultProject = workspace . projects . get ( workspace . extensions [ 'defaultProject' ] as string ) ;
161171 for ( const pkg of Object . keys ( pkgJson . dependencies ) ) {
162172 const version = pkgJson . dependencies [ pkg ] ;
163173 const entry = DEPENDENCIES_MAP . find ( e => e . name === pkg ) ;
@@ -168,8 +178,8 @@ const includeDependencies = async (pkgJson: any, context: SchematicContext, tree
168178 case 'hammerjs' :
169179 logIncludingDependency ( context , pkg , version ) ;
170180 addPackageToPkgJson ( tree , pkg , version , entry . target ) ;
171- await addHammerToConfig ( project , tree , 'build' ) ;
172- await addHammerToConfig ( project , tree , 'test' ) ;
181+ await addHammerToConfig ( defaultProject , tree , 'build' , context ) ;
182+ await addHammerToConfig ( defaultProject , tree , 'test' , context ) ;
173183 break ;
174184 default :
175185 logIncludingDependency ( context , pkg , version ) ;
0 commit comments