Skip to content

Commit d499b1b

Browse files
committed
fix(@schematics/angular): Add file generation for service worker schematic
1 parent 59956c3 commit d499b1b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

packages/schematics/angular/service-worker/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import {
1111
SchematicsException,
1212
Tree,
1313
UpdateRecorder,
14+
apply,
1415
chain,
16+
mergeWith,
17+
move,
18+
template,
19+
url,
1520
} from '@angular-devkit/schematics';
1621
import * as ts from 'typescript';
1722
import { addSymbolToNgModuleMetadata, isImported } from '../utility/ast-utils';
@@ -158,7 +163,19 @@ function getTsSourceFile(host: Tree, path: string): ts.SourceFile {
158163

159164
export default function (options: ServiceWorkerOptions): Rule {
160165
return (host: Tree, context: SchematicContext) => {
166+
const workspace = getWorkspace(host);
167+
const project = workspace.projects[options.project];
168+
if (!project) {
169+
throw new SchematicsException(`Invalid project name (${options.project})`);
170+
}
171+
172+
const templateSource = apply(url('./files'), [
173+
template({...options}),
174+
move(project.root),
175+
]);
176+
161177
return chain([
178+
mergeWith(templateSource),
162179
updateConfigFile(options),
163180
addDependencies(),
164181
updateAppModule(options),

packages/schematics/angular/service-worker/index_spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ describe('Service Worker Schematic', () => {
9090
// tslint:disable-next-line:max-line-length
9191
const regex = /ServiceWorkerModule\.register\('\/ngsw-worker.js\', { enabled: environment.production }\)/;
9292
expect(pkgText).toMatch(regex);
93+
});
9394

95+
it('should put the ngsw-config.json file in the project root', () => {
96+
const tree = schematicRunner.runSchematic('service-worker', defaultOptions, appTree);
97+
const path = '/projects/bar/ngsw-config.json';
98+
expect(tree.exists(path)).toEqual(true);
9499
});
95100
});

0 commit comments

Comments
 (0)