/
update-host-with-remote.ts
97 lines (92 loc) · 2.63 KB
/
update-host-with-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
import {
applyChangesToString,
joinPathFragments,
logger,
names,
readProjectConfiguration,
Tree,
} from '@nrwl/devkit';
import {
addRemoteDefinition,
addRemoteRoute,
addRemoteToMfeConfig,
} from '../../../mfe/mfe-ast-utils';
import * as ts from 'typescript';
export function updateHostWithRemote(
host: Tree,
hostName: string,
remoteName: string
) {
const hostConfig = readProjectConfiguration(host, hostName);
const mfeConfigPath = joinPathFragments(hostConfig.root, 'mfe.config.js');
const remoteDefsPath = joinPathFragments(
hostConfig.sourceRoot,
'remotes.d.ts'
);
const appComponentPath = findAppComponentPath(host, hostConfig.sourceRoot);
if (host.exists(mfeConfigPath)) {
// find the host project path
// Update remotes inside ${host_path}/src/remotes.d.ts
let sourceCode = host.read(mfeConfigPath).toString();
const source = ts.createSourceFile(
mfeConfigPath,
sourceCode,
ts.ScriptTarget.Latest,
true
);
host.write(
mfeConfigPath,
applyChangesToString(sourceCode, addRemoteToMfeConfig(source, remoteName))
);
} else {
// TODO(jack): Point to the nx.dev guide when ready.
logger.warn(
`Could not find MFE configuration at ${mfeConfigPath}. Did you generate this project with "@nrwl/react:mfe-host"?`
);
}
if (host.exists(remoteDefsPath)) {
let sourceCode = host.read(remoteDefsPath).toString();
const source = ts.createSourceFile(
mfeConfigPath,
sourceCode,
ts.ScriptTarget.Latest,
true
);
host.write(
remoteDefsPath,
applyChangesToString(sourceCode, addRemoteDefinition(source, remoteName))
);
} else {
logger.warn(
`Could not find remote definitions at ${remoteDefsPath}. Did you generate this project with "@nrwl/react:mfe-host"?`
);
}
if (host.exists(appComponentPath)) {
let sourceCode = host.read(appComponentPath).toString();
const source = ts.createSourceFile(
mfeConfigPath,
sourceCode,
ts.ScriptTarget.Latest,
true
);
host.write(
appComponentPath,
applyChangesToString(
sourceCode,
addRemoteRoute(source, names(remoteName))
)
);
} else {
logger.warn(
`Could not find app component at ${appComponentPath}. Did you generate this project with "@nrwl/react:mfe-host"?`
);
}
}
function findAppComponentPath(host: Tree, sourceRoot: string) {
const locations = ['app/app.tsx', 'app/App.tsx', 'app.tsx', 'App.tsx'];
for (const loc of locations) {
if (host.exists(joinPathFragments(sourceRoot, loc))) {
return joinPathFragments(sourceRoot, loc);
}
}
}