@@ -12,20 +12,55 @@ import {
12
12
BuilderConfiguration ,
13
13
BuilderContext ,
14
14
} from '@angular-devkit/architect' ;
15
- import { getSystemPath , normalize , resolve } from '@angular-devkit/core' ;
15
+ import { getSystemPath , normalize , resolve , tags } from '@angular-devkit/core' ;
16
+ import * as fs from 'fs' ;
16
17
import * as ngPackagr from 'ng-packagr' ;
17
- import { Observable } from 'rxjs' ;
18
+ import { EMPTY , Observable } from 'rxjs' ;
19
+ import { catchError , tap } from 'rxjs/operators' ;
20
+ import * as semver from 'semver' ;
21
+
22
+ const NEW_NG_PACKAGR_VERSION = '4.0.0-rc.3' ;
18
23
19
24
// TODO move this function to architect or somewhere else where it can be imported from.
20
25
// Blatantly copy-pasted from 'require-project-module.ts'.
21
26
function requireProjectModule ( root : string , moduleName : string ) {
22
27
return require ( require . resolve ( moduleName , { paths : [ root ] } ) ) ;
23
28
}
24
29
30
+ function resolveProjectModule ( root : string , moduleName : string ) {
31
+ return require . resolve ( moduleName , { paths : [ root ] } ) ;
32
+ }
25
33
26
34
export interface NgPackagrBuilderOptions {
27
35
project : string ;
28
36
tsConfig ?: string ;
37
+ watch ?: boolean ;
38
+ }
39
+
40
+ function checkNgPackagrVersion ( projectRoot : string ) : boolean {
41
+ let ngPackagrJsonPath ;
42
+
43
+ try {
44
+ ngPackagrJsonPath = resolveProjectModule ( projectRoot , 'ng-packagr/package.json' ) ;
45
+ } catch {
46
+ // ng-packagr is not installed
47
+ throw new Error ( tags . stripIndent `
48
+ ng-packagr is not installed. Run \`npm install ng-packagr --save-dev\` and try again.
49
+ ` ) ;
50
+ }
51
+
52
+ const ngPackagrPackageJson = fs . readFileSync ( ngPackagrJsonPath ) . toString ( ) ;
53
+ const ngPackagrVersion = JSON . parse ( ngPackagrPackageJson ) [ 'version' ] ;
54
+
55
+ if ( ! semver . gte ( ngPackagrVersion , NEW_NG_PACKAGR_VERSION ) ) {
56
+ throw new Error ( tags . stripIndent `
57
+ The installed version of ng-packagr is ${ ngPackagrVersion } . The watch feature
58
+ requires ng-packagr version to satisfy ${ NEW_NG_PACKAGR_VERSION } .
59
+ Please upgrade your ng-packagr version.
60
+ ` ) ;
61
+ }
62
+
63
+ return true ;
29
64
}
30
65
31
66
export class NgPackagrBuilder implements Builder < NgPackagrBuilderOptions > {
@@ -53,12 +88,30 @@ export class NgPackagrBuilder implements Builder<NgPackagrBuilderOptions> {
53
88
ngPkgProject . withTsConfig ( tsConfigPath ) ;
54
89
}
55
90
56
- ngPkgProject . build ( )
57
- . then ( ( ) => {
58
- obs . next ( { success : true } ) ;
59
- obs . complete ( ) ;
60
- } )
61
- . catch ( ( e ) => obs . error ( e ) ) ;
91
+ if ( options . watch ) {
92
+ checkNgPackagrVersion ( getSystemPath ( root ) ) ;
93
+
94
+ const ngPkgSubscription = ngPkgProject
95
+ . watch ( )
96
+ . pipe (
97
+ tap ( ( ) => obs . next ( { success : true } ) ) ,
98
+ catchError ( e => {
99
+ obs . error ( e ) ;
100
+
101
+ return EMPTY ;
102
+ } ) ,
103
+ )
104
+ . subscribe ( ) ;
105
+
106
+ return ( ) => ngPkgSubscription . unsubscribe ( ) ;
107
+ } else {
108
+ ngPkgProject . build ( )
109
+ . then ( ( ) => {
110
+ obs . next ( { success : true } ) ;
111
+ obs . complete ( ) ;
112
+ } )
113
+ . catch ( e => obs . error ( e ) ) ;
114
+ }
62
115
} ) ;
63
116
}
64
117
0 commit comments