-
Notifications
You must be signed in to change notification settings - Fork 964
Expand file tree
/
Copy pathbuild-application.task.ts
More file actions
189 lines (170 loc) · 6.44 KB
/
Copy pathbuild-application.task.ts
File metadata and controls
189 lines (170 loc) · 6.44 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { join } from 'path';
import mapSeries from 'p-map-series';
import type { BuildTask, BuiltTaskResult, BuildContext, ComponentResult, ArtifactDefinition } from '@teambit/builder';
import { CAPSULE_ARTIFACTS_DIR } from '@teambit/builder';
import { compact } from 'lodash';
import type { Capsule } from '@teambit/isolator';
import type { Component } from '@teambit/component';
import { ApplicationAspect } from './application.aspect';
import type { ApplicationMain } from './application.main.runtime';
import { AppBuildContext } from './app-build-context';
import type { Application } from './application';
export const BUILD_TASK = 'build_application';
export const ARTIFACTS_DIR_NAME = 'apps';
export type OneAppResult = {
componentResult: ComponentResult;
artifacts?: ArtifactDefinition[];
};
export type OneComponentResult = {
componentResult: ComponentResult;
artifacts?: ArtifactDefinition[];
};
export type BuildAppResult = {
componentResult: ComponentResult;
artifacts?: ArtifactDefinition[];
};
export type BuildDeployContexts = {
deployContext: { publicDir?: string; ssrPublicDir?: string };
name: string;
appType: string;
};
export type Options = {
deploy: boolean;
};
export class AppsBuildTask implements BuildTask {
name = BUILD_TASK;
aspectId = ApplicationAspect.id;
readonly location = 'end';
constructor(
protected application: ApplicationMain,
private opt: Options = { deploy: true }
) {}
/**
* decides whether this task should build a given app. the default task runs on every app except
* platforms; `PlatformAppsBuildTask` overrides this to only run on platforms.
*/
protected shouldRunForApp(app: Application): boolean {
return app.platform !== true;
}
async execute(context: BuildContext): Promise<BuiltTaskResult> {
const originalSeedersIds = context.capsuleNetwork.originalSeedersCapsules.map((c) => c.component.id.toString());
const { capsuleNetwork } = context;
const result: BuiltTaskResult = {
componentsResults: [],
};
// const componentsResults = await mapSeries(apps, async (app): Promise<AppsResults | undefined> => {
await mapSeries(capsuleNetwork.originalSeedersCapsules, async (capsule) => {
const component = capsule.component;
if (originalSeedersIds && originalSeedersIds.length && !originalSeedersIds.includes(component.id.toString())) {
return undefined;
}
const apps = await this.application.loadAppsFromComponent(component, capsule.path);
if (!apps || !apps.length) return undefined;
const componentsResults = await mapSeries(compact(apps), async (app) =>
this.runForOneApp(app, component, capsule, context)
);
const merged = this.mergeAppsResults(compact(componentsResults));
if (merged) {
result.componentsResults.push(merged.componentResult);
if (!result.artifacts) result.artifacts = [];
result.artifacts.push(...(merged.artifacts || []));
}
return undefined;
});
return result;
}
private async runForOneApp(
app: Application,
component: Component,
capsule: Capsule,
context: BuildContext
): Promise<OneAppResult | undefined> {
if (!app.build) return undefined;
if (!this.shouldRunForApp(app)) return undefined;
const artifactsDir = this.getArtifactDirectory();
const capsuleRootDir = context.capsuleNetwork.capsulesRootDir;
const appContext = await this.application.createAppBuildContext(
component.id,
app.name,
capsuleRootDir,
capsule.path
);
const appBuildContext = AppBuildContext.create({
appContext,
buildContext: context,
appComponent: component,
name: app.name,
capsule,
artifactsDir,
});
const deployContext = await app.build(appBuildContext);
const defaultArtifacts: ArtifactDefinition[] = this.getDefaultArtifactDef(app.applicationType || app.name);
const artifacts = defaultArtifacts.concat(deployContext.artifacts || []);
const getDeployContextFromMetadata = () => {
if (deployContext.metadata) {
return deployContext.metadata;
}
// if metadata is not defined, don't save deployContext blindly. in node-app for example it includes the entire
// Network object, with all capsules and components.
return {};
};
return {
artifacts,
componentResult: {
component: capsule.component,
errors: deployContext.errors,
warnings: deployContext.warnings,
metadata: { deployContext: getDeployContextFromMetadata(), name: app.name, appType: app.applicationType },
/**
* @deprecated - please use metadata instead
*
* @guysaar223
* @ranm8
* TODO: we need to think how to pass private metadata between build pipes, maybe create shared context
* or create new deploy context on builder
*/
// @ts-ignore
_metadata: { deployContext, name: app.name, appType: app.applicationType },
},
};
}
private mergeAppsResults(appsResults: OneAppResult[]): OneComponentResult | undefined {
if (!appsResults || !appsResults.length) return undefined;
const merged: OneComponentResult = {
artifacts: [],
componentResult: {
component: appsResults[0].componentResult.component,
errors: [],
warnings: [],
metadata: {
buildDeployContexts: [],
},
},
};
appsResults.forEach((appResult) => {
merged.artifacts = (merged.artifacts || []).concat(appResult.artifacts || []);
merged.componentResult.errors = (merged.componentResult.errors || []).concat(
appResult.componentResult.errors || []
);
merged.componentResult.warnings = (merged.componentResult.warnings || []).concat(
appResult.componentResult.warnings || []
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
merged.componentResult.metadata!.buildDeployContexts =
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(merged.componentResult.metadata!.buildDeployContexts || []).concat(appResult.componentResult.metadata || []);
});
return merged;
}
private getArtifactDirectory() {
return join(CAPSULE_ARTIFACTS_DIR, ARTIFACTS_DIR_NAME);
}
private getDefaultArtifactDef(nameSuffix: string): ArtifactDefinition[] {
return [
{
name: `app-build-${nameSuffix}`,
globPatterns: [`${this.getArtifactDirectory()}/**`],
},
];
}
}