-
Notifications
You must be signed in to change notification settings - Fork 12k
feat(@schematics/angular): add migration for ngsw-config.json
#15443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/schematics/angular/migrations/update-9/ngsw-config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* @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 { JsonParseMode, parseJsonAst } from '@angular-devkit/core'; | ||
import { Rule, SchematicsException, Tree } from '@angular-devkit/schematics'; | ||
import { appendValueInAstArray, findPropertyInAstObject } from '../../utility/json-utils'; | ||
import { Builders } from '../../utility/workspace-models'; | ||
import { getAllOptions, getTargets, getWorkspace } from './utils'; | ||
|
||
|
||
/** | ||
* Update ngsw-config.json to fix issue https://github.com/angular/angular-cli/pull/15277 | ||
*/ | ||
export function updateNGSWConfig(): Rule { | ||
return (tree: Tree) => { | ||
const workspace = getWorkspace(tree); | ||
|
||
for (const { target } of getTargets(workspace, 'build', Builders.Browser)) { | ||
for (const options of getAllOptions(target)) { | ||
const ngswConfigPath = findPropertyInAstObject(options, 'ngswConfigPath'); | ||
if (!ngswConfigPath || ngswConfigPath.kind !== 'string') { | ||
continue; | ||
} | ||
|
||
const path = ngswConfigPath.value; | ||
const configBuffer = tree.read(path); | ||
if (!configBuffer) { | ||
throw new SchematicsException(`Could not find (${path})`); | ||
} | ||
|
||
const content = configBuffer.toString(); | ||
const ngswConfigAst = parseJsonAst(content, JsonParseMode.Loose); | ||
if (!ngswConfigAst || ngswConfigAst.kind !== 'object') { | ||
continue; | ||
} | ||
|
||
const assetGroups = findPropertyInAstObject(ngswConfigAst, 'assetGroups'); | ||
if (!assetGroups || assetGroups.kind !== 'array') { | ||
continue; | ||
} | ||
|
||
const prefetchElement = assetGroups.elements.find(element => { | ||
const installMode = element.kind === 'object' && findPropertyInAstObject(element, 'installMode'); | ||
|
||
return installMode && installMode.value === 'prefetch'; | ||
}); | ||
|
||
if (!prefetchElement || prefetchElement.kind !== 'object') { | ||
continue; | ||
} | ||
|
||
const resources = findPropertyInAstObject(prefetchElement, 'resources'); | ||
if (!resources || resources.kind !== 'object') { | ||
continue; | ||
} | ||
|
||
const files = findPropertyInAstObject(resources, 'files'); | ||
if (!files || files.kind !== 'array') { | ||
continue; | ||
} | ||
|
||
const hasManifest = files.elements | ||
.some(({ value }) => typeof value === 'string' && value.endsWith('manifest.webmanifest')); | ||
if (hasManifest) { | ||
continue; | ||
} | ||
|
||
const recorder = tree.beginUpdate(path); | ||
appendValueInAstArray(recorder, files, '/manifest.webmanifest', 10); | ||
tree.commitUpdate(recorder); | ||
} | ||
} | ||
|
||
return tree; | ||
}; | ||
} |
139 changes: 139 additions & 0 deletions
139
packages/schematics/angular/migrations/update-9/ngsw-config_spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/** | ||
* @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 { EmptyTree } from '@angular-devkit/schematics'; | ||
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
import { WorkspaceTargets } from '../../utility/workspace-models'; | ||
|
||
// tslint:disable-next-line: no-any | ||
function getWorkspaceTargets(tree: UnitTestTree): any { | ||
return JSON.parse(tree.readContent(workspacePath)) | ||
.projects['migration-test'].architect; | ||
} | ||
|
||
function updateWorkspaceTargets(tree: UnitTestTree, workspaceTargets: WorkspaceTargets) { | ||
const config = JSON.parse(tree.readContent(workspacePath)); | ||
config.projects['migration-test'].architect = workspaceTargets; | ||
tree.overwrite(workspacePath, JSON.stringify(config, undefined, 2)); | ||
} | ||
|
||
const workspacePath = '/angular.json'; | ||
|
||
// tslint:disable:no-big-function | ||
describe('Migration to version 9', () => { | ||
describe('Migrate ngsw config', () => { | ||
const schematicRunner = new SchematicTestRunner( | ||
'migrations', | ||
require.resolve('../migration-collection.json'), | ||
); | ||
|
||
let tree: UnitTestTree; | ||
|
||
beforeEach(async () => { | ||
tree = new UnitTestTree(new EmptyTree()); | ||
tree = await schematicRunner | ||
.runExternalSchematicAsync( | ||
require.resolve('../../collection.json'), | ||
'ng-new', | ||
{ | ||
name: 'migration-test', | ||
version: '1.2.3', | ||
directory: '.', | ||
}, | ||
tree, | ||
) | ||
.toPromise(); | ||
}); | ||
|
||
it(`should add 'manifest.webmanifest' to files in prefetch`, async () => { | ||
const ngswConfigPath = '/ngsw-config.json'; | ||
const config = getWorkspaceTargets(tree); | ||
config.build.options.ngswConfigPath = ngswConfigPath; | ||
updateWorkspaceTargets(tree, config); | ||
|
||
const ngswConfig = JSON.stringify( | ||
{ | ||
assetGroups: [ | ||
{ | ||
name: 'app', | ||
installMode: 'prefetch', | ||
resources: { | ||
files: [ | ||
'/favicon.ico', | ||
'/index.html', | ||
], | ||
}, | ||
}, | ||
{ | ||
name: 'assets', | ||
installMode: 'lazy', | ||
updateMode: 'prefetch', | ||
resources: { | ||
files: [ | ||
'/assets/**', | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
null, | ||
2, | ||
); | ||
|
||
tree.create(ngswConfigPath, ngswConfig); | ||
|
||
const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise(); | ||
const { assetGroups } = JSON.parse(tree2.readContent(ngswConfigPath)); | ||
expect(assetGroups[0].resources.files).toEqual([ | ||
'/favicon.ico', | ||
'/index.html', | ||
'/manifest.webmanifest', | ||
]); | ||
expect(assetGroups[1].resources.files).toEqual([ | ||
'/assets/**', | ||
]); | ||
}); | ||
|
||
it(`should not add 'manifest.webmanifest' to files if exists`, async () => { | ||
const ngswConfigPath = '/ngsw-config.json'; | ||
const config = getWorkspaceTargets(tree); | ||
config.build.options.ngswConfigPath = ngswConfigPath; | ||
updateWorkspaceTargets(tree, config); | ||
|
||
const ngswConfig = JSON.stringify( | ||
{ | ||
assetGroups: [ | ||
{ | ||
name: 'app', | ||
installMode: 'prefetch', | ||
resources: { | ||
files: [ | ||
'/manifest.webmanifest', | ||
'/favicon.ico', | ||
'/index.html', | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
null, | ||
2, | ||
); | ||
|
||
tree.create(ngswConfigPath, ngswConfig); | ||
|
||
const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise(); | ||
const { assetGroups } = JSON.parse(tree2.readContent(ngswConfigPath)); | ||
expect(assetGroups[0].resources.files).toEqual([ | ||
'/manifest.webmanifest', | ||
'/favicon.ico', | ||
'/index.html', | ||
]); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.