1
1
import { Rule , SchematicContext , SchematicsException , Tree , chain } from '@angular-devkit/schematics' ;
2
2
import { experimental , JsonParseMode , parseJson } from '@angular-devkit/core' ;
3
- import {
4
- addPackageJsonDependency ,
5
- NodeDependency ,
6
- NodeDependencyType
7
- } from 'schematics-utilities' ;
3
+ import { addPackageJsonDependency , NodeDependency , NodeDependencyType } from 'schematics-utilities' ;
8
4
9
5
function addPackageJsonDependencies ( ) : Rule {
10
6
return ( host : Tree , context : SchematicContext ) => {
7
+
8
+ // always add the package under dev dependencies
11
9
const dependencies : NodeDependency [ ] = [
12
- { type : NodeDependencyType . Default , version : '~3.0 .0' , name : '@netlify-builder/deploy' }
10
+ { type : NodeDependencyType . Dev , version : '~3.1 .0' , name : '@netlify-builder/deploy' }
13
11
] ;
14
-
12
+
15
13
dependencies . forEach ( dependency => {
16
14
addPackageJsonDependency ( host , dependency ) ;
17
15
context . logger . log ( 'info' , `✅️ Added "${ dependency . name } " into ${ dependency . type } ` ) ;
@@ -22,30 +20,28 @@ function addPackageJsonDependencies(): Rule {
22
20
}
23
21
24
22
function getWorkspace ( host : Tree ) : { path : string ; workspace : experimental . workspace . WorkspaceSchema } {
25
- const possibleFiles = [ '/angular.json' , '/.angular.json' ] ;
26
- const path = possibleFiles . filter ( path => host . exists ( path ) ) [ 0 ] ;
23
+ const possibleFiles = [ '/angular.json' , './angular.json' ] ;
24
+ const path = possibleFiles . find ( path => host . exists ( path ) ) ;
25
+
26
+ if ( ! path ) {
27
+ throw new SchematicsException ( `Could not find angular.json` ) ;
28
+ }
27
29
28
30
const configBuffer = host . read ( path ) ;
29
- if ( configBuffer === null ) {
31
+ if ( ! configBuffer ) {
30
32
throw new SchematicsException ( `Could not find angular.json` ) ;
31
33
}
32
- const content = configBuffer . toString ( ) ;
33
34
35
+ const content = configBuffer . toString ( ) ;
34
36
let workspace : experimental . workspace . WorkspaceSchema ;
37
+
35
38
try {
36
- workspace = ( parseJson (
37
- content ,
38
- JsonParseMode . Loose
39
- ) as { } ) as experimental . workspace . WorkspaceSchema ;
39
+ workspace = < any > parseJson ( content , JsonParseMode . Loose ) as experimental . workspace . WorkspaceSchema ;
40
40
} catch ( e ) {
41
- throw new SchematicsException ( `Could not parse angular.json: ` + e . message ) ;
41
+ throw new SchematicsException ( `Could not parse angular.json: ${ e . message } ` ) ;
42
42
}
43
43
44
- return {
45
- path,
46
- workspace
47
- } ;
48
-
44
+ return { path, workspace } ;
49
45
}
50
46
51
47
interface NgAddOptions {
@@ -56,7 +52,7 @@ interface NgAddOptions {
56
52
57
53
export function netlifyBuilder ( options : NgAddOptions ) : Rule {
58
54
return ( tree : Tree , _context : SchematicContext ) => {
59
- // Verifying Angular.json
55
+ // get the workspace details
60
56
const { path : workspacePath , workspace } = getWorkspace ( tree ) ;
61
57
62
58
// getting project name
@@ -93,9 +89,7 @@ export function netlifyBuilder(options: NgAddOptions): Rule {
93
89
! project . architect . build . options . outputPath
94
90
) {
95
91
throw new SchematicsException (
96
- `Cannot read the output path (architect.build.options.outputPath) of the Angular project "${
97
- options . project
98
- } " in angular.json`
92
+ `Cannot read the output path(architect.build.options.outputPath) of the Angular project "${ options . project } " in angular.json`
99
93
) ;
100
94
}
101
95
@@ -108,19 +102,15 @@ export function netlifyBuilder(options: NgAddOptions): Rule {
108
102
"siteId" : options . siteID ,
109
103
}
110
104
}
111
-
112
- tree . overwrite ( workspacePath , JSON . stringify ( workspace , null , 2 ) ) ;
113
105
106
+ tree . overwrite ( workspacePath , JSON . stringify ( workspace , null , 2 ) ) ;
114
107
return tree ;
115
-
116
108
} ;
117
109
}
118
110
119
111
export default function ( options : NgAddOptions ) : Rule {
120
- return chain (
121
- [
122
- netlifyBuilder ( options ) ,
123
- addPackageJsonDependencies ( )
124
- ]
125
- )
112
+ return chain ( [
113
+ netlifyBuilder ( options ) ,
114
+ addPackageJsonDependencies ( )
115
+ ] ) ;
126
116
}
0 commit comments