Skip to content

Commit

Permalink
feat: change file exclude option to allow multiple exclude globs (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
ms14981 committed Mar 7, 2023
1 parent 06b6ed3 commit 733af31
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
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

0 comments on commit 733af31

Please sign in to comment.