Skip to content

Commit

Permalink
fix(nx-plugin): use importPath as package.json name when passed (#9430)
Browse files Browse the repository at this point in the history
  • Loading branch information
gioragutt committed Mar 21, 2022
1 parent 71ba52a commit 7fb441b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
16 changes: 10 additions & 6 deletions 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;
Expand All @@ -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) {
Expand Down
36 changes: 35 additions & 1 deletion 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';
Expand Down Expand Up @@ -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');
});
});
});
6 changes: 4 additions & 2 deletions packages/nx-plugin/src/generators/plugin/plugin.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 7fb441b

Please sign in to comment.