textlint
Version:
The pluggable linting tool for text and markdown.
110 lines • 4.23 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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__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.findFiles = exports.pathsToGlobPatterns = void 0;
const path_to_glob_pattern_1 = require("path-to-glob-pattern");
const glob = __importStar(require("glob"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("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 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);
}
exports.pathsToGlobPatterns = pathsToGlobPatterns;
/**
* 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 = path_1.default.resolve(cwd, options.ignoreFilePath);
if (fs_1.default.existsSync(normalizeIgnoreFilePath)) {
debug("findFiles ignore, normalizeIgnoreFilePath: %s", normalizeIgnoreFilePath);
const ignored = 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 = path_1.default.resolve(cwd, pattern);
if (isFile(file)) {
addFile(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(path_1.default.resolve(filePath));
});
}
});
return files;
}
exports.findFiles = findFiles;
//# sourceMappingURL=old-find-util.js.map