forked from nrwl/nx
/
convert-to-standalone-app.ts
131 lines (114 loc) · 4.01 KB
/
convert-to-standalone-app.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
import type { Tree } from '@nrwl/devkit';
import { joinPathFragments } from '@nrwl/devkit';
import type { NormalizedSchema } from './normalized-schema';
import { tsquery } from '@phenomnomnominal/tsquery';
export function convertToStandaloneApp(tree: Tree, options: NormalizedSchema) {
const pathToAppModule = joinPathFragments(
options.appProjectRoot,
'src/app/app.module.ts'
);
updateMainEntrypoint(options, tree, pathToAppModule);
updateAppComponent(tree, options);
if (!options.skipTests) {
updateAppComponentSpec(tree, options);
}
tree.delete(pathToAppModule);
}
function updateMainEntrypoint(
options: NormalizedSchema,
tree: Tree,
pathToAppModule: string
) {
let routerModuleSetup: string;
if (options.routing) {
const appModuleContents = tree.read(pathToAppModule, 'utf-8');
const ast = tsquery.ast(appModuleContents);
const ROUTER_MODULE_SELECTOR =
'PropertyAssignment:has(Identifier[name=imports]) CallExpression:has(PropertyAccessExpression > Identifier[name=RouterModule])';
const nodes = tsquery(ast, ROUTER_MODULE_SELECTOR, {
visitAllChildren: true,
});
if (nodes.length > 0) {
routerModuleSetup = nodes[0].getText();
}
}
tree.write(
joinPathFragments(options.appProjectRoot, 'src/main.ts'),
standaloneComponentMainContents(routerModuleSetup)
);
}
const standaloneComponentMainContents = (
routerModuleSetup
) => `import { enableProdMode } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';${
routerModuleSetup
? `
import { provideRouter, withEnabledBlockingInitialNavigation } from '@angular/router'`
: ``
};
import { AppComponent } from './app/app.component';
import { environment } from './environments/environment';
${routerModuleSetup ? `import { appRoutes } from './app/app.routes';` : ''}
if (environment.production) {
enableProdMode();
}
bootstrapApplication(AppComponent${
routerModuleSetup
? `, {
providers: [provideRouter(appRoutes, withEnabledBlockingInitialNavigation())],
}`
: ''
}).catch((err) => console.error(err));`;
function updateAppComponent(tree: Tree, options: NormalizedSchema) {
const pathToAppComponent = joinPathFragments(
options.appProjectRoot,
'src/app/app.component.ts'
);
const appComponentContents = tree.read(pathToAppComponent, 'utf-8');
const ast = tsquery.ast(appComponentContents);
const COMPONENT_DECORATOR_SELECTOR =
'Decorator > CallExpression:has(Identifier[name=Component]) ObjectLiteralExpression';
const nodes = tsquery(ast, COMPONENT_DECORATOR_SELECTOR, {
visitAllChildren: true,
});
if (nodes.length === 0) {
throw new Error(
'Could not find Component decorator within app.component.ts for standalone app generation.'
);
}
const startPos = nodes[0].getStart() + 1;
const newAppComponentContents = `import { NxWelcomeComponent } from './nx-welcome.component';${
options.routing
? `
import { RouterModule } from '@angular/router';`
: ''
}
${appComponentContents.slice(0, startPos)}
standalone: true,
imports: [NxWelcomeComponent${
options.routing ? ', RouterModule' : ''
}],${appComponentContents.slice(startPos, -1)}`;
tree.write(pathToAppComponent, newAppComponentContents);
}
function updateAppComponentSpec(tree: Tree, options: NormalizedSchema) {
const pathToAppComponentSpec = joinPathFragments(
options.appProjectRoot,
'src/app/app.component.spec.ts'
);
const appComponentSpecContents = tree.read(pathToAppComponentSpec, 'utf-8');
let newAppComponentSpecContents: string;
if (!options.routing) {
newAppComponentSpecContents = appComponentSpecContents.replace(
'declarations',
'imports'
);
} else {
newAppComponentSpecContents = appComponentSpecContents
.replace(
'imports: [RouterTestingModule],',
'imports: [AppComponent, NxWelcomeComponent, RouterTestingModule]'
)
.replace('declarations: [AppComponent, NxWelcomeComponent]', '');
}
tree.write(pathToAppComponentSpec, newAppComponentSpecContents);
}