Skip to content

Commit

Permalink
refactor: add project names to project option in nxc plugin (#1961)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKless authored Jan 2, 2024
1 parent d1b2abe commit 4236bbb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,8 @@ class NxGenerateService(val project: Project) {
options: List<NxGeneratorOption>? = null
) {
val generatorOptions =
if (options != null) options
else {
val nxProjectNames =
project.service<NxlsService>().workspace()?.workspace?.projects?.keys?.toList()
?: emptyList()
project
options
?: project
.service<NxlsService>()
.generatorOptions(
NxGeneratorOptionsRequestOptions(
Expand All @@ -127,17 +123,6 @@ class NxGenerateService(val project: Project) {
generator.path
)
)
.map {
if (
it.name == "project" ||
it.name == "projectName" ||
it.dropdown == "projects"
) {
it.items = nxProjectNames
}
it
}
}

val generatorWithOptions = NxGenerator(generator, generatorOptions)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { GeneratorSchema } from '@nx-console/shared/generate-ui-types';
import { SchemaProcessor } from '../nx-console-plugin-types';
import { NxWorkspace } from '@nx-console/shared/types';
import { isProjectOption } from '@nx-console/shared/schema';

export const addProjectItemsToOptionProcessor: SchemaProcessor = (
schema: GeneratorSchema,
workspace: NxWorkspace
) => {
return {
...schema,
options: (schema.options ?? []).map((option) => {
if (isProjectOption(option)) {
const projects = Object.keys(workspace.workspace.projects);
option.items = projects.sort();
}
return option;
}),
};
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NxConsolePluginsDefinition } from '../nx-console-plugin-types';
import { addProjectItemsToOptionProcessor } from './add-project-items-to-option-processor';
import { filterInternalAndDeprecatedProcessor } from './filter-internal-and-deprecated-processor';
import { gitCleanMessageFactory } from './git-clean-message-factory';
import {
Expand All @@ -17,6 +18,7 @@ export const internalPlugins: NxConsolePluginsDefinition = {
filterInternalAndDeprecatedProcessor,
prefillProjectAndDirProcessor,
nameAndDirectoryProcessor,
addProjectItemsToOptionProcessor,
],
validators: [],
startupMessageFactories: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,8 @@ export async function openGenerateUi(
};
}

generateUIWebview.openGenerateUi(
await augmentGeneratorSchema({
...generator,
context: generatorContext,
})
);
}

async function augmentGeneratorSchema(
generatorSchema: GeneratorSchema
): Promise<GeneratorSchema> {
for (const option of generatorSchema.options) {
if (isProjectOption(option)) {
const projects = Object.entries(await getNxWorkspaceProjects());
option.items = projects.map((entry) => entry[0]).sort();
}
}

return generatorSchema;
generateUIWebview.openGenerateUi({
...generator,
context: generatorContext,
});
}

0 comments on commit 4236bbb

Please sign in to comment.