Skip to content

Commit

Permalink
fix(angular): require importPath for publishable libs & adjust buildable
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #2794
  • Loading branch information
juristr committed Jul 21, 2020
1 parent d512f9d commit 77378a0
Show file tree
Hide file tree
Showing 17 changed files with 260 additions and 62 deletions.
18 changes: 15 additions & 3 deletions docs/angular/api-angular/schematics/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,26 @@ Type: `boolean`

Add a module spec file.

### buildable

Default: `false`

Type: `boolean`

Generate a buildable library.

### directory

Type: `string`

A directory where the lib is placed

### importPath

Type: `string`

The library name used to import it, like @myorg/my-awesome-lib. Must be a valid npm name.

### lazy

Default: `false`
Expand Down Expand Up @@ -72,13 +86,11 @@ The prefix to apply to generated selectors.

### publishable

Alias(es): buildable

Default: `false`

Type: `boolean`

Generate a buildable library.
Generate a publishable library.

### routing

Expand Down
6 changes: 6 additions & 0 deletions docs/angular/api-react/schematics/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ Type: `string`

A directory where the lib is placed.

### importPath

Type: `string`

The library name used to import it, like @myorg/my-awesome-lib

### js

Default: `false`
Expand Down
18 changes: 15 additions & 3 deletions docs/react/api-angular/schematics/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,26 @@ Type: `boolean`

Add a module spec file.

### buildable

Default: `false`

Type: `boolean`

Generate a buildable library.

### directory

Type: `string`

A directory where the lib is placed

### importPath

Type: `string`

The library name used to import it, like @myorg/my-awesome-lib. Must be a valid npm name.

### lazy

Default: `false`
Expand Down Expand Up @@ -72,13 +86,11 @@ The prefix to apply to generated selectors.

### publishable

Alias(es): buildable

Default: `false`

Type: `boolean`

Generate a buildable library.
Generate a publishable library.

### routing

