-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(@schematics/angular): add migration to remove
package.json
in l…
…ibraries secondary entrypoints `package.json` files have been remove from secondary entrypoints in version 14 of Angular package format (APF). With this change we delete the now redundant files and in case these contained the deprecated way of how to configure secondary entrypoints in ng-packagr we migrate these as well. (cherry picked from commit 32281c2)
- Loading branch information
1 parent
20c10b3
commit 7e8e420
Showing
3 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
65 changes: 65 additions & 0 deletions
65
packages/schematics/angular/migrations/update-14/update-libraries-secondary-entrypoints.ts
This file contains 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,65 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC 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 { dirname, isJsonObject, join, normalize } from '@angular-devkit/core'; | ||
import { DirEntry, Rule } from '@angular-devkit/schematics'; | ||
import { getWorkspace } from '../../utility/workspace'; | ||
|
||
function* visitPackageJsonFiles( | ||
directory: DirEntry, | ||
includedInLookup = false, | ||
): IterableIterator<string> { | ||
if (includedInLookup) { | ||
for (const path of directory.subfiles) { | ||
if (path !== 'package.json') { | ||
continue; | ||
} | ||
|
||
yield join(directory.path, path); | ||
} | ||
} | ||
|
||
for (const path of directory.subdirs) { | ||
if (path === 'node_modules' || path.startsWith('.')) { | ||
continue; | ||
} | ||
|
||
yield* visitPackageJsonFiles(directory.dir(path), true); | ||
} | ||
} | ||
|
||
/** Migration to remove secondary entrypoints 'package.json' files and migrate ng-packagr configurations. */ | ||
export default function (): Rule { | ||
return async (tree) => { | ||
const workspace = await getWorkspace(tree); | ||
|
||
for (const project of workspace.projects.values()) { | ||
if ( | ||
project.extensions['projectType'] !== 'library' || | ||
![...project.targets.values()].some( | ||
({ builder }) => builder === '@angular-devkit/build-angular:ng-packagr', | ||
) | ||
) { | ||
// Project is not a library or doesn't use ng-packagr, skip. | ||
continue; | ||
} | ||
|
||
for (const path of visitPackageJsonFiles(tree.getDir(project.root))) { | ||
const json = tree.readJson(path); | ||
if (isJsonObject(json) && json['ngPackage']) { | ||
// Migrate ng-packagr config to an ng-packagr config file. | ||
const configFilePath = join(dirname(normalize(path)), 'ng-package.json'); | ||
tree.create(configFilePath, JSON.stringify(json['ngPackage'], undefined, 2)); | ||
} | ||
|
||
// Delete package.json as it is no longer needed in APF 14. | ||
tree.delete(path); | ||
} | ||
} | ||
}; | ||
} |
122 changes: 122 additions & 0 deletions
122
...es/schematics/angular/migrations/update-14/update-libraries-secondary-entrypoints_spec.ts
This file contains 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,122 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC 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 { Builders, ProjectType, WorkspaceSchema } from '../../utility/workspace-models'; | ||
|
||
function createFileStructure(tree: UnitTestTree) { | ||
const angularConfig: WorkspaceSchema = { | ||
version: 1, | ||
projects: { | ||
foo: { | ||
root: 'projects/foo', | ||
sourceRoot: 'projects/foo/src', | ||
projectType: ProjectType.Library, | ||
prefix: 'lib', | ||
architect: { | ||
build: { | ||
builder: Builders.NgPackagr, | ||
options: { | ||
project: '', | ||
tsConfig: '', | ||
}, | ||
}, | ||
}, | ||
}, | ||
bar: { | ||
root: 'projects/bar', | ||
sourceRoot: 'projects/bar/src', | ||
projectType: ProjectType.Library, | ||
prefix: 'lib', | ||
architect: { | ||
test: { | ||
builder: Builders.Karma, | ||
options: { | ||
karmaConfig: '', | ||
tsConfig: '', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2)); | ||
|
||
// Library foo | ||
tree.create('/projects/foo/package.json', JSON.stringify({ version: '0.0.0' }, undefined, 2)); | ||
// Library foo/secondary | ||
tree.create( | ||
'/projects/foo/secondary/package.json', | ||
JSON.stringify( | ||
{ version: '0.0.0', ngPackage: { lib: { entryFile: 'src/public-api.ts' } } }, | ||
undefined, | ||
2, | ||
), | ||
); | ||
|
||
// Library bar | ||
tree.create('/projects/bar/package.json', JSON.stringify({ version: '0.0.0' }, undefined, 2)); | ||
// Library bar/secondary | ||
tree.create( | ||
'/projects/bar/secondary/package.json', | ||
JSON.stringify({ version: '0.0.0' }, undefined, 2), | ||
); | ||
} | ||
|
||
describe(`Migration to update Angular libraries secondary entrypoints.`, () => { | ||
const schematicName = 'update-libraries-secondary-entrypoints'; | ||
const schematicRunner = new SchematicTestRunner( | ||
'migrations', | ||
require.resolve('../migration-collection.json'), | ||
); | ||
|
||
let tree: UnitTestTree; | ||
beforeEach(() => { | ||
tree = new UnitTestTree(new EmptyTree()); | ||
createFileStructure(tree); | ||
}); | ||
|
||
describe(`when library has '@angular-devkit/build-angular:ng-packagr' as a builder`, () => { | ||
it(`should not delete 'package.json' of primary entry-point`, async () => { | ||
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); | ||
|
||
expect(newTree.exists('/projects/foo/package.json')).toBeTrue(); | ||
}); | ||
|
||
it(`should delete 'package.json' of secondary entry-point`, async () => { | ||
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); | ||
expect(newTree.exists('/projects/foo/secondary/package.json')).toBeFalse(); | ||
}); | ||
|
||
it(`should move ng-packagr configuration from 'package.json' to 'ng-package.json'`, async () => { | ||
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); | ||
expect(newTree.readJson('projects/foo/secondary/ng-package.json')).toEqual({ | ||
lib: { entryFile: 'src/public-api.ts' }, | ||
}); | ||
}); | ||
}); | ||
|
||
describe(`when library doesn't have '@angular-devkit/build-angular:ng-packagr' as a builder`, () => { | ||
it(`should not delete 'package.json' of primary entry-point`, async () => { | ||
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); | ||
expect(newTree.exists('/projects/bar/package.json')).toBeTrue(); | ||
}); | ||
|
||
it(`should not delete 'package.json' of secondary entry-point`, async () => { | ||
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); | ||
expect(newTree.exists('/projects/bar/package.json')).toBeTrue(); | ||
}); | ||
|
||
it(`should not create ng-packagr configuration`, async () => { | ||
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise(); | ||
expect(newTree.exists('projects/bar/secondary/ng-package.json')).toBeFalse(); | ||
}); | ||
}); | ||
}); |