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(nxls): respect nx.json generator defaults when showing generate ui #2077

Merged
merged 1 commit into from
Mar 28, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ export async function getNxWorkspaceConfig(
.workspaceConfiguration;
error = `${e.stack}`;
}
} else {
workspaceConfiguration = (await readWorkspaceConfigs(workspacePath))
.workspaceConfiguration;
}
try {
process.exit = function (code?: number) {
Expand Down
8 changes: 8 additions & 0 deletions libs/shared/nx-console-plugins/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* eslint-disable */
export default {
coverageDirectory: '../../../coverage/libs/shared/schema',
globals: {},
displayName: 'shared-schema',
testEnvironment: 'node',
preset: '../../../jest.preset.js',
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
projectNameAndRootStartupMessage,
projectNameAndRootProcessor,
} from './project-name-and-root-plugin';
import { useGeneratorDefaultsProcessor } from './use-generator-defaults-processor';

export const internalPlugins: NxConsolePluginsDefinition = {
schemaProcessors: [
Expand All @@ -19,6 +20,7 @@ export const internalPlugins: NxConsolePluginsDefinition = {
prefillProjectAndDirProcessor,
nameAndDirectoryProcessor,
addProjectItemsToOptionProcessor,
useGeneratorDefaultsProcessor,
],
validators: [],
startupMessageFactories: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { NxWorkspace } from '@nx-console/shared/types';
import { useGeneratorDefaultsProcessor } from './use-generator-defaults-processor';
import { GeneratorSchema } from '@nx-console/shared/generate-ui-types';
const mockLogger = {
log: (value: string) => {
// noop
},
};
const defaultSchema: GeneratorSchema = {
collectionName: 'my-collection',
generatorName: 'my-generator',
options: [
{
name: 'option1',
default: 'default1',
aliases: [],
isRequired: false,
},
{
name: 'option2',
default: 'default2',
aliases: [],
isRequired: false,
},
],
description: '',
};

describe('useGeneratorDefaultsProcessor', () => {
it('should update options with default values from nx.json - nested', () => {
const workspace = {
workspace: {
generators: {
'my-collection': {
'my-generator': {
option1: 'updated-default1',
option2: 'updated-default2',
},
},
},
},
};

const processedSchema = useGeneratorDefaultsProcessor(
defaultSchema,
workspace as any as NxWorkspace,
mockLogger
);

expect(processedSchema.options).toEqual([
{
name: 'option1',
default: 'updated-default1',
aliases: [],
isRequired: false,
},
{
name: 'option2',
default: 'updated-default2',
aliases: [],
isRequired: false,
},
]);
});

it('should update options with default values from nx.json - flat', () => {
const workspace = {
workspace: {
generators: {
'my-collection:my-generator': {
option1: 'updated-default1',
option2: 'updated-default2',
},
},
},
};

const processedSchema = useGeneratorDefaultsProcessor(
defaultSchema,
workspace as any as NxWorkspace,
mockLogger
);

expect(processedSchema.options).toEqual([
{
name: 'option1',
default: 'updated-default1',
aliases: [],
isRequired: false,
},
{
name: 'option2',
default: 'updated-default2',
aliases: [],
isRequired: false,
},
]);
});

it('should return the original schema if nx.json entry is not found', () => {
const workspace = {
workspace: {
generators: {},
},
};

const processedSchema = useGeneratorDefaultsProcessor(
defaultSchema,
workspace as any as NxWorkspace,
mockLogger
);

expect(processedSchema).toEqual(defaultSchema);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { GeneratorSchema } from '@nx-console/shared/generate-ui-types';
import { SchemaProcessor } from '../nx-console-plugin-types';
import { NxWorkspace } from '@nx-console/shared/types';

export const useGeneratorDefaultsProcessor: SchemaProcessor = (
schema: GeneratorSchema,
workspace: NxWorkspace
) => {
const nxJsonGeneratorsEntry =
workspace.workspace?.generators?.[schema.collectionName]?.[
schema.generatorName
] ??
workspace.workspace?.generators?.[
`${schema.collectionName}:${schema.generatorName}`
];

if (!nxJsonGeneratorsEntry) {
return schema;
}

return {
...schema,
options: (schema.options ?? []).map((option) => {
if (nxJsonGeneratorsEntry[option.name]) {
option.default = nxJsonGeneratorsEntry[option.name];
}
return option;
}),
};
};
3 changes: 3 additions & 0 deletions libs/shared/nx-console-plugins/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
9 changes: 9 additions & 0 deletions libs/shared/nx-console-plugins/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": ["**/*.spec.ts", "**/*.test.ts", "**/*.d.ts", "jest.config.ts"]
}
Loading