@@ -12,20 +12,55 @@ import {
1212 BuilderConfiguration ,
1313 BuilderContext ,
1414} 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' ;
1617import * 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' ;
1823
1924// TODO move this function to architect or somewhere else where it can be imported from.
2025// Blatantly copy-pasted from 'require-project-module.ts'.
2126function requireProjectModule ( root : string , moduleName : string ) {
2227 return require ( require . resolve ( moduleName , { paths : [ root ] } ) ) ;
2328}
2429
30+ function resolveProjectModule ( root : string , moduleName : string ) {
31+ return require . resolve ( moduleName , { paths : [ root ] } ) ;
32+ }
2533
2634export interface NgPackagrBuilderOptions {
2735 project : string ;
2836 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 ;
2964}
3065
3166export class NgPackagrBuilder implements Builder < NgPackagrBuilderOptions > {
@@ -53,12 +88,30 @@ export class NgPackagrBuilder implements Builder<NgPackagrBuilderOptions> {
5388 ngPkgProject . withTsConfig ( tsConfigPath ) ;
5489 }
5590
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+ }
62115 } ) ;
63116 }
64117
0 commit comments