Skip to content

Commit

Permalink
feat: configuration for showing additional sub generator tiles on the…
Browse files Browse the repository at this point in the history
… main page (#862)
  • Loading branch information
alex-gilin authored Jan 7, 2025
1 parent 45975fe commit 6fa1330
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
33 changes: 33 additions & 0 deletions packages/backend/src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export type GeneratorData = {
generatorPackageJson: any;
};

type AdditionalGenerator = {
namespace: string;
displayName: string;
description: string;
homePage?: string;
image?: string;
};

export class GeneratorNotFoundError extends Error {
constructor(message: string) {
super(message);
Expand Down Expand Up @@ -174,6 +182,31 @@ class EnvUtil {
}
});

// lookup for additional generators
let additional: AdditionalGenerator[] = [];
gensData.forEach((genData) => {
additional = additional.concat(...(genData.generatorPackageJson.additional_generators ?? []));
});
// remove duplicates
additional = _.uniqBy(additional, "namespace");
// get additional generators data
if (additional.length) {
const additionalGensMeta = this.allInstalledGensMeta.filter((genMeta) =>
additional.find((gen) => gen.namespace === genMeta.namespace),
);
const additionalPackageJsons = await NpmCommand.getPackageJsons(additionalGensMeta);
const additionalGensData = additionalPackageJsons.map((generatorPackageJson: any | undefined, index: number) => {
if (generatorPackageJson) {
return {
generatorMeta: additionalGensMeta[index],
// populate additional generator properties with main generator package.json
generatorPackageJson: { ...generatorPackageJson, ...additional[index] },
};
}
});
gensData.push(...additionalGensData);
}

return _.compact(gensData);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/yeomanui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ export class YeomanUI {
let genImageUrl;

try {
genImageUrl = await datauri(path.join(genPackagePath, YeomanUI.YEOMAN_PNG));
genImageUrl = await datauri(path.join(genPackagePath, _.get(packageJson, "image", YeomanUI.YEOMAN_PNG)));
} catch (error) {
genImageUrl = defaultImage.default;
this.logger.debug(error);
Expand Down
82 changes: 82 additions & 0 deletions packages/backend/test/yeomanui.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,88 @@ describe("yeomanui unit test", () => {
expect(result.questions[0].choices).to.have.lengthOf(5);
});

it("get generators all generators and additional generators", async () => {
const gensMeta = [
{
generatorMeta: {
generatorPath: "test1Path/app/index.js",
namespace: "test1-project:app",
packagePath: "test1Path",
},
generatorPackageJson: {
"generator-filter": { type: "project" },
description: "test1Description",
},
},
{
generatorMeta: {
generatorPath: "test2Path/app/index.js",
namespace: "test2-module:app",
packagePath: "test2Path",
},
generatorPackageJson: {
"generator-filter": { type: "project_test" },
},
},
{
generatorMeta: {
generatorPath: "test3Path/app/index.js",
namespace: "test3:app",
packagePath: "test3Path",
},
generatorPackageJson: {
"generator-filter": { type: "module" },
},
},
{
generatorMeta: {
generatorPath: "test4Path/app/index.js",
namespace: "test4:app",
packagePath: "test4Path",
},
generatorPackageJson: {
"generator-filter": { type: "project" },
description: "test4Description",
additional_generators: [
{
namespace: "test4:subgen",
description: "test 4 sub gen description",
displayName: "Test Sub Gen 4",
},
],
},
},
{
generatorMeta: {
generatorPath: "test4Path/subgen/index.js",
namespace: "test4:subgen",
packagePath: "test4Path",
isAdditional: true,
},
generatorPackageJson: {
description: "test5Description",
additional_generators: [
{
namespace: "test4:subgen",
description: "test 4 sub gen description",
displayName: "Test Sub Gen 4",
},
],
},
},
];

envUtilsMock.expects("getGeneratorsData").resolves(gensMeta);
wsConfigMock.expects("get").withExactArgs("ApplicationWizard.HideGenerator").returns("");
yeomanUi["uiOptions"] = {
filter: GeneratorFilter.create({ type: [] }),
messages,
};
const result = await yeomanUi["getGeneratorsPrompt"]();

expect(result.questions[0].choices).to.have.lengthOf(5);
});

it("wrong generators filter type is provided", async () => {
wsConfigMock.expects("get").withExactArgs("ApplicationWizard.Workspace").returns({});
wsConfigMock.expects("get").withExactArgs("ApplicationWizard.HideGenerator").returns("");
Expand Down

0 comments on commit 6fa1330

Please sign in to comment.