From 5840db16f018680cf66f25559ec943e56c6da798 Mon Sep 17 00:00:00 2001 From: Juri Date: Fri, 17 Jan 2020 09:57:48 +0100 Subject: [PATCH] fix(node): add npmScope prefix in library name 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. --- .../library/files/lib/package.json__tmpl__ | 2 +- .../src/schematics/library/library.spec.ts | 18 ++++++++++++++++++ .../node/src/schematics/library/library.ts | 10 +++++++--- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packages/node/src/schematics/library/files/lib/package.json__tmpl__ b/packages/node/src/schematics/library/files/lib/package.json__tmpl__ index fa518765a372f..9ebd4abc6585a 100644 --- a/packages/node/src/schematics/library/files/lib/package.json__tmpl__ +++ b/packages/node/src/schematics/library/files/lib/package.json__tmpl__ @@ -1,4 +1,4 @@ { - "name": "<%= name %>", + "name": "@<%= prefix %>/<%= name %>", "version": "0.0.1" } diff --git a/packages/node/src/schematics/library/library.spec.ts b/packages/node/src/schematics/library/library.spec.ts index 8e56e0c43596f..b26047ec4dae7 100644 --- a/packages/node/src/schematics/library/library.spec.ts +++ b/packages/node/src/schematics/library/library.spec.ts @@ -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; @@ -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'); + }); + }); }); diff --git a/packages/node/src/schematics/library/library.ts b/packages/node/src/schematics/library/library.ts index 1c600fa6498a7..7aa1b0d330c99 100644 --- a/packages/node/src/schematics/library/library.ts +++ b/packages/node/src/schematics/library/library.ts @@ -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; @@ -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), @@ -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}` @@ -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,