-
Notifications
You must be signed in to change notification settings - Fork 12k
feat: add secondary entrypoint schematic #28723
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
Open
Jefiozie
wants to merge
4
commits into
angular:main
Choose a base branch
from
Jefiozie:secondaryEntrypoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
3 changes: 3 additions & 0 deletions
3
packages/schematics/angular/library-entrypoint/files/README.md.template
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,3 @@ | ||
# <%= secondaryEntryPoint %> | ||
|
||
Secondary entry point of `<%= mainEntryPoint %>`. It can be used by importing from `<%= secondaryEntryPoint %>` |
6 changes: 6 additions & 0 deletions
6
packages/schematics/angular/library-entrypoint/files/ng-package.json.template
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,6 @@ | ||
{ | ||
"$schema": "<%= relativePathToWorkspaceRoot %>/node_modules/ng-packagr/ng-package.schema.json", | ||
"lib": { | ||
"entryFile": "src/<%= entryFile %>.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,95 @@ | ||
/** | ||
* @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.dev/license | ||
*/ | ||
|
||
import { | ||
Rule, | ||
SchematicsException, | ||
Tree, | ||
apply, | ||
applyTemplates, | ||
chain, | ||
mergeWith, | ||
move, | ||
strings, | ||
url, | ||
} from '@angular-devkit/schematics'; | ||
import { JSONFile } from '../utility/json-file'; | ||
import { relativePathToWorkspaceRoot } from '../utility/paths'; | ||
import { buildDefaultPath, getWorkspace } from '../utility/workspace'; | ||
import { ProjectType } from '../utility/workspace-models'; | ||
import { Schema as LibraryOptions } from './schema'; | ||
|
||
function updateTsConfig(packageName: string, ...paths: string[]) { | ||
return (host: Tree) => { | ||
if (!host.exists('tsconfig.json')) { | ||
return host; | ||
} | ||
|
||
const file = new JSONFile(host, 'tsconfig.json'); | ||
const jsonPath = ['compilerOptions', 'paths', packageName]; | ||
const value = file.get(jsonPath); | ||
file.modify(jsonPath, Array.isArray(value) ? [...paths, ...value] : paths); | ||
}; | ||
} | ||
|
||
export default function (options: LibraryOptions): Rule { | ||
return async (host: Tree) => { | ||
return async (host: Tree) => { | ||
const workspace = await getWorkspace(host); | ||
const project = workspace.projects.get(options.project); | ||
|
||
if (!project) { | ||
throw new SchematicsException(`Project "${options.project}" does not exist.`); | ||
} | ||
|
||
if (project?.extensions.projectType !== ProjectType.Library) { | ||
throw new SchematicsException( | ||
`Library entrypoint schematic requires a project type of "library".`, | ||
); | ||
} | ||
|
||
const path = buildDefaultPath(project); | ||
const libDir = `${path}/${options.name}`; | ||
const pkgPath = `${project.root}/package.json`; | ||
|
||
const pkg = host.readJson(pkgPath) as { name: string } | null; | ||
if (pkg === null) { | ||
throw new SchematicsException(`Could not find ${pkgPath}`); | ||
} | ||
|
||
const mainEntryPoint = pkg.name; | ||
const secondaryEntryPoint = `${mainEntryPoint}/${options.name}`; | ||
|
||
let folderName = mainEntryPoint.startsWith('@') ? mainEntryPoint.slice(1) : mainEntryPoint; | ||
if (/[A-Z]/.test(folderName)) { | ||
folderName = strings.dasherize(folderName); | ||
} | ||
|
||
const distRoot = `dist/${folderName}/${options.name}`; | ||
|
||
const templateSource = apply(url('./files'), [ | ||
applyTemplates({ | ||
...strings, | ||
...options, | ||
mainEntryPoint, | ||
secondaryEntryPoint, | ||
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(libDir), | ||
packageName: options.name, | ||
//TODO: fix this | ||
Check failure on line 83 in packages/schematics/angular/library-entrypoint/index.ts
|
||
entryFile: 'index.ts', | ||
}), | ||
move(libDir), | ||
]); | ||
|
||
return chain([ | ||
mergeWith(templateSource), | ||
updateTsConfig(secondaryEntryPoint, './' + distRoot), | ||
]); | ||
}; | ||
}; | ||
} |
142 changes: 142 additions & 0 deletions
142
packages/schematics/angular/library-entrypoint/index_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,142 @@ | ||
/** | ||
* @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.dev/license | ||
*/ | ||
|
||
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
import { parse as parseJson } from 'jsonc-parser'; | ||
import { Schema as LibraryOptions } from '../library/schema'; | ||
import { Schema as WorkspaceOptions } from '../workspace/schema'; | ||
import { Schema as GenerateLibrarySchema } from './schema'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function getJsonFileContent(tree: UnitTestTree, path: string): any { | ||
return parseJson(tree.readContent(path).toString()); | ||
} | ||
|
||
describe('Secondary Entrypoint Schematic', () => { | ||
const schematicRunner = new SchematicTestRunner( | ||
'@schematics/ng_packagr', | ||
require.resolve('../collection.json'), | ||
); | ||
const defaultOptions: GenerateLibrarySchema = { | ||
name: 'foo-secondary', | ||
project: 'foo', | ||
}; | ||
|
||
const workspaceOptions: WorkspaceOptions = { | ||
name: 'workspace', | ||
newProjectRoot: 'projects', | ||
|
||
version: '6.0.0', | ||
}; | ||
const libaryOptions: LibraryOptions = { | ||
name: 'foo', | ||
standalone: true, | ||
skipPackageJson: false, | ||
skipTsConfig: false, | ||
skipInstall: false, | ||
}; | ||
|
||
let workspaceTree: UnitTestTree; | ||
beforeEach(async () => { | ||
workspaceTree = await schematicRunner.runSchematic('workspace', workspaceOptions); | ||
workspaceTree = await schematicRunner.runSchematic('library', libaryOptions, workspaceTree); | ||
}); | ||
|
||
it('should create correct files', async () => { | ||
const tree = await schematicRunner.runSchematic( | ||
'library-entrypoint', | ||
{ ...defaultOptions }, | ||
workspaceTree, | ||
); | ||
const files = tree.files; | ||
|
||
expect(files).toEqual( | ||
jasmine.arrayContaining([ | ||
'/projects/foo/src/lib/foo-secondary/README.md', | ||
'/projects/foo/src/lib/foo-secondary/ng-package.json', | ||
]), | ||
); | ||
}); | ||
|
||
it('should set correct main and secondary entrypoints in the README', async () => { | ||
const tree = await schematicRunner.runSchematic( | ||
'library-entrypoint', | ||
{ ...defaultOptions }, | ||
workspaceTree, | ||
); | ||
const content = tree.readContent('/projects/foo/src/lib/foo-secondary/README.md'); | ||
expect(content).toMatch('# foo/foo-secondary'); | ||
}); | ||
|
||
it('should handle scope packages', async () => { | ||
workspaceTree = await schematicRunner.runSchematic( | ||
'library', | ||
{ ...libaryOptions, name: '@scope/package' }, | ||
workspaceTree, | ||
); | ||
const tree = await schematicRunner.runSchematic( | ||
'library-entrypoint', | ||
{ ...defaultOptions, name: 'testing', project: '@scope/package' }, | ||
workspaceTree, | ||
); | ||
const files = tree.files; | ||
expect(files).toEqual( | ||
jasmine.arrayContaining([ | ||
'/projects/scope/package/src/lib/testing/README.md', | ||
'/projects/scope/package/src/lib/testing/ng-package.json', | ||
]), | ||
); | ||
}); | ||
|
||
it(`should add paths mapping to the tsconfig`, async () => { | ||
workspaceTree = await schematicRunner.runSchematic( | ||
'library', | ||
{ ...libaryOptions, name: '@scope/package' }, | ||
workspaceTree, | ||
); | ||
const tree = await schematicRunner.runSchematic( | ||
'library-entrypoint', | ||
{ ...defaultOptions, name: 'testing', project: '@scope/package' }, | ||
workspaceTree, | ||
); | ||
|
||
const tsConfigJson = getJsonFileContent(tree, 'tsconfig.json'); | ||
expect(tsConfigJson.compilerOptions.paths['@scope/package/testing']).toEqual([ | ||
'./dist/scope/package/testing', | ||
]); | ||
}); | ||
|
||
it(`should append to existing paths mappings`, async () => { | ||
workspaceTree = await schematicRunner.runSchematic( | ||
'library', | ||
{ ...libaryOptions, name: '@scope/package' }, | ||
workspaceTree, | ||
); | ||
workspaceTree.overwrite( | ||
'tsconfig.json', | ||
JSON.stringify({ | ||
compilerOptions: { | ||
paths: { | ||
'unrelated': ['./something/else.ts'], | ||
'@scope/package/testing': ['libs/*'], | ||
}, | ||
}, | ||
}), | ||
); | ||
const tree = await schematicRunner.runSchematic( | ||
'library-entrypoint', | ||
{ ...defaultOptions, name: 'testing', project: '@scope/package' }, | ||
workspaceTree, | ||
); | ||
const tsConfigJson = getJsonFileContent(tree, 'tsconfig.json'); | ||
expect(tsConfigJson.compilerOptions.paths['@scope/package/testing']).toEqual([ | ||
'./dist/scope/package/testing', | ||
'libs/*', | ||
]); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
packages/schematics/angular/library-entrypoint/schema.json
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,28 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema", | ||
"$id": "SchematicsLibrary", | ||
"title": "Library Entrypoint Schema", | ||
"type": "object", | ||
"description": "Generate a library entrypoint in an Angular library project.", | ||
"additionalProperties": false, | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"description": "The name of the entrypoint to create.", | ||
"pattern": "^[a-zA-Z0-9-._~]+/?[a-zA-Z0-9-._~]+$", | ||
"$default": { | ||
"$source": "argv", | ||
"index": 0 | ||
}, | ||
"x-prompt": "What name would you like to use for the entrypoint?" | ||
}, | ||
"project": { | ||
"type": "string", | ||
"description": "The name of the project.", | ||
"$default": { | ||
"$source": "projectName" | ||
} | ||
} | ||
}, | ||
"required": ["name", "project"] | ||
} |
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
Oops, something went wrong.
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.