1
1
import { workspaces } from '@angular-devkit/core' ;
2
- import { SchematicContext , Rule , SchematicsException , Tree } from '@angular-devkit/schematics' ;
2
+ import { SchematicContext , Rule , Tree } from '@angular-devkit/schematics' ;
3
3
import { Options } from '../interfaces/options' ;
4
4
import { createHost } from './util' ;
5
5
@@ -47,39 +47,44 @@ export const getWorkspacePath = (host: Tree): string => {
47
47
const logIncludingDependency = ( context : SchematicContext , pkg : string , version : string ) : void =>
48
48
context . logger . info ( `Including ${ pkg } - Version: ${ version } ` ) ;
49
49
50
- const getTargetedProjectOptions = ( project : workspaces . ProjectDefinition , target : string ) => {
50
+ const getTargetedProjectOptions = ( project : workspaces . ProjectDefinition , target : string , context : SchematicContext ) => {
51
51
if ( project . targets &&
52
52
project . targets [ target ] &&
53
53
project . targets [ target ] . options ) {
54
54
return project . targets [ target ] . options ;
55
55
}
56
56
57
- const projectTarget = project . targets . get ( target ) ;
57
+ const projectTarget = project . targets ? .get ( target ) ;
58
58
if ( projectTarget ) {
59
59
return projectTarget . options ;
60
60
}
61
61
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.` ) ;
63
65
} ;
64
66
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.` ) ;
78
73
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
+ } ;
79
84
export const overwriteJsonFile = ( tree : Tree , targetFile : string , data : any ) =>
80
85
tree . overwrite ( targetFile , JSON . stringify ( data , null , 2 ) + '\n' ) ;
81
86
82
-
87
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
83
88
export const logSuccess = ( options : Options ) : Rule => ( tree : Tree , context : SchematicContext ) => {
84
89
context . logger . info ( '' ) ;
85
90
context . logger . warn ( 'Ignite UI for Angular installed' ) ;
@@ -140,24 +145,29 @@ export const getPropertyFromWorkspace = (targetProp: string, workspace: any, cur
140
145
return null ;
141
146
} ;
142
147
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
+ } ;
156
166
157
167
const includeDependencies = async ( pkgJson : any , context : SchematicContext , tree : Tree ) : Promise < void > => {
158
168
const workspaceHost = createHost ( tree ) ;
159
169
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 ) ;
161
171
for ( const pkg of Object . keys ( pkgJson . dependencies ) ) {
162
172
const version = pkgJson . dependencies [ pkg ] ;
163
173
const entry = DEPENDENCIES_MAP . find ( e => e . name === pkg ) ;
@@ -168,8 +178,8 @@ const includeDependencies = async (pkgJson: any, context: SchematicContext, tree
168
178
case 'hammerjs' :
169
179
logIncludingDependency ( context , pkg , version ) ;
170
180
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 ) ;
173
183
break ;
174
184
default :
175
185
logIncludingDependency ( context , pkg , version ) ;
0 commit comments