forked from nrwl/nx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert-nx-executor.ts
94 lines (88 loc) · 3.03 KB
/
convert-nx-executor.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
/**
* This is a copy of the @nx/devkit utility but this should not be used outside of the nx package
*/
import type { Observable } from 'rxjs';
import { readNxJson } from '../../config/nx-json';
import { Executor, ExecutorContext } from '../../config/misc-interfaces';
import { retrieveProjectConfigurations } from '../../project-graph/utils/retrieve-workspace-files';
import { ProjectsConfigurations } from '../../config/workspace-json-project-json';
/**
* Convert an Nx Executor into an Angular Devkit Builder
*
* Use this to expose a compatible Angular Builder
*/
export function convertNxExecutor(executor: Executor) {
const builderFunction = (options, builderContext) => {
const promise = async () => {
const nxJsonConfiguration = readNxJson(builderContext.workspaceRoot);
const projectsConfigurations: ProjectsConfigurations = {
version: 2,
projects: (
await retrieveProjectConfigurations(
builderContext.workspaceRoot,
nxJsonConfiguration
)
).projects,
};
const context: ExecutorContext = {
root: builderContext.workspaceRoot,
projectName: builderContext.target.project,
targetName: builderContext.target.target,
target: builderContext.target.target,
configurationName: builderContext.target.configuration,
workspace: { ...nxJsonConfiguration, ...projectsConfigurations },
projectsConfigurations,
nxJsonConfiguration,
cwd: process.cwd(),
projectGraph: null,
taskGraph: null,
isVerbose: false,
};
return executor(options, context);
};
return toObservable(promise());
};
return require('@angular-devkit/architect').createBuilder(builderFunction);
}
function toObservable<T extends { success: boolean }>(
promiseOrAsyncIterator: Promise<T | AsyncIterableIterator<T>>
): Observable<T> {
return new (require('rxjs') as typeof import('rxjs')).Observable(
(subscriber) => {
promiseOrAsyncIterator
.then((value) => {
if (!(value as any).next) {
subscriber.next(value as T);
subscriber.complete();
} else {
let asyncIterator = value as AsyncIterableIterator<T>;
function recurse(iterator: AsyncIterableIterator<T>) {
iterator
.next()
.then((result) => {
if (!result.done) {
subscriber.next(result.value);
recurse(iterator);
} else {
if (result.value) {
subscriber.next(result.value);
}
subscriber.complete();
}
})
.catch((e) => {
subscriber.error(e);
});
}
recurse(asyncIterator);
return () => {
asyncIterator.return();
};
}
})
.catch((err) => {
subscriber.error(err);
});
}
);
}