-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
host.ts
115 lines (100 loc) · 2.68 KB
/
host.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
import {
formatFiles,
getProjects,
joinPathFragments,
names,
readProjectConfiguration,
Tree,
} from '@nrwl/devkit';
import type { Schema } from './schema';
import applicationGenerator from '../application/application';
import remoteGenerator from '../remote/remote';
import { normalizeProjectName } from '../utils/project';
import * as ts from 'typescript';
import { addRoute } from '../../utils/nx-devkit/ast-utils';
export default async function host(tree: Tree, options: Schema) {
const projects = getProjects(tree);
const remotesToGenerate: string[] = [];
const remotesToIntegrate: string[] = [];
if (options.remotes && options.remotes.length > 0) {
options.remotes.forEach((remote) => {
if (!projects.has(remote)) {
remotesToGenerate.push(remote);
} else {
remotesToIntegrate.push(remote);
}
});
}
const installTask = await applicationGenerator(tree, {
...options,
mfe: true,
mfeType: 'host',
routing: true,
remotes: remotesToIntegrate ?? [],
port: 4200,
federationType: options.dynamic ? 'dynamic' : 'static',
skipFormat: true,
});
for (const remote of remotesToGenerate) {
await remoteGenerator(tree, {
...options,
name: remote,
host: normalizeProjectName(options.name, options.directory),
skipFormat: true,
});
}
routeToNxWelcome(tree, options);
if (!options.skipFormat) {
await formatFiles(tree);
}
return installTask;
}
function routeToNxWelcome(tree: Tree, options: Schema) {
const { sourceRoot } = readProjectConfiguration(
tree,
normalizeProjectName(options.name, options.directory)
);
const remoteRoutes =
options.remotes && Array.isArray(options.remotes)
? options.remotes.reduce(
(routes, remote) =>
`${routes}\n<li><a routerLink='${normalizeProjectName(
remote,
options.directory
)}'>${names(remote).className}</a></li>`,
''
)
: '';
tree.write(
joinPathFragments(sourceRoot, 'app/app.component.html'),
`<ul class="remote-menu">
<li><a routerLink='/'>Home</a></li>
${remoteRoutes}
</ul>
<router-outlet></router-outlet>
`
);
const pathToHostAppModule = joinPathFragments(
sourceRoot,
'app/app.module.ts'
);
const hostAppModule = tree.read(pathToHostAppModule, 'utf-8');
if (!hostAppModule.includes('RouterModule.forRoot(')) {
return;
}
let sourceFile = ts.createSourceFile(
pathToHostAppModule,
hostAppModule,
ts.ScriptTarget.Latest,
true
);
sourceFile = addRoute(
tree,
pathToHostAppModule,
sourceFile,
`{
path: '',
component: NxWelcomeComponent
}`
);
}