From d8bff4f1e68a76da1983f9d0774f415e73dfd8c3 Mon Sep 17 00:00:00 2001 From: Dariusz Ostolski Date: Sat, 1 Oct 2022 22:35:00 +0200 Subject: [PATCH] feat(@schematics/angular): Added --project-root option to the library schematics With this change user is able to specify different project root for libraries than default one. Closes: #11927 --- packages/schematics/angular/library/index.ts | 16 ++++--- .../schematics/angular/library/index_spec.ts | 43 +++++++++++++++++++ .../schematics/angular/library/schema.json | 4 ++ 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/packages/schematics/angular/library/index.ts b/packages/schematics/angular/library/index.ts index dfeb2e477cff..9101741bdbfd 100644 --- a/packages/schematics/angular/library/index.ts +++ b/packages/schematics/angular/library/index.ts @@ -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', { diff --git a/packages/schematics/angular/library/index_spec.ts b/packages/schematics/angular/library/index_spec.ts index bd129d2982a3..8e5cc7d1e398 100644 --- a/packages/schematics/angular/library/index_spec.ts +++ b/packages/schematics/angular/library/index_spec.ts @@ -31,6 +31,7 @@ describe('Library Schematic', () => { skipTsConfig: false, skipInstall: false, }; + const workspaceOptions: WorkspaceOptions = { name: 'workspace', newProjectRoot: 'projects', @@ -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) diff --git a/packages/schematics/angular/library/schema.json b/packages/schematics/angular/library/schema.json index cb0083e60a06..95c97f1732d4 100644 --- a/packages/schematics/angular/library/schema.json +++ b/packages/schematics/angular/library/schema.json @@ -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"]