-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
remote.ts
108 lines (93 loc) · 3 KB
/
remote.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
import {
getProjects,
joinPathFragments,
readProjectConfiguration,
Tree,
} from '@nrwl/devkit';
import type { Schema } from './schema';
import applicationGenerator from '../application/application';
import { getMFProjects } from '../../utils/get-mf-projects';
import { normalizeProjectName } from '../utils/project';
function findNextAvailablePort(tree: Tree) {
const mfeProjects = getMFProjects(tree);
const ports = new Set<number>([4200]);
for (const mfeProject of mfeProjects) {
const { targets } = readProjectConfiguration(tree, mfeProject);
const port = targets?.serve?.options?.port ?? 4200;
ports.add(port);
}
const nextAvailablePort = Math.max(...ports) + 1;
return nextAvailablePort;
}
export default async function remote(tree: Tree, options: Schema) {
const projects = getProjects(tree);
if (options.host && !projects.has(options.host)) {
throw new Error(
`The name of the application to be used as the host app does not exist. (${options.host})`
);
}
const installTask = await applicationGenerator(tree, {
...options,
mfe: true,
mfeType: 'remote',
routing: true,
host: options.host,
port: options.port ?? findNextAvailablePort(tree),
});
removeDeadCode(tree, options);
return installTask;
}
function removeDeadCode(tree: Tree, options: Schema) {
const projectName = normalizeProjectName(options.name, options.directory);
const project = readProjectConfiguration(tree, projectName);
['css', 'less', 'scss', 'sass'].forEach((style) => {
const pathToComponentStyle = joinPathFragments(
project.sourceRoot,
`app/app.component.${style}`
);
if (tree.exists(pathToComponentStyle)) {
tree.delete(pathToComponentStyle);
}
});
tree.rename(
joinPathFragments(project.sourceRoot, 'app/nx-welcome.component.ts'),
joinPathFragments(
project.sourceRoot,
'app/remote-entry/nx-welcome.component.ts'
)
);
tree.delete(
joinPathFragments(project.sourceRoot, 'app/app.component.spec.ts')
);
tree.delete(joinPathFragments(project.sourceRoot, 'app/app.component.html'));
const pathToComponent = joinPathFragments(
project.sourceRoot,
'app/app.component.ts'
);
const component =
tree.read(pathToComponent, 'utf-8').split('templateUrl')[0] +
`template: '<router-outlet></router-outlet>'
})
export class AppComponent {}`;
tree.write(pathToComponent, component);
tree.write(
joinPathFragments(project.sourceRoot, 'app/app.module.ts'),
`import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
RouterModule.forRoot([{
path: '',
loadChildren: () => import('./remote-entry/entry.module').then(m => m.RemoteEntryModule)
}], { initialNavigation: 'enabledBlocking' }),
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}`
);
}