/
package.impl.ts
127 lines (117 loc) · 3.82 KB
/
package.impl.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
import type { ExecutorContext } from '@nrwl/devkit';
import { readCachedProjectGraph } from '@nrwl/workspace/src/core/project-graph';
import {
calculateProjectDependencies,
checkDependentProjectsHaveBeenBuilt,
createTmpTsConfig,
DependentBuildableProjectNode,
updateBuildableProjectPackageJsonDependencies,
} from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import type { NgPackagr } from 'ng-packagr';
import { resolve } from 'path';
import { from } from 'rxjs';
import { eachValueFrom } from 'rxjs-for-await';
import { mapTo, switchMap, tap } from 'rxjs/operators';
import { NX_ENTRY_POINT_PROVIDERS } from './ng-packagr-adjustments/ng-package/entry-point/entry-point.di';
import { nxProvideOptions } from './ng-packagr-adjustments/ng-package/options.di';
import {
NX_PACKAGE_PROVIDERS,
NX_PACKAGE_TRANSFORM,
} from './ng-packagr-adjustments/ng-package/package.di';
import type { BuildAngularLibraryExecutorOptions } from './schema';
async function initializeNgPackagr(
options: BuildAngularLibraryExecutorOptions,
context: ExecutorContext,
projectDependencies: DependentBuildableProjectNode[]
): Promise<NgPackagr> {
const packager = new (await import('ng-packagr')).NgPackagr([
...NX_PACKAGE_PROVIDERS,
...NX_ENTRY_POINT_PROVIDERS,
nxProvideOptions({
tailwindConfig: options.tailwindConfig,
watch: options.watch,
}),
]);
packager.forProject(resolve(context.root, options.project));
packager.withBuildTransform(NX_PACKAGE_TRANSFORM.provide);
if (options.tsConfig) {
const tsConfigPath = createTmpTsConfig(
options.tsConfig,
context.root,
context.workspace.projects[context.projectName].root,
projectDependencies
);
packager.withTsConfig(tsConfigPath);
}
return packager;
}
/**
* Creates an executor function that executes the library build of an Angular
* package using ng-packagr.
* @param initializeNgPackagr function that returns an ngPackagr instance to use for the build.
*/
export function createLibraryExecutor(
initializeNgPackagr: (
options: BuildAngularLibraryExecutorOptions,
context: ExecutorContext,
projectDependencies: DependentBuildableProjectNode[]
) => Promise<NgPackagr>
) {
return async function* (
options: BuildAngularLibraryExecutorOptions,
context: ExecutorContext
) {
const { target, dependencies, topLevelDependencies } =
calculateProjectDependencies(
readCachedProjectGraph(),
context.root,
context.projectName,
context.targetName,
context.configurationName
);
if (
!checkDependentProjectsHaveBeenBuilt(
context.root,
context.projectName,
context.targetName,
dependencies
)
) {
return Promise.resolve({ success: false });
}
function updatePackageJson(): void {
if (
dependencies.length > 0 &&
options.updateBuildableProjectDepsInPackageJson
) {
updateBuildableProjectPackageJsonDependencies(
context.root,
context.projectName,
context.targetName,
context.configurationName,
target,
topLevelDependencies,
options.buildableProjectDepsInPackageJsonType
);
}
}
if (options.watch) {
return yield* eachValueFrom(
from(initializeNgPackagr(options, context, dependencies)).pipe(
switchMap((packagr) => packagr.watch()),
tap(() => updatePackageJson()),
mapTo({ success: true })
)
);
}
return from(initializeNgPackagr(options, context, dependencies))
.pipe(
switchMap((packagr) => packagr.build()),
tap(() => updatePackageJson()),
mapTo({ success: true })
)
.toPromise();
};
}
export const packageExecutor = createLibraryExecutor(initializeNgPackagr);
export default packageExecutor;