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

feat: change file exclude option to allow multiple exclude globs #170

Merged
merged 1 commit into from
Mar 7, 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
6 changes: 3 additions & 3 deletions src/forge/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const forgeCommand = function (program) {
"Git URL, file path or npm package of a language-specific generator"
)
.option(
"-e, --exclude <glob>",
"A glob pattern that excludes files from the generator in the output",
""
"-e, --exclude <glob...>",
"A glob pattern or patterns that exclude files from the generator in the output",
[]
)
.option(
"-o, --output <path>",
Expand Down
5 changes: 4 additions & 1 deletion src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ function processTemplateFactory(
outputFolder
) {
return async function (file) {
if (options.exclude && minimatch(file, options.exclude)) {
if (
options.exclude &&
options.exclude.some((excludeGlob) => minimatch(file, excludeGlob))
) {
return;
}
log.verbose(`\n${log.brightYellowForeground}${file}${log.resetStyling}`);
Expand Down
24 changes: 23 additions & 1 deletion test/generate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ const Handlebars = require("handlebars");

const generate = require("../src/generate");
const generatorResolver = require("../src/common/generatorResolver");
const minimatch = require("minimatch");

jest.mock("fs");
jest.mock("path");
jest.mock("handlebars");
jest.mock("minimatch");
jest.mock("../src/common/generatorResolver");

describe("generate", () => {
Expand Down Expand Up @@ -50,7 +52,6 @@ describe("generate", () => {
skipValidation: true,
output: outDir,
});
console.error(fs.copyFileSync.mock.calls);
expect(fs.copyFileSync).toHaveBeenCalledWith(
`${generatorPath}/template/${fileName}.${fileExtension}`,
`${outDir}/${fileName}.${fileExtension}`
Expand Down Expand Up @@ -113,6 +114,27 @@ describe("generate", () => {
outCode
);
});
it("should not copy excluded files", async () => {
const file = "exampleFile.js";
mockReaddirSync({
helpers: [],
partials: [],
templates: [file],
});
minimatch.mockImplementation(
(fileToMatch, excludedFiles) =>
fileToMatch === file && excludedFiles.includes(file)
);
await generate(schemaPathOrUrl, generatorPath, {
skipValidation: true,
output: outDir,
exclude: [file],
});
expect(fs.copyFileSync).not.toHaveBeenCalled();
expect(fs.readFileSync).not.toHaveBeenCalledWith(
expect.stringContaining(file)
);
});
});

function mockReaddirSync({ helpers, partials, templates }) {
Expand Down