diff --git a/lib/index.cjs b/lib/index.cjs new file mode 100644 index 0000000..972b268 --- /dev/null +++ b/lib/index.cjs @@ -0,0 +1,30 @@ +/** + * @fileoverview eslint ignore files automatically(cjs projects). + * @author 唯然 + */ +const process = require("process"); +const fs = require("fs"); +const path = require("path"); + +/** + * @param {object} [opts] + * @param {string} [opts.cwd] + * @param {string} [opts.filename] + * @param {string[]?} [opts.extraIgnores] + * @returns {{ignores: string[]}} the list of ignored files + */ +module.exports.ignores = function ignores(opts = {}) { + const ignoreFile = path.join( + opts.cwd || process.cwd(), + opts.filename || ".gitignore" + ); + const text = fs.readFileSync(ignoreFile, "utf-8"); + const lines = text + .split(/(\r\n|\n)+/) + .map((line) => line.trim()) + .filter((line) => line && !line.startsWith("#")); + + return { + ignores: [...lines, ...(opts.extraIgnores || [])], + }; +} diff --git a/lib/index.js b/lib/index.js index 7483189..dc1c786 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,5 +1,5 @@ /** - * @fileoverview eslint ignore files automatically. + * @fileoverview eslint ignore files automatically(esm projects). * @author 唯然 */ import process from "process"; diff --git a/package.json b/package.json index 5dff4aa..7b3a794 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,13 @@ "author": "唯然