textlint
Version:
The pluggable linting tool for natural language.
119 lines • 4.56 kB
JavaScript
// LICENSE : MIT
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.pathsToGlobPatterns = pathsToGlobPatterns;
exports.findFiles = findFiles;
const path_to_glob_pattern_1 = require("path-to-glob-pattern");
const glob = __importStar(require("glob"));
const node_path_1 = __importDefault(require("node:path"));
const node_fs_1 = __importDefault(require("node:fs"));
const debug_1 = __importDefault(require("debug"));
const debug = (0, debug_1.default)("textlint:find-util");
const DEFAULT_IGNORE_PATTERNS = Object.freeze(["**/.git/**", "**/node_modules/**"]);
const isFile = (filePath) => {
try {
return node_fs_1.default.statSync(filePath).isFile();
}
catch (error) {
return false;
}
};
/**
* filter files by config
* @param {string[]} patterns glob patterns
* @param {{extensions?: string[], cwd?: string }} options
*/
function pathsToGlobPatterns(patterns, options = {}) {
const processPatterns = (0, path_to_glob_pattern_1.pathToGlobPattern)({
extensions: options.extensions || [],
cwd: options.cwd || process.cwd()
});
return patterns.map(processPatterns);
}
/**
* found files by glob pattern
* @param {string[]} patterns
* @param {FindFilesOptions} options
* @returns {string[]} file path list
*/
function findFiles(patterns, options = {}) {
const cwd = options.cwd || process.cwd();
const ignoredPatterns = [];
ignoredPatterns.push(...DEFAULT_IGNORE_PATTERNS);
if (options.ignoreFilePath) {
const normalizeIgnoreFilePath = node_path_1.default.resolve(cwd, options.ignoreFilePath);
if (node_fs_1.default.existsSync(normalizeIgnoreFilePath)) {
debug("findFiles ignore, normalizeIgnoreFilePath: %s", normalizeIgnoreFilePath);
const ignored = node_fs_1.default
.readFileSync(normalizeIgnoreFilePath, "utf-8")
.split(/\r?\n/)
.filter((line) => !/^\s*$/.test(line) && !/^\s*#/.test(line));
debug("add ignored pattern: %o", ignored);
ignoredPatterns.push(...ignored);
}
}
debug("search patterns: %o", patterns);
debug("search ignore patterns: %o", ignoredPatterns);
const files = [];
const addFile = (filePath) => {
if (files.indexOf(filePath) === -1) {
files.push(filePath);
}
};
patterns.forEach((pattern) => {
const file = node_path_1.default.resolve(cwd, pattern);
if (isFile(file)) {
addFile(node_fs_1.default.realpathSync(file));
}
else {
glob.sync(pattern, {
cwd,
absolute: true,
nodir: true,
ignore: ignoredPatterns
}).forEach((filePath) => {
// workaround for windows
// https://github.com/isaacs/node-glob/issues/74#issuecomment-31548810
addFile(node_path_1.default.resolve(filePath));
});
}
});
return files;
}
//# sourceMappingURL=old-find-util.js.map