-
Notifications
You must be signed in to change notification settings - Fork 295
/
packagr.ts
133 lines (115 loc) · 3.92 KB
/
packagr.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { InjectionToken, Provider, ReflectiveInjector } from 'injection-js';
import { ParsedConfiguration } from '@angular/compiler-cli';
import { of as observableOf, Observable, throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { BuildGraph } from '../brocc/build-graph';
import { Transform } from '../brocc/transform';
import * as log from '../util/log';
import { provideTsConfig } from './init/init-tsconfig.di';
import { ENTRY_POINT_PROVIDERS } from './entry-point.di';
import { PACKAGE_TRANSFORM, PACKAGE_PROVIDERS } from './package.di';
import { provideProject } from './project.di';
import { provideOptions, NgPackagrOptions } from './options.di';
/**
* The original ng-packagr implemented on top of a rxjs-ified and di-jectable transformation pipeline.
*
* See the `docs/transformations.md` for more prose description.
*
* @link https://github.com/ng-packagr/ng-packagr/pull/572
*/
export class NgPackagr {
private buildTransform: InjectionToken<Transform> = PACKAGE_TRANSFORM.provide;
constructor(private providers: Provider[]) {}
/**
* Adds options to ng-packagr
*
* @param options Ng Packagr Options
* @return Self instance for fluent API
*/
public withOptions(options: NgPackagrOptions): NgPackagr {
this.providers.push(provideOptions(options));
return this;
}
/**
* Sets the path to the user's "ng-package" file (either `package.json`, `ng-package.json`, or `ng-package.js`)
*
* @param project File path
* @return Self instance for fluent API
*/
public forProject(project: string): NgPackagr {
this.providers.push(provideProject(project));
return this;
}
/**
* Adds dependency injection providers.
*
* @param providers
* @return Self instance for fluent API
* @link https://github.com/mgechev/injection-js
*/
public withProviders(providers: Provider[]): NgPackagr {
this.providers = [...this.providers, ...providers];
return this;
}
/**
* Overwrites the default TypeScript configuration.
*
* @param defaultValues A tsconfig providing default values to the compilation.
* @return Self instance for fluent API
*/
public withTsConfig(defaultValues: ParsedConfiguration | string): NgPackagr {
this.providers.push(provideTsConfig(defaultValues));
return this;
}
/**
* Overwrites the 'build' transform.
*
* @param transform
* @return Self intance for fluent API
*/
public withBuildTransform(transform: InjectionToken<Transform>): NgPackagr {
this.buildTransform = transform;
return this;
}
/**
* Builds the project by kick-starting the 'build' transform over an (initially) empty `BuildGraph``
*
* @return A promisified result of the transformation pipeline.
*/
public build(): Promise<void> {
return this.buildAsObservable().toPromise();
}
/**
* Builds and watch for changes by kick-starting the 'watch' transform over an (initially) empty `BuildGraph``
*
* @return An observable result of the transformation pipeline.
*/
public watch(): Observable<void> {
this.providers.push(provideOptions({ watch: true }));
return this.buildAsObservable();
}
/**
* Builds the project by kick-starting the 'build' transform over an (initially) empty `BuildGraph``
*
* @return An observable result of the transformation pipeline.
*/
public buildAsObservable(): Observable<void> {
const injector = ReflectiveInjector.resolveAndCreate(this.providers);
const buildTransformOperator = injector.get(this.buildTransform);
return observableOf(new BuildGraph()).pipe(
buildTransformOperator,
catchError(err => {
// Report error and re-throw to subscribers
log.error(err);
return throwError(err);
}),
map(() => undefined),
);
}
}
export const ngPackagr = (): NgPackagr =>
new NgPackagr([
// Add default providers to this list.
...PACKAGE_PROVIDERS,
...ENTRY_POINT_PROVIDERS,
]);