Skip to content

Commit

Permalink
feat(@schematics/angular): Added --project-root option to the library…
Browse files Browse the repository at this point in the history
… schematics

With this change user is able to specify different project root for libraries than default one.

Closes: #11927
  • Loading branch information
DariuszOstolski authored and clydin committed Oct 7, 2022
1 parent 9b07b46 commit d8bff4f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/schematics/angular/library/index.ts
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
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
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

0 comments on commit d8bff4f

Please sign in to comment.