diff --git a/packages/nx-plugin/src/generators/generator/generator.ts b/packages/nx-plugin/src/generators/generator/generator.ts index 3592b07cf6a87..601c3ab42f1e4 100644 --- a/packages/nx-plugin/src/generators/generator/generator.ts +++ b/packages/nx-plugin/src/generators/generator/generator.ts @@ -1,14 +1,15 @@ +import type { Tree } from '@nrwl/devkit'; import { - readProjectConfiguration, - names, convertNxGenerator, generateFiles, - updateJson, getWorkspaceLayout, + names, + readJson, + readProjectConfiguration, + updateJson, } from '@nrwl/devkit'; -import type { Tree } from '@nrwl/devkit'; -import type { Schema } from './schema'; import * as path from 'path'; +import type { Schema } from './schema'; interface NormalizedSchema extends Schema { fileName: string; @@ -26,7 +27,10 @@ function normalizeOptions(host: Tree, options: Schema): NormalizedSchema { const { root: projectRoot, sourceRoot: projectSourceRoot } = readProjectConfiguration(host, options.project); - const npmPackageName = `@${npmScope}/${options.project}`; + const npmPackageName = readJson<{ name: string }>( + host, + path.join(projectRoot, 'package.json') + ).name; let description: string; if (options.description) { diff --git a/packages/nx-plugin/src/generators/plugin/plugin.spec.ts b/packages/nx-plugin/src/generators/plugin/plugin.spec.ts index f4906533808fc..1deec71cbcf51 100644 --- a/packages/nx-plugin/src/generators/plugin/plugin.spec.ts +++ b/packages/nx-plugin/src/generators/plugin/plugin.spec.ts @@ -1,5 +1,10 @@ import { pluginGenerator } from './plugin'; -import { Tree, readProjectConfiguration } from '@nrwl/devkit'; +import { + Tree, + readProjectConfiguration, + readJson, + joinPathFragments, +} from '@nrwl/devkit'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import { Schema } from './schema'; import { Linter } from '@nrwl/linter'; @@ -168,4 +173,33 @@ describe('NxPlugin Plugin Generator', () => { expect(build.executor).toEqual('@nrwl/js:swc'); }); }); + + describe('--importPath', () => { + it('should use the workspace npmScope by default for the package.json', async () => { + await pluginGenerator(tree, getSchema()); + + const { root } = readProjectConfiguration(tree, 'my-plugin'); + const { name } = readJson<{ name: string }>( + tree, + joinPathFragments(root, 'package.json') + ); + + expect(name).toEqual('@proj/my-plugin'); + }); + + it('should use importPath as the package.json name', async () => { + await pluginGenerator( + tree, + getSchema({ importPath: '@my-company/my-plugin' }) + ); + + const { root } = readProjectConfiguration(tree, 'my-plugin'); + const { name } = readJson<{ name: string }>( + tree, + joinPathFragments(root, 'package.json') + ); + + expect(name).toEqual('@my-company/my-plugin'); + }); + }); }); diff --git a/packages/nx-plugin/src/generators/plugin/plugin.ts b/packages/nx-plugin/src/generators/plugin/plugin.ts index 4e25f74af3765..69ad664e7bc44 100644 --- a/packages/nx-plugin/src/generators/plugin/plugin.ts +++ b/packages/nx-plugin/src/generators/plugin/plugin.ts @@ -46,7 +46,8 @@ function normalizeOptions(host: Tree, options: Schema): NormalizedSchema { const parsedTags = options.tags ? options.tags.split(',').map((s) => s.trim()) : []; - const npmPackageName = `@${npmScope}/${name}`; + + const npmPackageName = options.importPath || `@${npmScope}/${name}`; return { ...options, @@ -128,8 +129,9 @@ export async function pluginGenerator(host: Tree, schema: Schema) { ...schema, config: options.standaloneConfig !== false ? 'project' : 'workspace', buildable: true, - importPath: schema.importPath ?? options.npmPackageName, + importPath: options.npmPackageName, }); + tasks.push(libraryTask); const installTask = addDependenciesToPackageJson(