-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-responsive.ts.ts
229 lines (203 loc) · 7.55 KB
/
index-responsive.ts.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { strings } from '@angular-devkit/core';
import {
apply,
template,
branchAndMerge,
chain,
forEach,
FileEntry,
mergeWith,
move,
Rule,
SchematicsException,
Tree,
url
} from '@angular-devkit/schematics';
import { FileSystemSchematicContext } from '@angular-devkit/schematics/tools';
import { InsertChange } from '@schematics/angular/utility/change';
import { getWorkspace } from '@schematics/angular/utility/config';
import {
buildRelativePath,
findModule,
MODULE_EXT,
ROUTING_MODULE_EXT
} from '@schematics/angular/utility/find-module';
import { parseName } from '@schematics/angular/utility/parse-name';
import { buildDefaultPath } from '@schematics/angular/utility/project';
import { getProjectFromWorkspace } from '@angular/cdk/schematics/utils/get-project';
import { appendHtmlElementToHead } from '@angular/cdk/schematics/utils/html-head-element';import {
addDeclarationToModule,
addProviderToModule
} from './vendored-ast-utils';
import {
appendHtmlElementToBody,
appendToStartFile
} from './cap-utils';
import { Schema as ComponentOptions } from './schema';
import * as ts from 'typescript';
function updateIndexFile(path: string): Rule {
return (host: Tree) => {
/** Appends the given element HTML fragment to the `<head>` element of the specified HTML file. */
[
'<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500&display=optional" rel="stylesheet" async defer>',
'<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i,800,800i&display=optional" rel="stylesheet" async defer>',
'<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" async defer>',
'<link rel="stylesheet" href="assets/css/bootstrap.min.css" async defer>',
'<script src="assets/js/jquery-latest.min.js" async defer></script>',
].map((element: string) => {
appendHtmlElementToHead(host, path, element);
});
/** Appends the given element HTML fragment to the `<body>` element of the specified HTML file after open tag. */
appendHtmlElementToBody(host, path, '<div class="container-fluid p-0">', 'left');
/** Appends the given element HTML fragment to the `<body>` element of the specified HTML file before close tag. */
appendHtmlElementToBody(host, path, '</div>', 'right');
return host;
};
}
function appendToAppComponentFile(path: string): Rule {
return (host: Tree) => {
const content = `<app-header></app-header>`;
appendToStartFile(host, path, content);
return host;
};
}
function appendToStylesFile(path: string): Rule {
return (host: Tree) => {
const content = `
// Webslidemenu imports
@import "./assets/webslidemenu/dropdown-effects/fade-down.css";
@import "./assets/webslidemenu/webslidemenu.css";
body {
background-color: #333333;
color: #f2f2f2;
}
app-header {
height: 103px;
display: block;
@media (min-width: 1200px) {}
@media (min-width: 992px) and (max-width: 1199px) {}
@media (min-width: 576px) and (max-width: 991px) {
height: 54px;
}
@media (max-width: 575px) {
height: 54px;
}
}
router-outlet {
padding-bottom: 80px;
}
`;
appendToStartFile(host, path, content);
return host;
};
}
function readIntoSourceFile(host: Tree, modulePath: string) {
const text = host.read(modulePath);
if (text === null) {
throw new SchematicsException(`File ${modulePath} does not exist.`);
}
return ts.createSourceFile(modulePath, text.toString('utf-8'), ts.ScriptTarget.Latest, true);
}
function addDeclarationToNgModule(options: ComponentOptions): Rule {
return (host: Tree) => {
const modulePath = options.module;
// Import Header Component and declare
let source = readIntoSourceFile(host, modulePath);
const componentPath = `${options.path}/app/header/header.component`;
const relativePath = buildRelativePath(modulePath, componentPath);
const classifiedName = strings.classify(`HeaderComponent`);
const declarationChanges: any = addDeclarationToModule(
source,
modulePath,
classifiedName,
relativePath);
const declarationRecorder = host.beginUpdate(modulePath);
for (const change of declarationChanges) {
if (change instanceof InsertChange) {
declarationRecorder.insertLeft(change.pos, change.toAdd);
}
}
host.commitUpdate(declarationRecorder);
// Import and include on Providers the load script ScriptService
if (options) {
// Need to refresh the AST because we overwrote the file in the host.
source = readIntoSourceFile(host, modulePath);
const servicePath = `${options.path}/app/shared/services/load-scripts.service`;
const relativePath = buildRelativePath(modulePath, servicePath);
const classifiedName = strings.classify(`ScriptService`);
const providerRecorder = host.beginUpdate(modulePath);
const providerChanges: any = addProviderToModule(
source,
modulePath,
classifiedName,
relativePath);
for (const change of providerChanges) {
if (change instanceof InsertChange) {
providerRecorder.insertLeft(change.pos, change.toAdd);
}
}
host.commitUpdate(providerRecorder);
}
return host;
};
}
export function schematicsPWA(options: ComponentOptions): Rule {
return (host: Tree, context: FileSystemSchematicContext) => {
const workspace = getWorkspace(host);
const project = getProjectFromWorkspace(workspace, options.project);
if (!project) {
throw new SchematicsException(`Project is not defined in this workspace.`);
}
if (options.path === undefined) {
options.path = buildDefaultPath(project);
}
options.module = findModule(host, options.path, 'app' + MODULE_EXT, ROUTING_MODULE_EXT);
options.name = '';
const parsedPath = parseName(options.path!, options.name);
options.name = parsedPath.name;
options.path = parsedPath.path;
// Get Index
if (!options.project) {
throw new SchematicsException('Option "project" is required.');
}
const projectType: string = project.projectType || project.projects[options.project].projectType;
if (projectType !== 'application') {
throw new SchematicsException(`Is required a project type of "application".`);
}
// Get the index path
const index = project.architect.build.options.index || `src/index.html`;
// Get the styles.scss file
const styles = `src/styles.scss`;
// Get the app.component file
const appComponent = `src/app/app.component.html`;
const files: any = {
index: index,
styles: styles,
appComponent: appComponent
}
// Object that will be used as context for the EJS templates.
const baseTemplateContext = {
...strings,
...options,
};
const templateSource = apply(url('./files'), [
template(baseTemplateContext),
move(null as any, parsedPath.path),
forEach((fileEntry: FileEntry) => {
if (host.exists(fileEntry.path)) {
host.overwrite(fileEntry.path, fileEntry.content);
}
return fileEntry;
})
]);
return chain([
branchAndMerge(chain([
addDeclarationToNgModule(options),
mergeWith(templateSource),
updateIndexFile(files.index),
appendToStylesFile(files.styles),
appendToAppComponentFile(files.appComponent),
])),
])(host, context);
};
}