Skip to content

Commit d8bff4f

Browse files
DariuszOstolskiclydin
authored andcommitted
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
1 parent 9b07b46 commit d8bff4f

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

packages/schematics/angular/library/index.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -136,29 +136,33 @@ export default function (options: LibraryOptions): Rule {
136136
folderName = strings.dasherize(folderName);
137137
}
138138

139-
const projectRoot = join(normalize(newProjectRoot), folderName);
139+
const libDir =
140+
options.projectRoot !== undefined
141+
? normalize(options.projectRoot)
142+
: join(normalize(newProjectRoot), folderName);
143+
140144
const distRoot = `dist/${folderName}`;
141-
const sourceDir = `${projectRoot}/src/lib`;
145+
const sourceDir = `${libDir}/src/lib`;
142146

143147
const templateSource = apply(url('./files'), [
144148
applyTemplates({
145149
...strings,
146150
...options,
147151
packageName,
148-
projectRoot,
152+
libDir,
149153
distRoot,
150-
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(projectRoot),
154+
relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(libDir),
151155
prefix,
152156
angularLatestVersion: latestVersions.Angular.replace(/~|\^/, ''),
153157
tsLibLatestVersion: latestVersions['tslib'].replace(/~|\^/, ''),
154158
folderName,
155159
}),
156-
move(projectRoot),
160+
move(libDir),
157161
]);
158162

159163
return chain([
160164
mergeWith(templateSource),
161-
addLibToWorkspaceFile(options, projectRoot, packageName),
165+
addLibToWorkspaceFile(options, libDir, packageName),
162166
options.skipPackageJson ? noop() : addDependenciesToPackageJson(),
163167
options.skipTsConfig ? noop() : updateTsConfig(packageName, distRoot),
164168
schematic('module', {

packages/schematics/angular/library/index_spec.ts

+43
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe('Library Schematic', () => {
3131
skipTsConfig: false,
3232
skipInstall: false,
3333
};
34+
3435
const workspaceOptions: WorkspaceOptions = {
3536
name: 'workspace',
3637
newProjectRoot: 'projects',
@@ -66,6 +67,48 @@ describe('Library Schematic', () => {
6667
);
6768
});
6869

70+
describe('custom projectRoot', () => {
71+
const customProjectRootOptions: GenerateLibrarySchema = {
72+
name: 'foo',
73+
entryFile: 'my-index',
74+
skipPackageJson: false,
75+
skipTsConfig: false,
76+
skipInstall: false,
77+
projectRoot: 'some/other/directory/bar',
78+
};
79+
80+
it('should create files in /some/other/directory/bar', async () => {
81+
const tree = await schematicRunner
82+
.runSchematicAsync('library', customProjectRootOptions, workspaceTree)
83+
.toPromise();
84+
const files = tree.files;
85+
expect(files).toEqual(
86+
jasmine.arrayContaining([
87+
'/some/other/directory/bar/ng-package.json',
88+
'/some/other/directory/bar/package.json',
89+
'/some/other/directory/bar/README.md',
90+
'/some/other/directory/bar/tsconfig.lib.json',
91+
'/some/other/directory/bar/tsconfig.lib.prod.json',
92+
'/some/other/directory/bar/src/my-index.ts',
93+
'/some/other/directory/bar/src/lib/foo.module.ts',
94+
'/some/other/directory/bar/src/lib/foo.component.spec.ts',
95+
'/some/other/directory/bar/src/lib/foo.component.ts',
96+
'/some/other/directory/bar/src/lib/foo.service.spec.ts',
97+
'/some/other/directory/bar/src/lib/foo.service.ts',
98+
]),
99+
);
100+
});
101+
102+
it(`should add library to workspace`, async () => {
103+
const tree = await schematicRunner
104+
.runSchematicAsync('library', customProjectRootOptions, workspaceTree)
105+
.toPromise();
106+
107+
const workspace = getJsonFileContent(tree, '/angular.json');
108+
expect(workspace.projects.foo).toBeDefined();
109+
});
110+
});
111+
69112
it('should create a package.json named "foo"', async () => {
70113
const tree = await schematicRunner
71114
.runSchematicAsync('library', defaultOptions, workspaceTree)

packages/schematics/angular/library/schema.json

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
"type": "boolean",
4444
"default": false,
4545
"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."
46+
},
47+
"projectRoot": {
48+
"type": "string",
49+
"description": "The root directory of the new library."
4650
}
4751
},
4852
"required": ["name"]

0 commit comments

Comments
 (0)