This repository was archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsky-pages-module-generator.js
150 lines (128 loc) · 3.74 KB
/
sky-pages-module-generator.js
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
147
148
149
150
/*jshint node: true*/
'use strict';
const componentGenerator = require('./sky-pages-component-generator');
const routeGenerator = require('./sky-pages-route-generator');
/**
* Generates the source necessary to register all routes + components.
* Declared in order to satisfy jshint.
* @name getSource
* @returns {string} source
*/
function getSource(skyAppConfig) {
// Generate these first so we can check for 404 route
const components = componentGenerator.getComponents(skyAppConfig);
const componentNames = components.names;
// Should we add the 404 route
if (componentNames.indexOf('NotFoundComponent') === -1) {
skyAppConfig.runtime.handle404 = true;
}
const routes = routeGenerator.getRoutes(skyAppConfig);
const names = `${componentNames.concat(routes.names).join(',\n ')}`;
skyAppConfig.runtime.routes = routes.routesForConfig;
const skyAppConfigAsString = JSON.stringify(skyAppConfig);
let runtimeImports = [
'SkyAppBootstrapper',
'SkyAppConfig',
'SkyAppWindowRef',
'SkyAppStyleLoader',
'SkyAuthTokenProvider'
];
let runtimeProviders = [
'SkyAppWindowRef',
'SkyAppStyleLoader',
'SkyAuthTokenProvider',
`{
provide: SkyAppConfig,
useValue: ${skyAppConfigAsString}
}`
];
if (skyAppConfig.skyux.auth) {
runtimeImports.push(`SkyAuthHttp`);
runtimeProviders.push(`{
provide: SkyAuthHttp,
useClass: SkyAuthHttp,
deps: [XHRBackend, RequestOptions, SkyAppWindowRef, SkyAuthTokenProvider]
}`);
}
let runtimeModuleImports = [
'CommonModule',
'HttpModule',
'FormsModule',
'ReactiveFormsModule',
'SkyModule',
'AppExtrasModule'
];
if (skyAppConfig.runtime.includeRouteModule) {
runtimeModuleImports.push('routing');
runtimeProviders.push('appRoutingProviders');
} else {
/*
Import the regular RouterModule so that tested components can reference things
like routerLink
*/
runtimeModuleImports.push('RouterModule');
}
let enableProdMode = ``;
let useMockAuth = ``;
switch (skyAppConfig.runtime.command) {
case 'build':
enableProdMode =
`import { enableProdMode } from '@angular/core';
enableProdMode();`;
break;
case 'e2e':
useMockAuth =
`import { BBAuth } from '@blackbaud/auth-client';
BBAuth.mock = true;
`;
break;
}
let moduleSource =
`${useMockAuth}
import '${skyAppConfig.runtime.skyPagesOutAlias}/src/main';
import {
Component,
Inject,
NgModule,
OnInit,
OnDestroy,
OpaqueToken
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpModule, XHRBackend, RequestOptions } from '@angular/http';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ActivatedRoute, RouterModule, Routes } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';
import { SkyModule } from '${skyAppConfig.runtime.skyuxPathAlias}/core';
import {
AppExtrasModule
} from '${skyAppConfig.runtime.skyPagesOutAlias}/src/app/app-extras.module';
import { ${runtimeImports.join(', ')} } from '${skyAppConfig.runtime.runtimeAlias}';
// Setting skyux config as static property exclusively for Bootstrapper
SkyAppBootstrapper.config = ${JSON.stringify(skyAppConfig.skyux)};
${components.imports}
${routes.definitions}
// Routes need to be defined after their corresponding components
const appRoutingProviders: any[] = [];
const routes: Routes = ${routes.declarations};
const routing = RouterModule.forRoot(routes);
${enableProdMode}
@NgModule({
declarations: [
${names}
],
imports: [
${runtimeModuleImports.join()}
],
exports: [
${names}
],
providers: [
${runtimeProviders.join()}
]
}) export class SkyPagesModule { }`;
return moduleSource;
}
module.exports = {
getSource: getSource
};