From ba93545a67ee7b35796e17292990e4df27a9a819 Mon Sep 17 00:00:00 2001 From: Matthew Smith Date: Wed, 15 Feb 2023 12:29:07 +0000 Subject: [PATCH] feat: change file exclude option to allow multiple exclude globs --- src/forge/index.js | 6 +++--- src/generate.js | 5 ++++- test/generate.test.js | 24 +++++++++++++++++++++++- 3 files changed, 30 insertions(+), 5 deletions(-) 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 4b7be2c..e241139 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 }) {