@@ -2,9 +2,41 @@ import { SchematicContext, Rule, SchematicsException } from '@angular-devkit/sch
22import { Tree } from '@angular-devkit/schematics/src/tree/interface' ;
33import { getWorkspace } from '@schematics/angular/utility/config' ;
44import { Options } from '../interfaces/options' ;
5- import { WorkspaceProject , ProjectType , WorkspaceSchema } from '@schematics/angular/utility/workspace-models' ;
5+ import { WorkspaceProject , ProjectType } from '@schematics/angular/utility/workspace-models' ;
6+
7+
8+ export enum PackageTarget {
9+ DEV = 'devDependencies' ,
10+ REGULAR = 'dependencies' ,
11+ NONE = 'none'
12+ }
13+ export interface PackageEntry {
14+ name : string ;
15+ target : PackageTarget ;
16+ }
17+
618
719const schematicsPackage = '@igniteui/angular-schematics' ;
20+ /**
21+ * Dependencies are explicitly defined here, so we avoid adding
22+ * unnecessary packages to the consuming project's deps
23+ */
24+ export const DEPENDENCIES_MAP : PackageEntry [ ] = [
25+ // dependencies
26+ { name : 'hammerjs' , target : PackageTarget . REGULAR } ,
27+ { name : 'jszip' , target : PackageTarget . REGULAR } ,
28+ { name : 'resize-observer-polyfill' , target : PackageTarget . REGULAR } ,
29+ { name : '@types/hammerjs' , target : PackageTarget . DEV } ,
30+ { name : '@types/jszip' , target : PackageTarget . DEV } ,
31+ // peerDependencies
32+ { name : '@angular/forms' , target : PackageTarget . NONE } ,
33+ { name : '@angular/common' , target : PackageTarget . NONE } ,
34+ { name : '@angular/core' , target : PackageTarget . NONE } ,
35+ { name : '@angular/animations' , target : PackageTarget . NONE } ,
36+ { name : 'web-animations-js' , target : PackageTarget . REGULAR } ,
37+ // igxDevDependencies
38+ { name : '@igniteui/angular-schematics' , target : PackageTarget . DEV }
39+ ] ;
840
941function logIncludingDependency ( context : SchematicContext , pkg : string , version : string ) : void {
1042 context . logger . info ( `Including ${ pkg } - Version: ${ version } ` ) ;
@@ -52,22 +84,20 @@ export function logSuccess(options: Options): Rule {
5284export function addDependencies ( options : Options ) : Rule {
5385 return ( tree : Tree , context : SchematicContext ) => {
5486 const pkgJson = require ( '../../package.json' ) ;
55- const dependencies = 'dependencies' ;
56- const devDependencies = 'devDependencies' ;
5787
58- includeDependencies ( pkgJson , context , tree , dependencies ) ;
88+ includeDependencies ( pkgJson , context , tree ) ;
5989
6090 // Add web-animations-js to dependencies
6191 Object . keys ( pkgJson . peerDependencies ) . forEach ( pkg => {
62- const version = pkgJson . peerDependencies [ pkg ] ;
6392 if ( pkg . includes ( 'web-animations' ) ) {
64- addPackageToPkgJson ( tree , pkg , version , dependencies ) ;
93+ const version = pkgJson . peerDependencies [ pkg ] ;
94+ addPackageToPkgJson ( tree , pkg , version , PackageTarget . REGULAR ) ;
6595 logIncludingDependency ( context , pkg , version ) ;
6696 return ;
6797 }
6898 } ) ;
6999
70- addPackageToPkgJson ( tree , schematicsPackage , pkgJson . igxDevDependencies [ schematicsPackage ] , devDependencies ) ;
100+ addPackageToPkgJson ( tree , schematicsPackage , pkgJson . igxDevDependencies [ schematicsPackage ] , PackageTarget . DEV ) ;
71101 return tree ;
72102 } ;
73103}
@@ -102,13 +132,17 @@ export function getPropertyFromWorkspace(targetProp: string, workspace: any, cur
102132 return null ;
103133}
104134
105- function includeDependencies ( pkgJson : any , context : SchematicContext , tree : Tree , dependenciesKey : string ) {
135+ function includeDependencies ( pkgJson : any , context : SchematicContext , tree : Tree ) {
106136 Object . keys ( pkgJson . dependencies ) . forEach ( pkg => {
107137 const version = pkgJson . dependencies [ pkg ] ;
138+ const entry = DEPENDENCIES_MAP . find ( e => e . name === pkg ) ;
139+ if ( ! entry || entry . target === PackageTarget . NONE ) {
140+ return ;
141+ }
108142 switch ( pkg ) {
109143 case 'hammerjs' :
110144 logIncludingDependency ( context , pkg , version ) ;
111- addPackageToPkgJson ( tree , pkg , version , dependenciesKey ) ;
145+ addPackageToPkgJson ( tree , pkg , version , entry . target ) ;
112146
113147 const workspace = getWorkspace ( tree ) ;
114148 const project = workspace . projects [ workspace . defaultProject ] ;
@@ -126,42 +160,13 @@ function includeDependencies(pkgJson: any, context: SchematicContext, tree: Tree
126160 break ;
127161 default :
128162 logIncludingDependency ( context , pkg , version ) ;
129- addPackageToPkgJson ( tree , pkg , version , dependenciesKey ) ;
163+ addPackageToPkgJson ( tree , pkg , version , entry . target ) ;
130164 break ;
131165 }
132166 } ) ;
133167}
134168
135- /**
136- * Add an item to an angular.json section, within the architect
137- * @param workspace Angular Workspace Schema (angular.json)
138- * @param key Architect tool key to add option to
139- */
140- function addItemToAngularWorkspace ( workspace : WorkspaceSchema , key : string , item : string ) : boolean {
141- const currentProjectName = workspace . defaultProject ;
142- if ( currentProjectName ) {
143- if ( ! workspace . projects [ currentProjectName ] . architect ) {
144- workspace . projects [ currentProjectName ] . architect = { } ;
145- }
146- if ( ! workspace . projects [ currentProjectName ] . architect [ key ] ) {
147- workspace . projects [ currentProjectName ] . architect [ key ] = { } ;
148- }
149- if ( ! workspace . projects [ currentProjectName ] . architect [ key ] . options ) {
150- workspace . projects [ currentProjectName ] . architect [ key ] . options = { } ;
151- }
152- if ( ! workspace . projects [ currentProjectName ] . architect [ key ] . options . scripts ) {
153- workspace . projects [ currentProjectName ] . architect [ key ] . options . scripts = [ ] ;
154- }
155- if ( ! workspace . projects [ currentProjectName ] . architect [ key ] . options . scripts . includes ( item ) ) {
156- workspace . projects [ currentProjectName ] . architect [ key ] . options . scripts . push ( item ) ;
157- return true ;
158- }
159-
160- return false ;
161- }
162- }
163-
164- function addPackageToPkgJson ( tree : Tree , pkg : string , version : string , target : string ) : Tree {
169+ export function addPackageToPkgJson ( tree : Tree , pkg : string , version : string , target : string ) : boolean {
165170 const targetFile = 'package.json' ;
166171 if ( tree . exists ( targetFile ) ) {
167172 const sourceText = tree . read ( targetFile ) . toString ( ) ;
@@ -179,7 +184,9 @@ function addPackageToPkgJson(tree: Tree, pkg: string, version: string, target: s
179184 . reduce ( ( result , key ) => ( result [ key ] = json [ target ] [ key ] ) && result , { } ) ;
180185 tree . overwrite ( targetFile , JSON . stringify ( json , null , 2 ) + '\n' ) ;
181186 }
187+
188+ return true ;
182189 }
183190
184- return tree ;
191+ return false ;
185192}
0 commit comments