Skip to content

Commit

Permalink
fix(node): add npmScope prefix in library name
Browse files Browse the repository at this point in the history
Publishable node libraries should use the default npmScope prefix like @proj/mylib. Otherwise it
might lead to inconsistencies when using the library within the monorepo with @proj/mylib and when
publishing it.
  • Loading branch information
juristr authored and vsavkin committed Feb 4, 2020
1 parent e5e0e31 commit 5840db1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
@@ -1,4 +1,4 @@
{
"name": "<%= name %>",
"name": "@<%= prefix %>/<%= name %>",
"version": "0.0.1"
}
18 changes: 18 additions & 0 deletions packages/node/src/schematics/library/library.spec.ts
Expand Up @@ -2,6 +2,7 @@ import { Tree } from '@angular-devkit/schematics';
import { NxJson, readJsonInTree } from '@nrwl/workspace';
import { createEmptyWorkspace } from '@nrwl/workspace/testing';
import { runSchematic } from '../../utils/testing';
import { expectTestsPass } from 'e2e/utils';

describe('lib', () => {
let appTree: Tree;
Expand Down Expand Up @@ -239,4 +240,21 @@ describe('lib', () => {
).toEqual(['libs/my-lib/tsconfig.lib.json']);
});
});

describe('publishable package', () => {
it('should update package.json', async () => {
const publishableTree = await runSchematic(
'lib',
{ name: 'mylib', publishable: true },
appTree
);

let packageJsonContent = readJsonInTree(
publishableTree,
'libs/mylib/package.json'
);

expect(packageJsonContent.name).toEqual('@proj/mylib');
});
});
});
10 changes: 7 additions & 3 deletions packages/node/src/schematics/library/library.ts
Expand Up @@ -21,12 +21,14 @@ import {
offsetFromRoot,
toFileName,
updateJsonInTree,
updateWorkspaceInTree
updateWorkspaceInTree,
getNpmScope
} from '@nrwl/workspace';
import { Schema } from './schema';

export interface NormalizedSchema extends Schema {
name: string;
prefix: string;
fileName: string;
projectRoot: Path;
projectDirectory: string;
Expand All @@ -35,7 +37,7 @@ export interface NormalizedSchema extends Schema {

export default function(schema: NormalizedSchema): Rule {
return (host: Tree, context: SchematicContext) => {
const options = normalizeOptions(schema);
const options = normalizeOptions(host, schema);

return chain([
externalSchematic('@nrwl/workspace', 'lib', schema),
Expand All @@ -47,7 +49,8 @@ export default function(schema: NormalizedSchema): Rule {
};
}

function normalizeOptions(options: Schema): NormalizedSchema {
function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
const defaultPrefix = getNpmScope(host);
const name = toFileName(options.name);
const projectDirectory = options.directory
? `${toFileName(options.directory)}/${name}`
Expand All @@ -63,6 +66,7 @@ function normalizeOptions(options: Schema): NormalizedSchema {

const normalized: NormalizedSchema = {
...options,
prefix: defaultPrefix, // we could also allow customizing this
fileName,
name: projectName,
projectRoot,
Expand Down

0 comments on commit 5840db1

Please sign in to comment.