-
Notifications
You must be signed in to change notification settings - Fork 965
Expand file tree
/
Copy pathapplication.main.runtime.ts
More file actions
565 lines (505 loc) · 17.7 KB
/
Copy pathapplication.main.runtime.ts
File metadata and controls
565 lines (505 loc) · 17.7 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
import type { CLIMain } from '@teambit/cli';
import { MainRuntime, CLIAspect } from '@teambit/cli';
import { compact, flatten, head } from 'lodash';
import type { AspectLoaderMain } from '@teambit/aspect-loader';
import { AspectLoaderAspect } from '@teambit/aspect-loader';
import type { SlotRegistry, Harmony } from '@teambit/harmony';
import { Slot } from '@teambit/harmony';
import type { Workspace } from '@teambit/workspace';
import { WorkspaceAspect } from '@teambit/workspace';
import { BitError } from '@teambit/bit-error';
import type { WatcherMain } from '@teambit/watcher';
import { WatcherAspect } from '@teambit/watcher';
import type { BuilderMain } from '@teambit/builder';
import { BuilderAspect } from '@teambit/builder';
import type { ScopeMain } from '@teambit/scope';
import { ScopeAspect } from '@teambit/scope';
import type { Logger, LoggerMain } from '@teambit/logger';
import { LoggerAspect } from '@teambit/logger';
import type { EnvsMain } from '@teambit/envs';
import { EnvsAspect } from '@teambit/envs';
import type { ComponentMain, ComponentID, Component } from '@teambit/component';
import { ComponentAspect } from '@teambit/component';
import type { ApplicationType } from './application-type';
import type { Application } from './application';
import type { DeploymentProvider } from './deployment-provider';
import { AppNotFound } from './exceptions';
import { ApplicationAspect } from './application.aspect';
import { AppsBuildTask } from './build-application.task';
import { PlatformAppsBuildTask } from './build-platform-application.task';
import { RunCmd } from './run.cmd';
import { AppService } from './application.service';
import { AppCmd, AppListCmd } from './app.cmd';
import { AppPlugin, BIT_APP_PATTERN } from './app.plugin';
import { AppTypePlugin } from './app-type.plugin';
import { AppContext } from './app-context';
import { DeployTask } from './deploy.task';
export type ApplicationTypeSlot = SlotRegistry<ApplicationType<unknown>[]>;
export type ApplicationSlot = SlotRegistry<Application[]>;
export type DeploymentProviderSlot = SlotRegistry<DeploymentProvider[]>;
export type ApplicationAspectConfig = {
/**
* envs ids to load app types.
*/
envs?: string[];
};
/**
* Application meta data that is stored on the component on load if it's an application.
*/
export type ApplicationMetadata = {
appName: string;
type?: string;
};
export type ServeAppOptions = {
/**
* default port range used to serve applications.
*/
defaultPortRange?: [start: number, end: number];
/**
* determine whether to start the application in dev mode.
*/
dev: boolean;
/**
* actively watch and compile the workspace (like the bit watch command)
* @default true
*/
watch?: boolean;
/**
* determine whether to start the application in server side mode.
* @default false
*/
ssr?: boolean;
/**
* exact port to run the app
*/
port?: number;
/**
* arguments passing to the app.
*/
args?: string;
};
export class ApplicationMain {
constructor(
private appSlot: ApplicationSlot,
// TODO unused
private appTypeSlot: ApplicationTypeSlot,
private deploymentProviderSlot: DeploymentProviderSlot,
private config: ApplicationAspectConfig,
private envs: EnvsMain,
private componentAspect: ComponentMain,
private appService: AppService,
private aspectLoader: AspectLoaderMain,
private workspace: Workspace,
private logger: Logger,
private watcher: WatcherMain,
private harmony: Harmony
) {}
/**
* register a new app.
*/
registerApp(app: Application) {
this.appSlot.register([app]);
return this;
}
/**
* list all registered apps.
*/
listApps(): Application[] {
return flatten(this.appSlot.values());
}
async listAppsIdsAndNames(): Promise<{ id: string; name: string }[]> {
await this.loadAllAppsAsAspects();
const appComponents = this.mapApps();
return appComponents.flatMap(([id, apps]) => {
return apps.map((app) => {
if (!app.name) throw new BitError(`app ${id.toString()} is missing a name`);
return { id, name: app.name };
});
});
}
/**
* map all apps by component ID.
*/
mapApps() {
return this.appSlot.toArray();
}
/**
* instead of adding apps to workspace.jsonc, this method gets all apps components and load them as aspects so then
* they could register to the apps slots and be available to list/run etc.
* if poolIds is provided, it will load only the apps that are part of the pool.
*/
async loadAllAppsAsAspects(poolIds?: ComponentID[]): Promise<ComponentID[]> {
const apps = await this.listAppsComponents(poolIds);
if (!apps.length) return [];
// do not load apps that their env was not loaded yet. their package-json may not be up to date. e.g. it could be
// cjs, when the env needs it as esm. once it is loaded, node.js saved the package.json in the cache with no way to
// refresh it.
const appsWithEnvLoaded = apps.filter((app) => !app.state.issues.getIssueByName('NonLoadedEnv'));
if (apps.length !== appsWithEnvLoaded.length) {
this.logger.warn(`some apps were not loaded as aspects because their env was not loaded yet`);
}
const appIds = appsWithEnvLoaded.map((app) => app.id);
await this.componentAspect.getHost().loadAspects(appIds.map((id) => id.toString()));
return appIds;
}
/**
* list apps by a component id.
* make sure to call `this.loadAllAppsAsAspects` before calling this method in case the app is not listed in workspace.jsonc
*/
listAppsById(id?: ComponentID): Application[] | undefined {
if (!id) return undefined;
return this.appSlot.get(id.toString());
}
/**
* get an application by a component id.
* make sure to call `this.loadAllAppsAsAspects` before calling this method in case the app is not listed in workspace.jsonc
*/
async getAppById(id: ComponentID) {
const apps = await this.listAppsById(id);
if (!apps) return undefined;
return head(apps);
}
/**
* calculate an application by a component.
* This should be only used during the on component load slot
*/
calculateAppByComponent(component: Component) {
const apps = this.appSlot.get(component.id.toString());
if (!apps) return undefined;
return head(apps);
}
listAppTypes() {
return flatten(this.appTypeSlot.values());
}
/**
* @deprecated use `listAppsComponents` instead.
* @returns
*/
async listAppsFromComponents(): Promise<Component[]> {
return this.listAppsComponents();
}
/**
* list all components that are apps.
* if poolIds is provided, it will load only the apps that are part of the pool.
*/
async listAppsComponents(poolIds?: ComponentID[]): Promise<Component[]> {
const host = this.workspace || this.componentAspect.getHost();
if (!host) return [];
const components = poolIds
? this.workspace
? await this.workspace.getMany(poolIds, {
loadExtensions: true,
executeLoadSlot: true,
loadSeedersAsAspects: true,
})
: await host.getMany(poolIds)
: await host.list();
const appTypesPatterns = this.getAppPatterns();
const appsComponents = components.filter((component) => this.hasAppTypePattern(component, appTypesPatterns));
return appsComponents;
}
private hasAppTypePattern(component: Component, appTypesPatterns?: string[]): boolean {
const patterns = appTypesPatterns || this.getAppPatterns();
// has app plugin from registered types.
const files = component.filesystem.byGlob(patterns);
return !!files.length;
}
getAppPatterns() {
const appTypes = this.listAppTypes();
const appTypesPatterns = appTypes.map((appType) => {
return this.getAppPattern(appType);
});
return appTypesPatterns.concat(BIT_APP_PATTERN);
}
async loadApps(): Promise<Application[]> {
const apps = await this.listAppsComponents();
const appTypesPatterns = this.getAppPatterns();
const pluginsToLoad = apps.flatMap((appComponent) => {
const files = appComponent.filesystem.byGlob(appTypesPatterns);
return files.map((file) => file.path);
});
// const app = require(appPath);
const appManifests = Promise.all(
compact(
pluginsToLoad.map(async (pluginPath) => {
try {
const isModule = await this.aspectLoader.isEsmModule(pluginPath);
if (isModule) {
const appManifest = await this.aspectLoader.loadEsm(pluginPath);
return appManifest;
}
// eslint-disable-next-line
const appManifest = require(pluginPath)?.default;
return appManifest;
} catch {
this.logger.error(`failed loading app manifest: ${pluginPath}`);
return undefined;
}
})
)
);
return appManifests;
}
async loadAppsFromComponent(component: Component, rootDir: string): Promise<Application[] | undefined> {
const appTypesPatterns = this.getAppPatterns();
const isApp = this.hasAppTypePattern(component, appTypesPatterns);
if (!isApp) return undefined;
const allPluginDefs = this.aspectLoader.getPluginDefs();
const appsPluginDefs = allPluginDefs.filter((pluginDef) => {
return appTypesPatterns.includes(pluginDef.pattern.toString());
});
// const fileResolver = this.aspectLoader.pluginFileResolver(component, rootDir);
const plugins = this.aspectLoader.getPluginsFromDefs(component, rootDir, appsPluginDefs);
let loadedPlugins;
if (plugins.has()) {
loadedPlugins = await plugins.load(MainRuntime.name);
await this.aspectLoader.loadExtensionsByManifests([loadedPlugins], { seeders: [component.id.toString()] });
}
const listAppsById = this.listAppsById(component.id);
return listAppsById;
}
/**
* get an app.
* make sure to call `this.loadAllAppsAsAspects` before calling this method in case the app is not listed in workspace.jsonc
*/
getApp(appName: string, id?: ComponentID): Application | undefined {
const apps = id ? this.listAppsById(id) : this.listApps();
if (!apps) return undefined;
return apps.find((app) => app.name === appName);
}
getAppByNameOrId(appNameOrId: string): Application | undefined {
const byName = this.getApp(appNameOrId);
if (byName) return byName;
const byId = this.appSlot.get(appNameOrId);
if (!byId || !byId.length) return undefined;
if (byId.length > 1) {
throw new BitError(
`unable to figure out what app to retrieve. the id "${appNameOrId}" has more than one app. please use the app-name`
);
}
return byId[0];
}
getAppPattern(appType: ApplicationType<unknown>) {
if (appType.globPattern) return appType.globPattern;
return `*.${appType.name}.*`;
}
/**
* registers a new app and sets a plugin for it.
*/
registerAppType<T>(...appTypes: Array<ApplicationType<T>>) {
const plugins = appTypes.map((appType) => {
return new AppTypePlugin(this.getAppPattern(appType), appType, this.appSlot);
});
this.aspectLoader.registerPlugins(plugins);
this.appTypeSlot.register(appTypes);
return this;
}
/**
* get an app AspectId.
*/
getAppAspect(appName: string): string | undefined {
return this.appSlot.toArray().find(([, apps]) => apps.find((app) => app.name === appName))?.[0];
}
/**
* get app to throw.
* make sure to call `this.loadAllAppsAsAspects` before calling this method in case the app is not listed in workspace.jsonc
*/
getAppOrThrow(appName: string): Application {
const app = this.getAppByNameOrId(appName);
if (!app) throw new AppNotFound(appName);
return app;
}
defaultOpts: ServeAppOptions = {
dev: false,
ssr: false,
watch: true,
defaultPortRange: [3100, 3500],
};
private computeOptions(opts: Partial<ServeAppOptions> = {}) {
return {
...this.defaultOpts,
...opts,
};
}
async loadAppsToSlot() {
const apps = await this.loadApps();
this.appSlot.register(apps);
return this;
}
/**
* run an app.
* make sure to call `this.loadAllAppsAsAspects` before calling this method in case the app is not listed in workspace.jsonc
*/
async runApp(
appName: string,
options?: ServeAppOptions
): Promise<{
app: Application;
port: number | undefined;
errors?: Error[];
isOldApi: boolean;
}> {
await this.workspace.setComponentPathsRegExps();
options = this.computeOptions(options);
const app = this.getAppOrThrow(appName);
const context = await this.createAppContext(app.name, options.port, options.args);
if (!context) throw new AppNotFound(appName);
const instance = await app.run(context);
if (options.watch) {
this.watcher
.watch({
preCompile: false,
compile: true,
})
.catch((err) => {
// don't throw an error, we don't want to break the "run" process
this.logger.error(`compilation failed`, err);
});
}
const isOldApi = typeof instance === 'number';
const port = isOldApi ? instance : instance?.port;
return { app, port, errors: undefined, isOldApi };
}
/**
* get the component ID of a certain app.
*/
async getAppIdOrThrow(appName: string) {
const maybeApp = this.appSlot.toArray().find(([, apps]) => {
return apps.find((app) => app.name === appName);
});
if (!maybeApp) throw new AppNotFound(appName);
const host = this.componentAspect.getHost();
return host.resolveComponentId(maybeApp[0]);
}
private async createAppContext(appName: string, port?: number, args?: string): Promise<AppContext> {
const host = this.componentAspect.getHost();
// const components = await host.list();
const id = await this.getAppIdOrThrow(appName);
// const component = components.find((c) => c.id.isEqual(id));
const component = await host.get(id);
if (!component) throw new AppNotFound(appName);
const env = await this.envs.createEnvironment([component]);
const res = await env.run(this.appService);
const context = res.results[0].data;
if (!context) throw new AppNotFound(appName);
const hostRootDir = await this.workspace.getComponentPackagePath(component);
const workspaceComponentDir = this.workspace.componentDir(component.id);
const appContext = new AppContext(
appName,
this.harmony,
context.dev,
component,
this.workspace.path,
context,
hostRootDir,
port,
args,
workspaceComponentDir
);
return appContext;
}
async createAppBuildContext(id: ComponentID, appName: string, capsuleRootDir: string, rootDir?: string) {
const host = this.componentAspect.getHost();
// const components = await host.list();
// const component = components.find((c) => c.id.isEqual(id));
const component = await host.get(id);
if (!component) throw new AppNotFound(appName);
const env = await this.envs.createEnvironment([component]);
const res = await env.run(this.appService);
const context = res.results[0].data;
if (!context) throw new AppNotFound(appName);
const appContext = new AppContext(
appName,
this.harmony,
context.dev,
component,
capsuleRootDir,
context,
rootDir,
undefined,
undefined,
undefined,
undefined
);
return appContext;
}
static runtime = MainRuntime;
static dependencies = [
CLIAspect,
LoggerAspect,
BuilderAspect,
EnvsAspect,
ComponentAspect,
AspectLoaderAspect,
WorkspaceAspect,
WatcherAspect,
ScopeAspect,
];
static slots = [
Slot.withType<ApplicationType<unknown>[]>(),
Slot.withType<Application[]>(),
Slot.withType<DeploymentProvider[]>(),
];
static async provider(
[cli, loggerAspect, builder, envs, component, aspectLoader, workspace, watcher, scope]: [
CLIMain,
LoggerMain,
BuilderMain,
EnvsMain,
ComponentMain,
AspectLoaderMain,
Workspace,
WatcherMain,
ScopeMain,
],
config: ApplicationAspectConfig,
[appTypeSlot, appSlot, deploymentProviderSlot]: [ApplicationTypeSlot, ApplicationSlot, DeploymentProviderSlot],
harmony: Harmony
) {
const logger = loggerAspect.createLogger(ApplicationAspect.id);
const appService = new AppService();
const application = new ApplicationMain(
appSlot,
appTypeSlot,
deploymentProviderSlot,
config,
envs,
component,
appService,
aspectLoader,
workspace,
logger,
watcher,
harmony
);
appService.registerAppType = application.registerAppType.bind(application);
const appCmd = new AppCmd(application);
appCmd.commands = [new AppListCmd(application), new RunCmd(application, logger)];
aspectLoader.registerPlugins([new AppPlugin(appSlot)]);
builder.registerBuildTasks([new AppsBuildTask(application), new PlatformAppsBuildTask(application)]);
builder.registerSnapTasks([new DeployTask(application, builder)]);
builder.registerTagTasks([new DeployTask(application, builder)]);
envs.registerService(appService);
cli.registerGroup('apps', 'Applications');
cli.register(new RunCmd(application, logger), appCmd);
// cli.registerOnStart(async () => {
// await application.loadAppsToSlot();
// });
const calcAppOnLoad = async (loadedComponent): Promise<ApplicationMetadata | undefined> => {
const app = application.calculateAppByComponent(loadedComponent);
if (!app) return undefined;
return {
appName: app.name,
type: app.applicationType,
};
};
if (workspace) {
workspace.registerOnComponentLoad(calcAppOnLoad);
}
if (scope) {
scope.registerOnCompAspectReCalc(calcAppOnLoad);
}
return application;
}
}
ApplicationAspect.addRuntime(ApplicationMain);