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

Commit 8a28fc9

Browse files
committed
fix(@schematics/angular): Allow for scoped library names
fixes angular/angular-cli#10172
1 parent 746e402 commit 8a28fc9

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

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

+1-1
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

+11-6
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ 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+
options.name = /@[\S]*\/[\S]*/.test(options.name)
177+
? options.name.replace(/^@/, '').replace(/\//, '-')
178+
: options.name;
175179

176180
const workspace = getWorkspace(host);
177181
const newProjectRoot = workspace.newProjectRoot;
@@ -182,6 +186,7 @@ export default function (options: LibraryOptions): Rule {
182186
template({
183187
...strings,
184188
...options,
189+
packageName,
185190
projectRoot,
186191
}),
187192
// TODO: Moving inside `branchAndMerge` should work but is bugged right now.
@@ -193,27 +198,27 @@ export default function (options: LibraryOptions): Rule {
193198
branchAndMerge(mergeWith(templateSource)),
194199
addAppToWorkspaceFile(options, workspace),
195200
options.skipPackageJson ? noop() : addDependenciesToPackageJson(),
196-
options.skipTsConfig ? noop() : updateTsConfig(name),
201+
options.skipTsConfig ? noop() : updateTsConfig(options.name),
197202
schematic('module', {
198-
name: name,
203+
name: options.name,
199204
commonModule: false,
200205
flat: true,
201206
path: sourceDir,
202207
spec: false,
203208
}),
204209
schematic('component', {
205-
name: name,
210+
name: options.name,
206211
inlineStyle: true,
207212
inlineTemplate: true,
208213
flat: true,
209214
path: sourceDir,
210215
export: true,
211216
}),
212217
schematic('service', {
213-
name: name,
218+
name: options.name,
214219
flat: true,
215220
path: sourceDir,
216-
module: `${name}.module.ts`,
221+
module: `${options.name}.module.ts`,
217222
}),
218223
])(host, context);
219224
};

packages/schematics/angular/library/index_spec.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function getJsonFileContent(tree: UnitTestTree, path: string) {
1616
return JSON.parse(tree.readContent(path));
1717
}
1818

19-
describe('Library Schematic', () => {
19+
fdescribe('Library Schematic', () => {
2020
const schematicRunner = new SchematicTestRunner(
2121
'@schematics/ng_packagr',
2222
path.join(__dirname, '../collection.json'),
@@ -166,4 +166,16 @@ describe('Library Schematic', () => {
166166
expect(tsConfigJson.compilerOptions.paths).toBeUndefined();
167167
});
168168
});
169+
170+
it(`should support creating scoped libraries`, () => {
171+
const scopedName = '@scope/lib';
172+
const options = { ...defaultOptions, name: scopedName };
173+
const tree = schematicRunner.runSchematic('library', options, workspaceTree);
174+
175+
const pkgJsonPath = '/projects/scope-lib/package.json';
176+
expect(tree.files).toContain(pkgJsonPath);
177+
178+
const pkgJson = JSON.parse(tree.readContent(pkgJsonPath));
179+
expect(pkgJson.name).toEqual(scopedName);
180+
});
169181
});

0 commit comments

Comments
 (0)