1
- import { Rule , SchematicContext , SchematicsException , Tree } from '@angular-devkit/schematics ' ;
2
- import { getWorkspace , getWorkspacePath , ProjectType , WorkspaceProject } from 'schematics-utilities ' ;
1
+ import { TargetDefinition } from '@angular-devkit/core/src/workspace ' ;
2
+ import { chain , Rule , SchematicsException , Tree } from '@angular-devkit/schematics ' ;
3
3
import { NgAddOptions } from './schema' ;
4
+ import { getWorkspace , updateWorkspace } from './workspace' ;
4
5
5
6
6
- export function sourceMapBuilder ( options : NgAddOptions ) : Rule {
7
- return ( tree : Tree , _context : SchematicContext ) => {
8
- // get the workspace details
9
- const workspaceSchema = getWorkspace ( tree ) ;
10
- const workspacePath : string = getWorkspacePath ( tree ) ;
7
+ export function ngAdd ( options : NgAddOptions ) : Rule {
8
+ return async ( host : Tree ) => {
9
+ const workspace = await getWorkspace ( host ) ;
11
10
12
- // getting project name
11
+ // Get project name
13
12
if ( ! options . project ) {
14
- if ( workspaceSchema && workspaceSchema . defaultProject ) {
15
- options . project = workspaceSchema . defaultProject ;
13
+ if ( workspace . extensions . defaultProject ) {
14
+ options . project = workspace . extensions . defaultProject as string ;
16
15
} else {
17
16
throw new SchematicsException (
18
17
'No Angular project selected and no default project in the workspace'
@@ -21,45 +20,32 @@ export function sourceMapBuilder(options: NgAddOptions): Rule {
21
20
}
22
21
23
22
// Validating project name
24
- const project : WorkspaceProject < ProjectType . Application > = workspaceSchema . projects [ options . project ] ;
23
+ const project = workspace . projects . get ( options . project ) ;
25
24
if ( ! project ) {
26
- throw new SchematicsException (
27
- 'The specified Angular project is not defined in this workspace'
28
- ) ;
25
+ throw new SchematicsException ( `The specified Angular project is not defined in this workspace` ) ;
29
26
}
30
27
31
28
// Checking if it is application
32
- if ( project . projectType !== 'application' ) {
33
- throw new SchematicsException (
34
- `source-map-analyzer requires an Angular project type of "application" in angular.json`
35
- ) ;
29
+ if ( project . extensions [ 'projectType' ] !== 'application' ) {
30
+ throw new SchematicsException ( `source-map-analyzer requires an Angular project type of "application" in angular.json` ) ;
36
31
}
32
+
33
+ const outputPath : string | undefined = project . targets . get ( 'build' ) ?. options ?. outputPath as string ;
37
34
38
- // Getting output path from Angular.json
39
- if (
40
- ! project . architect ||
41
- ! project . architect . build ||
42
- ! project . architect . build . options ||
43
- ! project . architect . build . options . outputPath
44
- ) {
45
- throw new SchematicsException (
46
- `Cannot read the output path(architect.build.options.outputPath) of the Angular project "${ options . project } " in angular.json`
47
- ) ;
35
+ if ( ! outputPath ) {
36
+ const message : string = `Cannot read the output path(architect.build.options.outputPath) of the Angular project "${ options . project } " in angular.json` ;
37
+ throw new SchematicsException ( message ) ;
48
38
}
49
39
50
- // adding deploy statement for builder
51
- project . architect [ 'analyze' ] = {
52
- "builder" : "@ngx-builders/analyze:analyze" ,
53
- "options" : {
54
- "outputPath" : project . architect . build . options . outputPath
40
+ var targetDefinition : TargetDefinition = {
41
+ builder : "@ngx-builders/analyze:analyze" ,
42
+ options : {
43
+ outputPath : outputPath
55
44
}
56
45
}
57
46
58
- tree . overwrite ( workspacePath , JSON . stringify ( workspaceSchema , null , 2 ) ) ;
59
- return tree ;
60
- } ;
61
- }
47
+ project . targets . add ( { name : 'analyze' , ...targetDefinition } ) ;
62
48
63
- export default function ( options : NgAddOptions ) : Rule {
64
- return sourceMapBuilder ( options )
49
+ return chain ( [ updateWorkspace ( workspace ) ] ) ;
50
+ } ;
65
51
}
0 commit comments