diff --git a/src/forge/index.js b/src/forge/index.js index a384012..2fdbfa4 100644 --- a/src/forge/index.js +++ b/src/forge/index.js @@ -20,9 +20,9 @@ const forgeCommand = function (program) { "Git URL, file path or npm package of a language-specific generator" ) .option( - "-e, --exclude ", - "A glob pattern that excludes files from the generator in the output", - "" + "-e, --exclude ", + "A glob pattern or patterns that exclude files from the generator in the output", + [] ) .option( "-o, --output ", diff --git a/src/generate.js b/src/generate.js index e3b2dca..66a0515 100644 --- a/src/generate.js +++ b/src/generate.js @@ -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}`); diff --git a/test/generate.test.js b/test/generate.test.js index 00b3336..9765d47 100644 --- a/test/generate.test.js +++ b/test/generate.test.js @@ -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", () => { @@ -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}` @@ -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 }) {