-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathindex.ts
66 lines (56 loc) · 2.1 KB
/
index.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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect';
import { join, resolve } from 'path';
import { Observable, from, of } from 'rxjs';
import { catchError, mapTo, switchMap } from 'rxjs/operators';
import { normalizeCacheOptions } from '../../utils/normalize-cache';
import { purgeStaleBuildCache } from '../../utils/purge-cache';
import { Schema as NgPackagrBuilderOptions } from './schema';
/**
* @experimental Direct usage of this function is considered experimental.
*/
export function execute(
options: NgPackagrBuilderOptions,
context: BuilderContext,
): Observable<BuilderOutput> {
return from(
(async () => {
// Purge old build disk cache.
await purgeStaleBuildCache(context);
const root = context.workspaceRoot;
const packager = (await import('ng-packagr')).ngPackagr();
packager.forProject(resolve(root, options.project));
if (options.tsConfig) {
packager.withTsConfig(resolve(root, options.tsConfig));
}
const projectName = context.target?.project;
if (!projectName) {
throw new Error('The builder requires a target.');
}
const metadata = await context.getProjectMetadata(projectName);
const { enabled: cacheEnabled, path: cacheDirectory } = normalizeCacheOptions(
metadata,
context.workspaceRoot,
);
const ngPackagrOptions = {
cacheEnabled,
cacheDirectory: join(cacheDirectory, 'ng-packagr'),
};
return { packager, ngPackagrOptions };
})(),
).pipe(
switchMap(({ packager, ngPackagrOptions }) =>
options.watch ? packager.watch(ngPackagrOptions) : packager.build(ngPackagrOptions),
),
mapTo({ success: true }),
catchError((err) => of({ success: false, error: err.message })),
);
}
export { NgPackagrBuilderOptions };
export default createBuilder<Record<string, string> & NgPackagrBuilderOptions>(execute);