Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 284a7ba

Browse files
committed
fix(@schematics/angular): Allow for scoped library names
fixes angular/angular-cli#10172
1 parent de71dcb commit 284a7ba

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

packages/schematics/angular/library/files/__projectRoot__/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "<%= dasherize(name) %>",
2+
"name": "<%= packageName %>",
33
"version": "0.0.1",
44
"peerDependencies": {
55
"@angular/common": "^6.0.0-rc.0 || ^6.0.0",

packages/schematics/angular/library/index.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,28 @@ export default function (options: LibraryOptions): Rule {
171171
if (!options.name) {
172172
throw new SchematicsException(`Invalid options, "name" is required.`);
173173
}
174-
const name = options.name;
174+
// If scoped project (i.e. "@foo/bar"), convert projectDir to "foo-bar".
175+
const packageName = options.name;
176+
let scopeName = '';
177+
if (/@[\S]*\/[\S]*/.test(options.name)) {
178+
const [scope, name] = options.name.split('/');
179+
scopeName = scope.replace('@', '');
180+
options.name = name;
181+
}
175182

176183
const workspace = getWorkspace(host);
177184
const newProjectRoot = workspace.newProjectRoot;
178-
const projectRoot = `${newProjectRoot}/${options.name}`;
185+
let projectRoot = `${newProjectRoot}/${options.name}`;
186+
if (scopeName) {
187+
projectRoot = `${newProjectRoot}/${scopeName}/${options.name}`;
188+
}
179189
const sourceDir = `${projectRoot}/src/lib`;
180190

181191
const templateSource = apply(url('./files'), [
182192
template({
183193
...strings,
184194
...options,
195+
packageName,
185196
projectRoot,
186197
}),
187198
// TODO: Moving inside `branchAndMerge` should work but is bugged right now.
@@ -193,27 +204,27 @@ export default function (options: LibraryOptions): Rule {
193204
branchAndMerge(mergeWith(templateSource)),
194205
addAppToWorkspaceFile(options, workspace),
195206
options.skipPackageJson ? noop() : addDependenciesToPackageJson(),
196-
options.skipTsConfig ? noop() : updateTsConfig(name),
207+
options.skipTsConfig ? noop() : updateTsConfig(options.name),
197208
schematic('module', {
198-
name: name,
209+
name: options.name,
199210
commonModule: false,
200211
flat: true,
201212
path: sourceDir,
202213
spec: false,
203214
}),
204215
schematic('component', {
205-
name: name,
216+
name: options.name,
206217
inlineStyle: true,
207218
inlineTemplate: true,
208219
flat: true,
209220
path: sourceDir,
210221
export: true,
211222
}),
212223
schematic('service', {
213-
name: name,
224+
name: options.name,
214225
flat: true,
215226
path: sourceDir,
216-
module: `${name}.module.ts`,
227+
module: `${options.name}.module.ts`,
217228
}),
218229
])(host, context);
219230
};

packages/schematics/angular/library/index_spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,21 @@ describe('Library Schematic', () => {
166166
expect(tsConfigJson.compilerOptions.paths).toBeUndefined();
167167
});
168168
});
169+
170+
it(`should support creating scoped libraries`, () => {
171+
const scopedName = '@myscope/mylib';
172+
const options = { ...defaultOptions, name: scopedName };
173+
const tree = schematicRunner.runSchematic('library', options, workspaceTree);
174+
175+
const pkgJsonPath = '/projects/myscope/mylib/package.json';
176+
expect(tree.files).toContain(pkgJsonPath);
177+
expect(tree.files).toContain('/projects/myscope/mylib/src/lib/mylib.module.ts');
178+
expect(tree.files).toContain('/projects/myscope/mylib/src/lib/mylib.component.ts');
179+
180+
const pkgJson = JSON.parse(tree.readContent(pkgJsonPath));
181+
expect(pkgJson.name).toEqual(scopedName);
182+
183+
const tsConfigJson = JSON.parse(tree.readContent('/projects/myscope/mylib/tsconfig.spec.json'));
184+
expect(tsConfigJson.extends).toEqual('../../../tsconfig.json');
185+
});
169186
});

0 commit comments

Comments
 (0)