diff --git a/src/index.ts b/src/index.ts index 3d0428d..5da6f31 100644 --- a/src/index.ts +++ b/src/index.ts @@ -88,7 +88,14 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions, const ignoreManualFilesNames = options.ignore ? options.ignorePath || [] : []; const ignoreManualFilesPaths = ignoreManualFilesNames.map((fileName) => path.resolve(fileName)); - const ignoreManualFilesContents = await Promise.all(ignoreManualFilesPaths.map((filePath) => fs.readFile(filePath, "utf8"))); + const ignoreManualFilesContents = await Promise.all(ignoreManualFilesPaths.map(async (filePath) => { + try { + return await fs.readFile(filePath, "utf8"); + } catch { + // Treat a missing file as if it was empty (no ignores) + return ""; + } + })); const ignoreManualFoldersPaths = ignoreManualFilesPaths.map(path.dirname); const ignoreManual = getIgnoreBys(ignoreManualFoldersPaths, ignoreManualFilesContents.map(castArray)); diff --git a/test/__fixtures__/ignore-path/file-info-test/.customignore b/test/__fixtures__/ignore-path/file-info-test/.customignore new file mode 100644 index 0000000..b1f93ab --- /dev/null +++ b/test/__fixtures__/ignore-path/file-info-test/.customignore @@ -0,0 +1 @@ +ignored-by-customignore.js diff --git a/test/__fixtures__/ignore-path/file-info-test/.prettierignore b/test/__fixtures__/ignore-path/file-info-test/.prettierignore new file mode 100644 index 0000000..34fe7c3 --- /dev/null +++ b/test/__fixtures__/ignore-path/file-info-test/.prettierignore @@ -0,0 +1 @@ +ignored-by-prettierignore.js diff --git a/test/__fixtures__/ignore-path/ignore-path-test/.customignore b/test/__fixtures__/ignore-path/ignore-path-test/.customignore new file mode 100644 index 0000000..b1f93ab --- /dev/null +++ b/test/__fixtures__/ignore-path/ignore-path-test/.customignore @@ -0,0 +1 @@ +ignored-by-customignore.js diff --git a/test/__fixtures__/ignore-path/ignore-path-test/.gitignore b/test/__fixtures__/ignore-path/ignore-path-test/.gitignore new file mode 100644 index 0000000..a6c7c28 --- /dev/null +++ b/test/__fixtures__/ignore-path/ignore-path-test/.gitignore @@ -0,0 +1 @@ +*.js diff --git a/test/__fixtures__/ignore-path/ignore-path-test/.prettierignore b/test/__fixtures__/ignore-path/ignore-path-test/.prettierignore new file mode 100644 index 0000000..34fe7c3 --- /dev/null +++ b/test/__fixtures__/ignore-path/ignore-path-test/.prettierignore @@ -0,0 +1 @@ +ignored-by-prettierignore.js diff --git a/test/__tests__/ignore-path.js b/test/__tests__/ignore-path.js new file mode 100644 index 0000000..9d059ee --- /dev/null +++ b/test/__tests__/ignore-path.js @@ -0,0 +1,60 @@ +import { runCli } from "../utils"; +import fs from "node:fs/promises"; + +// `.js` files are ignored in `.gitignore` +const files = [ + "ignored-by-customignore.js", + "ignored-by-gitignore.js", + "ignored-by-prettierignore.js", +].map( + (file) => + new URL(`../__fixtures__/ignore-path/ignore-path-test/${file}`, import.meta.url), +); +const clean = () => + Promise.all(files.map((file) => fs.rm(file, { force: true }))); +const setup = () => + Promise.all(files.map((file) => fs.writeFile(file, " a+ b"))); + +beforeAll(async () => { + await setup(); +}); +afterAll(clean); + +const getUnformattedFiles = async (args) => { + const { stdout } = await runCli("ignore-path/ignore-path-test/", [ + "**/*.js", + "-l", + ...args, + ]); + return stdout ? stdout.split("\n").sort() : []; +}; + +test("custom ignore path", async () => { + expect(await getUnformattedFiles(["--ignore-path", ".customignore"])).toEqual( + ["ignored-by-gitignore.js", "ignored-by-prettierignore.js"], + ); +}); + +test("ignore files by .prettierignore and .gitignore by default", async () => { + expect( + await getUnformattedFiles(["--ignore-path", ".non-exists-ignore-file"]), + ).toEqual([ + "ignored-by-customignore.js", + "ignored-by-gitignore.js", + "ignored-by-prettierignore.js", + ]); + expect(await getUnformattedFiles([])).toEqual([]); +}); + +test("multiple `--ignore-path`", async () => { + expect( + await getUnformattedFiles([ + "--ignore-path", + ".customignore", + "--ignore-path", + ".prettierignore", + "--ignore-path", + ".non-exists-ignore-file", + ]), + ).toEqual(["ignored-by-gitignore.js"]); +});