11import { Rule , SchematicContext , SchematicsException , Tree } from '@angular-devkit/schematics' ;
2- import { experimental , JsonParseMode , parseJson } from '@angular-devkit/core' ;
2+ import { getWorkspace , getWorkspacePath , ProjectType , WorkspaceProject } from 'schematics-utilities' ;
3+ import { NgAddOptions } from './schema' ;
34
4- function getWorkspace ( host : Tree ) : { path : string ; workspace : experimental . workspace . WorkspaceSchema } {
5- const possibleFiles = [ '/angular.json' , './angular.json' ] ;
6- const path = possibleFiles . find ( path => host . exists ( path ) ) ;
7-
8- if ( ! path ) {
9- throw new SchematicsException ( `Could not find angular.json` ) ;
10- }
11-
12- const configBuffer = host . read ( path ) ;
13- if ( ! configBuffer ) {
14- throw new SchematicsException ( `Could not find angular.json` ) ;
15- }
16-
17- const content = configBuffer . toString ( ) ;
18- let workspace : experimental . workspace . WorkspaceSchema ;
19-
20- try {
21- workspace = < any > parseJson ( content , JsonParseMode . Loose ) as experimental . workspace . WorkspaceSchema ;
22- } catch ( e ) {
23- throw new SchematicsException ( `Could not parse angular.json: ${ e . message } ` ) ;
24- }
25-
26- return { path, workspace } ;
27- }
28-
29- interface NgAddOptions {
30- project ?: string ;
31- siteID : string ;
32- netlifyToken : string ;
33- }
345
356export function sourceMapBuilder ( options : NgAddOptions ) : Rule {
367 return ( tree : Tree , _context : SchematicContext ) => {
378 // get the workspace details
38- const { path : workspacePath , workspace } = getWorkspace ( tree ) ;
9+ const workspaceSchema = getWorkspace ( tree ) ;
10+ const workspacePath : string = getWorkspacePath ( tree ) ;
3911
4012 // getting project name
4113 if ( ! options . project ) {
42- if ( workspace . defaultProject ) {
43- options . project = workspace . defaultProject ;
14+ if ( workspaceSchema && workspaceSchema . defaultProject ) {
15+ options . project = workspaceSchema . defaultProject ;
4416 } else {
4517 throw new SchematicsException (
4618 'No Angular project selected and no default project in the workspace'
@@ -49,7 +21,7 @@ export function sourceMapBuilder(options: NgAddOptions): Rule {
4921 }
5022
5123 // Validating project name
52- const project = workspace . projects [ options . project ] ;
24+ const project : WorkspaceProject < ProjectType . Application > = workspaceSchema . projects [ options . project ] ;
5325 if ( ! project ) {
5426 throw new SchematicsException (
5527 'The specified Angular project is not defined in this workspace'
@@ -59,7 +31,7 @@ export function sourceMapBuilder(options: NgAddOptions): Rule {
5931 // Checking if it is application
6032 if ( project . projectType !== 'application' ) {
6133 throw new SchematicsException (
62- `Deploy requires an Angular project type of "application" in angular.json`
34+ `source-map-analyzer requires an Angular project type of "application" in angular.json`
6335 ) ;
6436 }
6537
@@ -83,7 +55,7 @@ export function sourceMapBuilder(options: NgAddOptions): Rule {
8355 }
8456 }
8557
86- tree . overwrite ( workspacePath , JSON . stringify ( workspace , null , 2 ) ) ;
58+ tree . overwrite ( workspacePath , JSON . stringify ( workspaceSchema , null , 2 ) ) ;
8759 return tree ;
8860 } ;
8961}
0 commit comments