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

added support for default values in generator options. #166

Merged
merged 2 commits into from
Feb 6, 2023
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
8 changes: 6 additions & 2 deletions src/generatorOptions/generatorOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const generatorResolver = require("../common/generatorResolver");

const generatorOptionsPrefix = "generator.";

async function generatorOptions(generatorPathOrUrl) {
// generates the help text for the additional options that a generator supports
async function generatorOptionsHelp(generatorPathOrUrl) {
let optionsHelp = "";
const generatorPath = generatorResolver.getGenerator(generatorPathOrUrl);
const configFile = path.join(generatorPath, "config.json");
Expand Down Expand Up @@ -53,12 +54,15 @@ function configToCommanderOptions(config) {
commanderOption.choices(option.choices);
commanderOption.default(option.choices[0]);
}
if (option.default) {
commanderOption.default(option.default);
}
return commanderOption;
});
}

module.exports = {
generatorOptions,
generatorOptionsHelp,
configToCommanderOptions,
generatorOptionsPrefix,
};
4 changes: 2 additions & 2 deletions src/generatorOptions/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { generatorOptions } = require("./generatorOptions");
const { generatorOptionsHelp } = require("./generatorOptions");

const generatorOptionsCommand = function (program) {
program
Expand All @@ -11,7 +11,7 @@ const generatorOptionsCommand = function (program) {
"Git URL, file path or npm package of a language-specific generator"
)
.action(async (generator) => {
console.log(await generatorOptions(generator));
console.log(await generatorOptionsHelp(generator));
});
};

Expand Down
23 changes: 17 additions & 6 deletions test/generatorOptions.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const fs = require("fs");

const {
generatorOptions,
generatorOptionsHelp,
configToCommanderOptions,
} = require("../src/generatorOptions/generatorOptions");
const generatorResolver = require("../src/common/generatorResolver");

Expand All @@ -20,7 +21,7 @@ describe("generate", () => {

it("should indicate if the generator doesn't have any additional options", async () => {
fs.existsSync.mockReturnValue(false);
const optionsHelp = await generatorOptions("some/path");
const optionsHelp = await generatorOptionsHelp("some/path");
expect(optionsHelp).toEqual("The generator has no additional options");
});

Expand All @@ -31,7 +32,7 @@ describe("generate", () => {
},
};
fs.readFileSync.mockReturnValue(JSON.stringify(config));
const optionsHelp = await generatorOptions("some/path");
const optionsHelp = await generatorOptionsHelp("some/path");
expect(optionsHelp).toEqual(
// Not checking the full string because extra whitespace is created:
expect.stringMatching(
Expand All @@ -45,7 +46,7 @@ describe("generate", () => {
moduleFormat: {},
};
fs.readFileSync.mockReturnValue(JSON.stringify(config));
const optionsHelp = await generatorOptions("some/path");
const optionsHelp = await generatorOptionsHelp("some/path");
expect(optionsHelp).toEqual(expect.stringMatching(/moduleFormat/));
});

Expand All @@ -60,7 +61,7 @@ describe("generate", () => {
};

fs.readFileSync.mockReturnValue(JSON.stringify(config));
const optionsHelp = await generatorOptions("some/path");
const optionsHelp = await generatorOptionsHelp("some/path");

expect(optionsHelp).toEqual(
expect.stringMatching(/optionOne.*description One./)
Expand All @@ -77,11 +78,21 @@ describe("generate", () => {
},
};
fs.readFileSync.mockReturnValue(JSON.stringify(config));
const optionsHelp = await generatorOptions("some/path");
const optionsHelp = await generatorOptionsHelp("some/path");
expect(optionsHelp).toEqual(
expect.stringMatching(
/moduleFormat.*(choices: "cjs", "esm", default: "cjs")/
)
);
});

it("should support default values", async () => {
const config = {
packageName: {
default: "my-package",
},
};
const options = configToCommanderOptions(config);
expect(options[0].defaultValue).toEqual("my-package");
});
});