Expand Down
6 changes: 3 additions & 3 deletions e2e/angular/src/angular-package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ forEachCli('angular', (cli) => {
newProject();

runCLI(
`generate @nrwl/angular:library ${parentLib} --publishable=true --no-interactive`
`generate @nrwl/angular:library ${parentLib} --publishable=true --importPath=@proj/${parentLib} --no-interactive`
);
runCLI(
`generate @nrwl/angular:library ${childLib} --publishable=true --no-interactive`
`generate @nrwl/angular:library ${childLib} --publishable=true --importPath=@proj/${childLib} --no-interactive`
);
runCLI(
`generate @nrwl/angular:library ${childLib2} --publishable=true --no-interactive`
`generate @nrwl/angular:library ${childLib2} --publishable=true --importPath=@proj/${childLib2} --no-interactive`
);

// create secondary entrypoint
Expand Down
16 changes: 9 additions & 7 deletions e2e/workspace/src/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ forEachCli((cliName) => {
const mylib1 = uniq('mylib1');
const mylib2 = uniq('mylib1');
runCLI(`generate @nrwl/react:app ${myapp}`);
runCLI(`generate @nrwl/react:lib ${mylib1} --publishable`);
runCLI(`generate @nrwl/react:lib ${mylib2} --publishable`);
runCLI(`generate @nrwl/react:lib ${mylib1} --buildable`);
runCLI(`generate @nrwl/react:lib ${mylib2} --buildable`);

updateFile(
`apps/${myapp}/src/main.ts`,
Expand Down Expand Up @@ -80,9 +80,9 @@ forEachCli((cliName) => {
const libD = uniq('libd-rand');

runCLI(`generate @nrwl/angular:app ${appA}`);
runCLI(`generate @nrwl/angular:lib ${libA} --publishable --defaults`);
runCLI(`generate @nrwl/angular:lib ${libB} --publishable --defaults`);
runCLI(`generate @nrwl/angular:lib ${libC} --publishable --defaults`);
runCLI(`generate @nrwl/angular:lib ${libA} --buildable --defaults`);
runCLI(`generate @nrwl/angular:lib ${libB} --buildable --defaults`);
runCLI(`generate @nrwl/angular:lib ${libC} --buildable --defaults`);
runCLI(`generate @nrwl/angular:lib ${libD} --defaults`);

// libA depends on libC
Expand Down Expand Up @@ -206,7 +206,9 @@ forEachCli((cliName) => {
runCLI(`generate @nrwl/angular:app ${myapp2}`);
runCLI(`generate @nrwl/angular:lib ${mylib}`);
runCLI(`generate @nrwl/angular:lib ${mylib2}`);
runCLI(`generate @nrwl/angular:lib ${mypublishablelib} --publishable`);
runCLI(
`generate @nrwl/angular:lib ${mypublishablelib} --publishable --importPath=@proj/${mypublishablelib}`
);

updateFile(
`apps/${myapp}/src/app/app.component.spec.ts`,
Expand Down Expand Up @@ -434,7 +436,7 @@ forEachCli((cliName) => {
runCLI(`generate @nrwl/react:app ${myapp2}`);
runCLI(`generate @nrwl/react:lib ${mylib}`);
runCLI(`generate @nrwl/react:lib ${mylib2}`);
runCLI(`generate @nrwl/react:lib ${mypublishablelib} --publishable`);
runCLI(`generate @nrwl/react:lib ${mypublishablelib} --buildable`);

updateFile(
`apps/${myapp}/src/main.tsx`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Tree } from '@angular-devkit/schematics';
import { getNpmScope, toClassName, toFileName } from '@nrwl/workspace';
import { libsDir } from '@nrwl/workspace/src/utils/ast-utils';
import { getNpmScope, toClassName, toFileName, NxJson } from '@nrwl/workspace';
import { libsDir, readJsonInTree } from '@nrwl/workspace/src/utils/ast-utils';
import { Schema } from '../schema';
import { NormalizedSchema } from './normalized-schema';

Expand All @@ -24,6 +24,9 @@ export function normalizeOptions(
const modulePath = `${projectRoot}/src/lib/${fileName}.module.ts`;
const defaultPrefix = getNpmScope(host);

const importPath =
options.importPath || `@${defaultPrefix}/${projectDirectory}`;

return {
...options,
prefix: options.prefix ? options.prefix : defaultPrefix,
Expand All @@ -35,5 +38,6 @@ export function normalizeOptions(
modulePath,
parsedTags,
fileName,
importPath,
};
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Rule, Tree } from '@angular-devkit/schematics';
import { getNpmScope, updateJsonInTree } from '@nrwl/workspace';
import { updateJsonInTree } from '@nrwl/workspace';
import { NormalizedSchema } from './normalized-schema';

export function updateLibPackageNpmScope(options: NormalizedSchema): Rule {
return (host: Tree) => {
return updateJsonInTree(`${options.projectRoot}/package.json`, (json) => {
json.name = `@${getNpmScope(host)}/${options.name}`;
return json;
});
};
return updateJsonInTree(`${options.projectRoot}/package.json`, (json) => {
json.name = options.importPath;
return json;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { libsDir } from '@nrwl/workspace/src/utils/ast-utils';
import { NormalizedSchema } from './normalized-schema';

export function updateNgPackage(host: Tree, options: NormalizedSchema): Rule {
if (!options.publishable) {
if (!options.publishable || !options.buildable) {
return noop();
}
const dest = `${offsetFromRoot(options.projectRoot)}dist/${libsDir(host)}/${
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/src/schematics/library/lib/update-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function updateProject(options: NormalizedSchema): Rule {
host.delete(path.join(libRoot, `${options.name}.component.spec.ts`));
}

if (!options.publishable) {
if (!options.publishable && !options.buildable) {
host.delete(path.join(options.projectRoot, 'ng-package.json'));
host.delete(path.join(options.projectRoot, 'package.json'));
host.delete(path.join(options.projectRoot, 'tsconfig.lib.prod.json'));
Expand Down Expand Up @@ -138,7 +138,7 @@ export function updateProject(options: NormalizedSchema): Rule {
};
}

if (!options.publishable) {
if (!options.publishable && !options.buildable) {
delete fixedProject.architect.build;
} else {
// adjust the builder path to our custom one
Expand Down
17 changes: 12 additions & 5 deletions packages/angular/src/schematics/library/lib/update-tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,30 @@ import {
chain,
Rule,
SchematicContext,
SchematicsException,
Tree,
} from '@angular-devkit/schematics';
import { NxJson, readJsonInTree, updateJsonInTree } from '@nrwl/workspace';
import { libsDir } from '@nrwl/workspace/src/utils/ast-utils';
import { updateJsonInTree } from '@nrwl/workspace';
import { NormalizedSchema } from './normalized-schema';

export function updateTsConfig(options: NormalizedSchema): Rule {
return chain([
(host: Tree, context: SchematicContext) => {
const nxJson = readJsonInTree<NxJson>(host, 'nx.json');
return updateJsonInTree('tsconfig.base.json', (json) => {
const c = json.compilerOptions;
c.paths = c.paths || {};
delete c.paths[options.name];
c.paths[`@${nxJson.npmScope}/${options.projectDirectory}`] = [
`${libsDir(host)}/${options.projectDirectory}/src/index.ts`,

if (c.paths[options.importPath]) {
throw new SchematicsException(
`You already have a library using the import path "${options.importPath}". Make sure to specify a unique one.`
);
}

c.paths[options.importPath] = [
`libs/${options.projectDirectory}/src/index.ts`,
];

return json;
})(host, context);
},
Expand Down
Loading

0 comments on commit 77378a0

Please sign in to comment.