/
host.ts
146 lines (126 loc) · 3.47 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
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
import {
formatFiles,
generateFiles,
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/route-utils';
import { setupMf } from '../setup-mf/setup-mf';
import { E2eTestRunner } from '../../utils/test-runners';
export 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 appName = normalizeProjectName(options.name, options.directory);
const installTask = await applicationGenerator(tree, {
...options,
routing: true,
port: 4200,
skipFormat: true,
});
const skipE2E =
!options.e2eTestRunner || options.e2eTestRunner === E2eTestRunner.None;
await setupMf(tree, {
appName,
mfType: 'host',
routing: true,
port: 4200,
remotes: remotesToIntegrate ?? [],
federationType: options.dynamic ? 'dynamic' : 'static',
skipPackageJson: options.skipPackageJson,
skipFormat: true,
skipE2E,
e2eProjectName: skipE2E ? undefined : `${appName}-e2e`,
});
for (const remote of remotesToGenerate) {
await remoteGenerator(tree, {
...options,
name: remote,
host: appName,
skipFormat: true,
standalone: options.standalone,
});
}
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 pathToHostRootRoutingFile = joinPathFragments(
sourceRoot,
'app/app.routes.ts'
);
const hostRootRoutingFile = tree.read(pathToHostRootRoutingFile, 'utf-8');
let sourceFile = ts.createSourceFile(
pathToHostRootRoutingFile,
hostRootRoutingFile,
ts.ScriptTarget.Latest,
true
);
addRoute(
tree,
pathToHostRootRoutingFile,
`{
path: '',
component: NxWelcomeComponent
}`
);
tree.write(
pathToHostRootRoutingFile,
`import { NxWelcomeComponent } from './nx-welcome.component';
${tree.read(pathToHostRootRoutingFile, 'utf-8')}`
);
generateFiles(
tree,
joinPathFragments(__dirname, 'files'),
joinPathFragments(sourceRoot, 'app'),
{
appName: normalizeProjectName(options.name, options.directory),
tmpl: '',
}
);
}
export default host;