Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/schematics/angular/entry-point/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ export default createProjectSchematic<EntryPointOptions>((options, { project, tr
throw new SchematicsException('Entry point name "package.json" is reserved.');
}

// The final segment names the generated class, selector, and files. Without this check,
// a name that is a valid export subpath but not usable as an identifier fails later with
// an error about a class or selector the user never provided.
if (
options.name !== '.' &&
!/^[a-zA-Z][a-zA-Z0-9_-]*(?:\.[a-zA-Z0-9_-]+)*$/.test(basename(options.name))
) {
throw new SchematicsException(
`Entry point name "${options.name}" is invalid. The final path segment is used to name ` +
'the generated source files and must start with a letter and contain only ' +
'alphanumeric characters, dots, dashes, and underscores.',
);
}

const libDir = project.root;
const packageJsonPath = join(libDir, 'package.json');
if (!tree.exists(packageJsonPath)) {
Expand Down
33 changes: 33 additions & 0 deletions packages/schematics/angular/entry-point/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,39 @@ describe('EntryPoint Schematic', () => {
}
});

it('should throw a descriptive error when the final name segment cannot name source files', async () => {
for (const invalidName of ['3d', 'feature/3d', 'svg~icons']) {
await expectAsync(
schematicRunner.runSchematic(
'entry-point',
{
name: invalidName,
project: 'foo',
},
workspaceTree,
),
).toBeRejectedWithError(
`Entry point name "${invalidName}" is invalid. The final path segment is used to name ` +
'the generated source files and must start with a letter and contain only ' +
'alphanumeric characters, dots, dashes, and underscores.',
);
}
});

it('should allow a final name segment containing dots', async () => {
const tree = await schematicRunner.runSchematic(
'entry-point',
{
name: 'testing.v2',
project: 'foo',
},
workspaceTree,
);

const packageJson = getJsonFileContent(tree, '/projects/foo/package.json');
expect(packageJson.exports['./testing.v2']).toBeDefined();
});

it('should throw if the entry point already exists in package.json exports', async () => {
const tree = await schematicRunner.runSchematic('entry-point', defaultOptions, workspaceTree);

Expand Down
Loading