Skip to content

Commit d747490

Browse files
cyrilletuzivikerman
authored andcommitted
feat(@schematics/angular): JSON schema for service worker config
1 parent e96afb8 commit d747490

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

packages/schematics/angular/service-worker/files/ngsw-config.json.template

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"$schema": "<%= relativePathToWorkspaceRoot %>/node_modules/@angular/service-worker/config/schema.json",
23
"index": "/index.html",
34
"assetGroups": [
45
{

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,16 @@ export default function (options: ServiceWorkerOptions): Rule {
169169
throw new SchematicsException(`Service worker requires a project type of "application".`);
170170
}
171171

172+
const relativePathToWorkspaceRoot = project.root ?
173+
project.root.split('/').filter(x => x !== '').map(x => '..').join('/') : '.';
174+
172175
let { resourcesOutputPath = '' } = getProjectConfiguration(workspace, options);
173176
if (resourcesOutputPath) {
174177
resourcesOutputPath = '/' + resourcesOutputPath.split('/').filter(x => !!x).join('/');
175178
}
176179

177180
const templateSource = apply(url('./files'), [
178-
applyTemplates({ ...options, resourcesOutputPath }),
181+
applyTemplates({ ...options, resourcesOutputPath, relativePathToWorkspaceRoot }),
179182
move(project.root),
180183
]);
181184

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

+25
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,31 @@ describe('Service Worker Schematic', () => {
101101
.toBe('projects/bar/ngsw-config.json');
102102
});
103103

104+
it('should add $schema in ngsw-config.json with correct relative path', () => {
105+
const pathToNgswConfigSchema = 'node_modules/@angular/service-worker/config/schema.json';
106+
107+
const name = 'foo';
108+
const rootAppOptions: ApplicationOptions = {
109+
...appOptions,
110+
name,
111+
projectRoot: '',
112+
};
113+
const rootSWOptions: ServiceWorkerOptions = {
114+
...defaultOptions,
115+
project: name,
116+
};
117+
const rootAppTree = schematicRunner.runSchematic('application', rootAppOptions, appTree);
118+
const treeInRoot = schematicRunner.runSchematic('service-worker', rootSWOptions, rootAppTree);
119+
const pkgTextInRoot = treeInRoot.readContent('/ngsw-config.json');
120+
const configInRoot = JSON.parse(pkgTextInRoot);
121+
expect(configInRoot.$schema).toBe(`./${pathToNgswConfigSchema}`);
122+
123+
const treeNotInRoot = schematicRunner.runSchematic('service-worker', defaultOptions, appTree);
124+
const pkgTextNotInRoot = treeNotInRoot.readContent('/projects/bar/ngsw-config.json');
125+
const configNotInRoot = JSON.parse(pkgTextNotInRoot);
126+
expect(configNotInRoot.$schema).toBe(`../../${pathToNgswConfigSchema}`);
127+
});
128+
104129
it('should add root assets RegExp', () => {
105130
const tree = schematicRunner.runSchematic('service-worker', defaultOptions, appTree);
106131
const pkgText = tree.readContent('/projects/bar/ngsw-config.json');

0 commit comments

Comments
 (0)