Skip to content

Commit

Permalink
fix(nx-plugin): handle nx.json without npmScope during plugin generation
Browse files Browse the repository at this point in the history
  • Loading branch information
juristr committed May 25, 2022
1 parent 0c30f08 commit 620b858
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/nx-plugin/src/generators/plugin/plugin.spec.ts
Expand Up @@ -187,6 +187,23 @@ describe('NxPlugin Plugin Generator', () => {
expect(name).toEqual('@proj/my-plugin');
});

it('should correctly setup npmScope less workspaces', async () => {
// remove the npmScope from nx.json
const nxJson = JSON.parse(tree.read('nx.json')!.toString());
delete nxJson.npmScope;
tree.write('nx.json', JSON.stringify(nxJson));

await pluginGenerator(tree, getSchema());

const { root } = readProjectConfiguration(tree, 'my-plugin');
const { name } = readJson<{ name: string }>(
tree,
joinPathFragments(root, 'package.json')
);

expect(name).toEqual('my-plugin');
});

it('should use importPath as the package.json name', async () => {
await pluginGenerator(
tree,
Expand Down
11 changes: 10 additions & 1 deletion packages/nx-plugin/src/generators/plugin/plugin.ts
Expand Up @@ -47,7 +47,8 @@ function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
? options.tags.split(',').map((s) => s.trim())
: [];

const npmPackageName = options.importPath || `@${npmScope}/${name}`;
const npmPackageName =
options.importPath || resolvePackageName(npmScope, name);

return {
...options,
Expand Down Expand Up @@ -163,5 +164,13 @@ export async function pluginGenerator(host: Tree, schema: Schema) {
return runTasksInSerial(...tasks);
}

function resolvePackageName(npmScope: string, name: string): string {
if (npmScope && npmScope !== '') {
return `@${npmScope}/${name}`;
} else {
return name;
}
}

export default pluginGenerator;
export const pluginSchematic = convertNxGenerator(pluginGenerator);

0 comments on commit 620b858

Please sign in to comment.