Skip to content
This repository has been archived by the owner on Apr 9, 2022. It is now read-only.

Commit

Permalink
fix(@schematics/angular): fix scoped library paths
Browse files Browse the repository at this point in the history
Partially address angular/angular-cli#10615
  • Loading branch information
filipesilva authored and Brocco committed May 16, 2018
1 parent 4ba5a9f commit 020c1d8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "<%= projectRoot.split('/').map(x => '..').join('/') %>/node_modules/ng-packagr/ng-package.schema.json",
"dest": "<%= projectRoot.split('/').map(x => '..').join('/') %>/dist/<%= dasherize(packageName) %>",
"$schema": "<%= relativePathToWorkspaceRoot %>/node_modules/ng-packagr/ng-package.schema.json",
"dest": "<%= relativePathToWorkspaceRoot %>/<%= distRoot %>",
"deleteDestPath": false,
"lib": {
"entryFile": "src/<%= entryFile %>.ts"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "<%= projectRoot.split('/').map(x => '..').join('/') %>/node_modules/ng-packagr/ng-package.schema.json",
"dest": "<%= projectRoot.split('/').map(x => '..').join('/') %>/dist/lib",
"$schema": "<%= relativePathToWorkspaceRoot %>/node_modules/ng-packagr/ng-package.schema.json",
"dest": "<%= relativePathToWorkspaceRoot %>/<%= distRoot %>",
"lib": {
"entryFile": "src/<%= entryFile %>.ts"
}
Expand Down
23 changes: 12 additions & 11 deletions packages/schematics/angular/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function updateJsonFile<T>(host: Tree, path: string, callback: UpdateJsonFn<T>):
return host;
}

function updateTsConfig(npmPackageName: string) {
function updateTsConfig(packageName: string, distRoot: string) {

return (host: Tree) => {
if (!host.exists('tsconfig.json')) { return host; }
Expand All @@ -78,10 +78,10 @@ function updateTsConfig(npmPackageName: string) {
if (!tsconfig.compilerOptions.paths) {
tsconfig.compilerOptions.paths = {};
}
if (!tsconfig.compilerOptions.paths[npmPackageName]) {
tsconfig.compilerOptions.paths[npmPackageName] = [];
if (!tsconfig.compilerOptions.paths[packageName]) {
tsconfig.compilerOptions.paths[packageName] = [];
}
tsconfig.compilerOptions.paths[npmPackageName].push(`dist/${npmPackageName}`);
tsconfig.compilerOptions.paths[packageName].push(distRoot);
});
};
}
Expand Down Expand Up @@ -183,7 +183,7 @@ export default function (options: LibraryOptions): Rule {

// If scoped project (i.e. "@foo/bar"), convert projectDir to "foo/bar".
const packageName = options.name;
let scopeName = '';
let scopeName = null;
if (/^@.*\/.*/.test(options.name)) {
const [scope, name] = options.name.split('/');
scopeName = scope.replace(/^@/, '');
Expand All @@ -192,11 +192,11 @@ export default function (options: LibraryOptions): Rule {

const workspace = getWorkspace(host);
const newProjectRoot = workspace.newProjectRoot;
let projectRoot = `${newProjectRoot}/${strings.dasherize(options.name)}`;
if (scopeName) {
projectRoot =
`${newProjectRoot}/${strings.dasherize(scopeName)}/${strings.dasherize(options.name)}`;
}

const scopeFolder = scopeName ? strings.dasherize(scopeName) + '/' : '';
const folderName = `${scopeFolder}${strings.dasherize(options.name)}`;
const projectRoot = `${newProjectRoot}/${folderName}`;
const distRoot = `dist/${folderName}`;

const sourceDir = `${projectRoot}/src/lib`;
const relativePathToWorkspaceRoot = projectRoot.split('/').map(x => '..').join('/');
Expand All @@ -207,6 +207,7 @@ export default function (options: LibraryOptions): Rule {
...options,
packageName,
projectRoot,
distRoot,
relativePathToWorkspaceRoot,
prefix,
}),
Expand All @@ -219,7 +220,7 @@ export default function (options: LibraryOptions): Rule {
branchAndMerge(mergeWith(templateSource)),
addAppToWorkspaceFile(options, workspace, projectRoot, packageName),
options.skipPackageJson ? noop() : addDependenciesToPackageJson(),
options.skipTsConfig ? noop() : updateTsConfig(options.name),
options.skipTsConfig ? noop() : updateTsConfig(packageName, distRoot),
schematic('module', {
name: options.name,
commonModule: false,
Expand Down
6 changes: 5 additions & 1 deletion packages/schematics/angular/library/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,23 @@ describe('Library Schematic', () => {

const cfg = JSON.parse(tree.readContent('/angular.json'));
expect(cfg.projects['@myscope/mylib']).toBeDefined();

const rootTsCfg = JSON.parse(tree.readContent('/tsconfig.json'));
expect(rootTsCfg.compilerOptions.paths['@myscope/mylib']).toEqual(['dist/myscope/mylib']);
});

it(`should dasherize scoped libraries`, () => {
const scopedName = '@myScope/myLib';
const expectedScopeName = '@my-scope/my-lib';
const expectedFolderName = 'my-scope/my-lib';
const options = { ...defaultOptions, name: scopedName };
const tree = schematicRunner.runSchematic('library', options, workspaceTree);

const pkgJsonPath = '/projects/my-scope/my-lib/package.json';
expect(tree.readContent(pkgJsonPath)).toContain(expectedScopeName);

const ngPkgJsonPath = '/projects/my-scope/my-lib/ng-package.json';
expect(tree.readContent(ngPkgJsonPath)).toContain(expectedScopeName);
expect(tree.readContent(ngPkgJsonPath)).toContain(expectedFolderName);

const pkgJson = JSON.parse(tree.readContent(pkgJsonPath));
expect(pkgJson.name).toEqual(expectedScopeName);
Expand Down

0 comments on commit 020c1d8

Please sign in to comment.