Skip to content

feat(@schematics/angular): Added --project-root option to the library schematic #23994

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 1 commit into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 10 additions & 6 deletions packages/schematics/angular/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,29 +136,33 @@ export default function (options: LibraryOptions): Rule {
folderName = strings.dasherize(folderName);
}

const projectRoot = join(normalize(newProjectRoot), folderName);
const libDir =
options.projectRoot !== undefined
? normalize(options.projectRoot)
: join(normalize(newProjectRoot), folderName);

const distRoot = `dist/${folderName}`;
const sourceDir = `${projectRoot}/src/lib`;
const sourceDir = `${libDir}/src/lib`;

const templateSource = apply(url('./files'), [
applyTemplates({
...strings,
...options,
packageName,
projectRoot,
libDir,
distRoot,
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(projectRoot),
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(libDir),
prefix,
angularLatestVersion: latestVersions.Angular.replace(/~|\^/, ''),
tsLibLatestVersion: latestVersions['tslib'].replace(/~|\^/, ''),
folderName,
}),
move(projectRoot),
move(libDir),
]);

return chain([
mergeWith(templateSource),
addLibToWorkspaceFile(options, projectRoot, packageName),
addLibToWorkspaceFile(options, libDir, packageName),
options.skipPackageJson ? noop() : addDependenciesToPackageJson(),
options.skipTsConfig ? noop() : updateTsConfig(packageName, distRoot),
schematic('module', {
Expand Down
43 changes: 43 additions & 0 deletions packages/schematics/angular/library/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('Library Schematic', () => {
skipTsConfig: false,
skipInstall: false,
};

const workspaceOptions: WorkspaceOptions = {
name: 'workspace',
newProjectRoot: 'projects',
Expand Down Expand Up @@ -66,6 +67,48 @@ describe('Library Schematic', () => {
);
});

describe('custom projectRoot', () => {
const customProjectRootOptions: GenerateLibrarySchema = {
name: 'foo',
entryFile: 'my-index',
skipPackageJson: false,
skipTsConfig: false,
skipInstall: false,
projectRoot: 'some/other/directory/bar',
};

it('should create files in /some/other/directory/bar', async () => {
const tree = await schematicRunner
.runSchematicAsync('library', customProjectRootOptions, workspaceTree)
.toPromise();
const files = tree.files;
expect(files).toEqual(
jasmine.arrayContaining([
'/some/other/directory/bar/ng-package.json',
'/some/other/directory/bar/package.json',
'/some/other/directory/bar/README.md',
'/some/other/directory/bar/tsconfig.lib.json',
'/some/other/directory/bar/tsconfig.lib.prod.json',
'/some/other/directory/bar/src/my-index.ts',
'/some/other/directory/bar/src/lib/foo.module.ts',
'/some/other/directory/bar/src/lib/foo.component.spec.ts',
'/some/other/directory/bar/src/lib/foo.component.ts',
'/some/other/directory/bar/src/lib/foo.service.spec.ts',
'/some/other/directory/bar/src/lib/foo.service.ts',
]),
);
});

it(`should add library to workspace`, async () => {
const tree = await schematicRunner
.runSchematicAsync('library', customProjectRootOptions, workspaceTree)
.toPromise();

const workspace = getJsonFileContent(tree, '/angular.json');
expect(workspace.projects.foo).toBeDefined();
});
});

it('should create a package.json named "foo"', async () => {
const tree = await schematicRunner
.runSchematicAsync('library', defaultOptions, workspaceTree)
Expand Down
4 changes: 4 additions & 0 deletions packages/schematics/angular/library/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
"type": "boolean",
"default": false,
"description": "Do not update \"tsconfig.json\" to add a path mapping for the new library. The path mapping is needed to use the library in an app, but can be disabled here to simplify development."
},
"projectRoot": {
"type": "string",
"description": "The root directory of the new library."
}
},
"required": ["name"]
Expand Down