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 ' ;
33import { NgAddOptions } from './schema' ;
4+ import { getWorkspace , updateWorkspace } from './workspace' ;
45
56
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 ) ;
1110
12- // getting project name
11+ // Get project name
1312 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 ;
1615 } else {
1716 throw new SchematicsException (
1817 'No Angular project selected and no default project in the workspace'
@@ -21,45 +20,32 @@ export function sourceMapBuilder(options: NgAddOptions): Rule {
2120 }
2221
2322 // Validating project name
24- const project : WorkspaceProject < ProjectType . Application > = workspaceSchema . projects [ options . project ] ;
23+ const project = workspace . projects . get ( options . project ) ;
2524 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` ) ;
2926 }
3027
3128 // 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` ) ;
3631 }
32+
33+ const outputPath : string | undefined = project . targets . get ( 'build' ) ?. options ?. outputPath as string ;
3734
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 ) ;
4838 }
4939
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
5544 }
5645 }
5746
58- tree . overwrite ( workspacePath , JSON . stringify ( workspaceSchema , null , 2 ) ) ;
59- return tree ;
60- } ;
61- }
47+ project . targets . add ( { name : 'analyze' , ...targetDefinition } ) ;
6248
63- export default function ( options : NgAddOptions ) : Rule {
64- return sourceMapBuilder ( options )
49+ return chain ( [ updateWorkspace ( workspace ) ] ) ;
50+ } ;
6551}
0 commit comments