Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nx-plugin): use importPath as package.json name when passed #9430

Merged
merged 1 commit into from Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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