-
Notifications
You must be signed in to change notification settings - Fork 12k
/
index.ts
192 lines (163 loc) · 6.1 KB
/
index.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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
Rule,
SchematicContext,
SchematicsException,
Tree,
UpdateRecorder,
apply,
chain,
mergeWith,
move,
template,
url,
} from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import * as ts from 'typescript';
import { addSymbolToNgModuleMetadata, insertImport, isImported } from '../utility/ast-utils';
import { InsertChange } from '../utility/change';
import {
getWorkspace,
getWorkspacePath,
} from '../utility/config';
import { getAppModulePath } from '../utility/ng-ast-utils';
import { Schema as ServiceWorkerOptions } from './schema';
const packageJsonPath = '/package.json';
function updateConfigFile(options: ServiceWorkerOptions): Rule {
return (host: Tree, context: SchematicContext) => {
context.logger.debug('updating config file.');
const workspacePath = getWorkspacePath(host);
const workspace = getWorkspace(host);
const project = workspace.projects[options.project as string];
if (!project) {
throw new Error(`Project is not defined in this workspace.`);
}
if (!project.architect) {
throw new Error(`Architect is not defined for this project.`);
}
if (!project.architect[options.target]) {
throw new Error(`Target is not defined for this project.`);
}
let applyTo = project.architect[options.target].options;
if (options.configuration &&
project.architect[options.target].configurations &&
project.architect[options.target].configurations[options.configuration]) {
applyTo = project.architect[options.target].configurations[options.configuration];
}
applyTo.serviceWorker = true;
host.overwrite(workspacePath, JSON.stringify(workspace, null, 2));
return host;
};
}
function addDependencies(): Rule {
return (host: Tree, context: SchematicContext) => {
const packageName = '@angular/service-worker';
context.logger.debug(`adding dependency (${packageName})`);
const buffer = host.read(packageJsonPath);
if (buffer === null) {
throw new SchematicsException('Could not find package.json');
}
const packageObject = JSON.parse(buffer.toString());
const ngCoreVersion = packageObject.dependencies['@angular/core'];
packageObject.dependencies[packageName] = ngCoreVersion;
host.overwrite(packageJsonPath, JSON.stringify(packageObject, null, 2));
return host;
};
}
function updateAppModule(options: ServiceWorkerOptions): Rule {
return (host: Tree, context: SchematicContext) => {
context.logger.debug('Updating appmodule');
// find app module
const workspace = getWorkspace(host);
const project = workspace.projects[options.project as string];
if (!project.architect) {
throw new Error('Project architect not found.');
}
const mainPath = project.architect.build.options.main;
const modulePath = getAppModulePath(host, mainPath);
context.logger.debug(`module path: ${modulePath}`);
// add import
let moduleSource = getTsSourceFile(host, modulePath);
let importModule = 'ServiceWorkerModule';
let importPath = '@angular/service-worker';
if (!isImported(moduleSource, importModule, importPath)) {
const change = insertImport
(moduleSource, modulePath, importModule, importPath);
if (change) {
const recorder = host.beginUpdate(modulePath);
recorder.insertLeft((change as InsertChange).pos, (change as InsertChange).toAdd);
host.commitUpdate(recorder);
}
}
// add import for environments
// import { environment } from '../environments/environment';
moduleSource = getTsSourceFile(host, modulePath);
importModule = 'environment';
// TODO: dynamically find environments relative path
importPath = '../environments/environment';
if (!isImported(moduleSource, importModule, importPath)) {
const change = insertImport
(moduleSource, modulePath, importModule, importPath);
if (change) {
const recorder = host.beginUpdate(modulePath);
recorder.insertLeft((change as InsertChange).pos, (change as InsertChange).toAdd);
host.commitUpdate(recorder);
}
}
// register SW in app module
const importText =
`ServiceWorkerModule.register('/ngsw-worker.js', { enabled: environment.production })`;
moduleSource = getTsSourceFile(host, modulePath);
const metadataChanges = addSymbolToNgModuleMetadata(
moduleSource, modulePath, 'imports', importText);
if (metadataChanges) {
const recorder = host.beginUpdate(modulePath);
metadataChanges.forEach((change: InsertChange) => {
recorder.insertRight(change.pos, change.toAdd);
});
host.commitUpdate(recorder);
}
return host;
};
}
function getTsSourceFile(host: Tree, path: string): ts.SourceFile {
const buffer = host.read(path);
if (!buffer) {
throw new SchematicsException(`Could not read file (${path}).`);
}
const content = buffer.toString();
const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true);
return source;
}
export default function (options: ServiceWorkerOptions): Rule {
return (host: Tree, context: SchematicContext) => {
const workspace = getWorkspace(host);
if (!options.project) {
throw new SchematicsException('Option "project" is required.');
}
const project = workspace.projects[options.project];
if (!project) {
throw new SchematicsException(`Invalid project name (${options.project})`);
}
if (project.projectType !== 'application') {
throw new SchematicsException(`Service worker requires a project type of "application".`);
}
const templateSource = apply(url('./files'), [
template({...options}),
move(project.root),
]);
context.addTask(new NodePackageInstallTask());
return chain([
mergeWith(templateSource),
updateConfigFile(options),
addDependencies(),
updateAppModule(options),
]);
};
}