1
1
import * as ts from 'typescript' ;
2
2
import { join , dirname , basename } from 'path' ;
3
3
import { Tree , SchematicsException } from '@angular-devkit/schematics' ;
4
- // import { getWorkspace, WorkspaceProject } from '@schematics/angular/utility/config';
4
+ import { getWorkspace } from '@schematics/angular/utility/config' ;
5
+ import { getProjectTargets } from '@schematics/angular/utility/project-targets' ;
5
6
6
- import { getSourceFile , getAngularJson , safeGet } from './utils' ;
7
+ import { getSourceFile , safeGet } from './utils' ;
7
8
import { findNode , getFunctionParams , findImportPath } from './ast-utils' ;
8
- import { SemVer , getAngularSemver , getAngularCLISemver } from './node-utils' ;
9
9
10
10
export interface AngularProjectSettings {
11
- /** ng cli Npm Version */
12
- ngCliSemVer : SemVer ;
13
- /** ng Npm Version */
14
- ngSemVer : SemVer ;
15
-
16
11
/** default: '' */
17
12
root : string ;
18
13
@@ -57,9 +52,6 @@ export interface CoreProjectSettings {
57
52
mainPath : string ;
58
53
prefix : string ;
59
54
tsConfig : string ;
60
-
61
- ngCliSemVer : SemVer ;
62
- ngSemVer : SemVer ;
63
55
}
64
56
65
57
/**
@@ -90,7 +82,7 @@ export interface ClassMetadata {
90
82
path : string
91
83
}
92
84
93
- export function getAngularProjectSettings ( tree : Tree , projectName : string = '' ) : AngularProjectSettings {
85
+ export function getAngularProjectSettings ( tree : Tree , projectName : string ) : AngularProjectSettings {
94
86
const projectSettings = getCoreProjectSettings ( tree , projectName ) ;
95
87
const entryModule = getEntryModuleMetadata ( tree , projectSettings . mainPath ) ;
96
88
@@ -114,64 +106,47 @@ export function getAngularProjectSettings(tree: Tree, projectName: string = ''):
114
106
} ;
115
107
}
116
108
117
- // Step 1 - get appRoot => open .angular-cli.json -> get apps.root
118
109
export function getCoreProjectSettings ( tree : Tree , projectName : string ) : CoreProjectSettings {
119
- const ngCliSemVer = getAngularCLISemver ( tree ) ;
120
- const ngSemVer = getAngularSemver ( tree ) ;
121
-
122
- if ( ngCliSemVer . major >= 6 ) {
123
- const project = getProjectObject ( tree , projectName ) ;
124
-
125
- const root = project . root || '' ;
126
- const sourceRoot : string = project . sourceRoot || 'src' ;
127
- const mainPath : string =
128
- safeGet ( project , 'targets' , 'build' , 'options' , 'main' ) || // Angular CLI 6.2
129
- safeGet ( project , 'architect' , 'build' , 'options' , 'main' ) || // Angular CLI 6.1
130
- 'src/main.ts' ;
131
- const mainName : string = basename ( mainPath ) . replace ( '.ts' , '' ) ;
132
- const prefix : string = project . prefix || 'app' ;
133
- const tsConfig : string =
134
- safeGet ( project , 'targets' , 'build' , 'options' , 'tsConfig' ) || // Angular CLI 6.2
135
- safeGet ( project , 'architect' , 'build' , 'options' , 'tsConfig' ) || // Angular CLI 6.1
136
- 'src/tsconfig.app.json' ;
137
-
138
- return {
139
- ngCliSemVer,
140
- ngSemVer,
141
-
142
- root,
143
- sourceRoot,
144
- mainName,
145
- mainPath,
146
- prefix,
147
- tsConfig,
148
- } ;
149
- } else {
150
- throw new SchematicsException ( `This schematic is not compatible with @angular/cli 1.x, use 6.x or newer` ) ;
110
+ const project = getProjectObject ( tree , projectName ) ;
111
+ const targets = getProjectTargets ( project ) ;
112
+ if ( ! targets ) {
113
+ throw new SchematicsException (
114
+ `Failed to find build targets for project ${ projectName } !`
115
+ ) ;
151
116
}
152
- }
153
117
154
- export function getProjectObject ( tree : Tree , projectName : string ) {
155
- const angularJson = getAngularJson ( tree ) ;
156
-
157
- // return the requested project object
158
- if ( projectName ) {
159
- const project = angularJson . projects [ projectName ] ;
160
- if ( ! project ) {
161
- throw new SchematicsException ( `Couldn't find --projectName "${ projectName } " in angular.json` ) ;
162
- }
163
-
164
- return project ;
118
+ const buildTarget = targets . build ;
119
+ if ( ! buildTarget ) {
120
+ throw new SchematicsException (
121
+ `Failed to find build target for project ${ projectName } !`
122
+ ) ;
165
123
}
166
124
167
- // or return the default project
168
- if ( angularJson . defaultProject ) {
169
- return angularJson . projects [ angularJson . defaultProject ] ;
125
+ const root = project . root ;
126
+ const sourceRoot = project . sourceRoot || 'src' ;
127
+ const mainPath = safeGet ( buildTarget , 'options' , 'main' ) ;
128
+ const mainName = basename ( mainPath ) . replace ( / \. t s $ / , '' ) ;
129
+ const prefix = project . prefix ;
130
+ const tsConfig = safeGet ( buildTarget , 'options' , 'tsConfig' ) ;
131
+
132
+ return {
133
+ root,
134
+ sourceRoot,
135
+ mainName,
136
+ mainPath,
137
+ prefix,
138
+ tsConfig,
139
+ } ;
140
+ }
141
+
142
+ function getProjectObject ( tree : Tree , projectName : string ) {
143
+ const workspace = getWorkspace ( tree ) ;
144
+ const project = workspace . projects [ projectName ] ;
145
+ if ( ! project ) {
146
+ throw new SchematicsException ( `Couldn't find project "${ projectName } " in the workspace!` ) ;
170
147
}
171
148
172
- // or return the first project on the list
173
- // this is the same behaviour as in ng cli
174
- return Object . values ( angularJson . projects ) [ 0 ] ;
149
+ return project ;
175
150
}
176
151
177
152
// Step 2 - get entryModule and entryModulePath => open ${sourceRoot}/${main}.ts
0 commit comments