Skip to content

Commit

Permalink
refactor: make small changes and excluded tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jefiozie committed Feb 2, 2025
1 parent fa0daa3 commit 57230d0
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 66 deletions.
2 changes: 1 addition & 1 deletion packages/schematics/angular/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"aliases": ["lib-entry"],
"factory": "./library-entrypoint",
"schema": "./library-entrypoint/schema.json",
"description": "Generate an library-entrypoint in an Angular library project."
"description": "Generate a library-entrypoint in an Angular library project."
},
"web-worker": {
"factory": "./web-worker",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# <%= secondaryEntryPoint %>

Secondary entry point of `<%= mainEntryPoint %>`. It can be used by importing from `<%= secondaryEntryPoint %>`.
Secondary entry point of `<%= mainEntryPoint %>`. It can be used by importing from `<%= secondaryEntryPoint %>`

This file was deleted.

4 changes: 3 additions & 1 deletion packages/schematics/angular/library-entrypoint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function (options: LibraryOptions): Rule {

const pkg = host.readJson(pkgPath) as { name: string } | null;
if (pkg === null) {
throw new SchematicsException('Could not find package.json');
throw new SchematicsException(`Could not find ${pkgPath}`);
}

const mainEntryPoint = pkg.name;
Expand All @@ -80,6 +80,8 @@ export default function (options: LibraryOptions): Rule {
secondaryEntryPoint,
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(libDir),
packageName: options.name,
//TODO: fix this
entryFile: 'index.ts',
}),
move(libDir),
]);
Expand Down
36 changes: 7 additions & 29 deletions packages/schematics/angular/library-entrypoint/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
*/

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';
import { parse as parseJson } from 'jsonc-parser';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getJsonFileContent(tree: UnitTestTree, path: string): any {
Expand All @@ -35,7 +35,6 @@ describe('Secondary Entrypoint Schematic', () => {
};
const libaryOptions: LibraryOptions = {
name: 'foo',
entryFile: 'my-index',
standalone: true,
skipPackageJson: false,
skipTsConfig: false,
Expand All @@ -45,12 +44,12 @@ describe('Secondary Entrypoint Schematic', () => {
let workspaceTree: UnitTestTree;
beforeEach(async () => {
workspaceTree = await schematicRunner.runSchematic('workspace', workspaceOptions);
workspaceTree = await schematicRunner.runSchematic('library', libaryOptions, workspaceTree);
});

it('should create correct files', async () => {
workspaceTree = await schematicRunner.runSchematic('library', libaryOptions, workspaceTree);
const tree = await schematicRunner.runSchematic(
'secondary',
'library-entrypoint',
{ ...defaultOptions },
workspaceTree,
);
Expand All @@ -60,47 +59,28 @@ describe('Secondary Entrypoint Schematic', () => {
jasmine.arrayContaining([
'/projects/foo/src/lib/foo-secondary/README.md',
'/projects/foo/src/lib/foo-secondary/ng-package.json',
'/projects/foo/src/lib/foo-secondary/src/public-api.ts',
]),
);
});

it('should set correct main and secondary entrypoints in the README', async () => {
workspaceTree = await schematicRunner.runSchematic('library', libaryOptions, workspaceTree);
const tree = await schematicRunner.runSchematic(
'secondary',
'library-entrypoint',
{ ...defaultOptions },
workspaceTree,
);
const content = tree.readContent('/projects/foo/src/lib/foo-secondary/README.md');
expect(content).toMatch('# foo/foo-secondary');
});

it('should set a custom entryfile', async () => {
workspaceTree = await schematicRunner.runSchematic('library', libaryOptions, workspaceTree);
const tree = await schematicRunner.runSchematic(
'secondary',
{ ...defaultOptions, entryFile: 'my-index' },
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',
'/projects/foo/src/lib/foo-secondary/src/my-index.ts',
]),
);
});

it('should handle scope packages', async () => {
workspaceTree = await schematicRunner.runSchematic(
'library',
{ ...libaryOptions, name: '@scope/package' },
workspaceTree,
);
const tree = await schematicRunner.runSchematic(
'secondary',
'library-entrypoint',
{ ...defaultOptions, name: 'testing', project: '@scope/package' },
workspaceTree,
);
Expand All @@ -109,7 +89,6 @@ describe('Secondary Entrypoint Schematic', () => {
jasmine.arrayContaining([
'/projects/scope/package/src/lib/testing/README.md',
'/projects/scope/package/src/lib/testing/ng-package.json',
'/projects/scope/package/src/lib/testing/src/public-api.ts',
]),
);
});
Expand All @@ -121,7 +100,7 @@ describe('Secondary Entrypoint Schematic', () => {
workspaceTree,
);
const tree = await schematicRunner.runSchematic(
'secondary',
'library-entrypoint',
{ ...defaultOptions, name: 'testing', project: '@scope/package' },
workspaceTree,
);
Expand Down Expand Up @@ -150,14 +129,13 @@ describe('Secondary Entrypoint Schematic', () => {
}),
);
const tree = await schematicRunner.runSchematic(
'secondary',
'library-entrypoint',
{ ...defaultOptions, name: 'testing', project: '@scope/package' },
workspaceTree,
);

const tsConfigJson = getJsonFileContent(tree, 'tsconfig.json');
expect(tsConfigJson.compilerOptions.paths['@scope/package/testing']).toEqual([
'libs/*',
'./dist/scope/package/testing',
]);
});
Expand Down
17 changes: 4 additions & 13 deletions packages/schematics/angular/library-entrypoint/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"$id": "SchematicsLibrary",
"title": "Library Entrypoint Schema",
"type": "object",
"description": "Generate an library entrypoint in an Angular library project.",
"description": "Generate a library entrypoint in an Angular library project.",
"additionalProperties": false,
"properties": {
"name": {
"type": "string",
"description": "The name of the library.",
"description": "The name of the entrypoint to create.",
"pattern": "^[a-zA-Z0-9-._~]+/?[a-zA-Z0-9-._~]+$",
"$default": {
"$source": "argv",
Expand All @@ -22,16 +22,7 @@
"$default": {
"$source": "projectName"
}
},
"entryFile": {
"type": "string",
"format": "path",
"description": "The path at which to create the library's secondary public API file, relative to the workspace root.",
"default": "public-api"
}
},
"required": [
"name",
"project"
]
}
"required": ["name", "project"]
}
17 changes: 1 addition & 16 deletions tests/legacy-cli/e2e/tests/build/library-with-demo-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import { updateJsonFile } from '../../utils/project';
export default async function () {
await ng('generate', 'library', 'mylib');
await ng('generate', 'lib-entry', 'secondary');
// await createLibraryEntryPoint('secondary');
await createLibraryEntryPoint('another');
await ng('generate', 'lib-entry', 'another');

// Scenario #1 where we use wildcard path mappings for secondary entry-points.
await updateJsonFile('tsconfig.json', (json) => {
Expand Down Expand Up @@ -48,17 +47,3 @@ export default async function () {

await ng('build');
}

async function createLibraryEntryPoint(name: string): Promise<void> {
await createDir(`projects/mylib/${name}`);
await writeFile(`projects/mylib/${name}/index.ts`, `export const foo = 'foo';`);

await writeFile(
`projects/mylib/${name}/ng-package.json`,
JSON.stringify({
lib: {
entryFile: 'index.ts',
},
}),
);
}

0 comments on commit 57230d0

Please sign in to comment.