UNPKG

textlint

Version:

The pluggable linting tool for text and markdown.

164 lines 7.59 kB
// LICENSE : MIT "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createLinter = void 0; const kernel_1 = require("@textlint/kernel"); const old_find_util_1 = require("./util/old-find-util"); const execute_file_backer_manager_1 = require("./engine/execute-file-backer-manager"); const cache_backer_1 = require("./engine/execute-file-backers/cache-backer"); const path_1 = __importDefault(require("path")); const crypto_1 = __importDefault(require("crypto")); const read_pkg_up_1 = __importDefault(require("read-pkg-up")); const promises_1 = __importDefault(require("fs/promises")); const logger_1 = require("./util/logger"); const debug_1 = __importDefault(require("debug")); const separate_by_availability_1 = require("./util/separate-by-availability"); const find_util_1 = require("./util/find-util"); const debug = (0, debug_1.default)("textlint:createTextlint"); const createHashForDescriptor = (descriptor) => { try { const version = read_pkg_up_1.default.sync({ cwd: __dirname }).pkg.version; const toString = JSON.stringify(descriptor.toJSON()); const md5 = crypto_1.default.createHash("md5"); return md5.update(`${version}-${toString}`, "utf8").digest("hex"); } catch (error) { // Fallback for some env // https://github.com/textlint/textlint/issues/597 logger_1.Logger.warn("Use random value as hash because calculating hash value throw error", error); return crypto_1.default.randomBytes(20).toString("hex"); } }; const createLinter = (options) => { var _a, _b, _c; const cwd = (_a = options.cwd) !== null && _a !== void 0 ? _a : process.cwd(); const executeFileBackerManger = new execute_file_backer_manager_1.ExecuteFileBackerManager(); const cacheBaker = new cache_backer_1.CacheBacker({ cache: (_b = options.cache) !== null && _b !== void 0 ? _b : false, cacheLocation: (_c = options.cacheLocation) !== null && _c !== void 0 ? _c : path_1.default.resolve(process.cwd(), ".textlintcache"), hash: createHashForDescriptor(options.descriptor) }); if (options.cache) { executeFileBackerManger.add(cacheBaker); } else { cacheBaker.destroyCache(); } const kernel = new kernel_1.TextlintKernel({ quiet: options.quiet }); const baseOptions = options.descriptor.toKernelOptions(); return { /** * Lint files * Note: lintFiles respect ignore file * @param {String[]} filesOrGlobs An array of file path and directory names, or glob. * @returns {Promise<TextlintResult[]>} The results for all files that were linted. */ async lintFiles(filesOrGlobs) { const patterns = (0, old_find_util_1.pathsToGlobPatterns)(filesOrGlobs, { extensions: options.descriptor.availableExtensions }); const targetFiles = (0, old_find_util_1.findFiles)(patterns, { ignoreFilePath: options.ignoreFilePath }); const { availableFiles, unAvailableFiles } = (0, separate_by_availability_1.separateByAvailability)(targetFiles, { extensions: options.descriptor.availableExtensions }); debug("Available extensions: %j", options.descriptor.availableExtensions); debug("Process files: %j", availableFiles); debug("No Process files that are un-support extensions: %j", unAvailableFiles); return executeFileBackerManger.process(availableFiles, async (filePath) => { const absoluteFilePath = path_1.default.resolve(process.cwd(), filePath); const fileContent = await promises_1.default.readFile(filePath, "utf-8"); const kernelOptions = { ext: path_1.default.extname(filePath), filePath: absoluteFilePath, ...baseOptions }; return kernel.lintText(fileContent, kernelOptions); }); }, /** * Lint text * Note: lintText does not respect ignore file * You can detect the file path is ignored or not by `scanFilePath()` * @param text * @param filePath */ async lintText(text, filePath) { const kernelOptions = { ext: path_1.default.extname(filePath), filePath, ...baseOptions }; return kernel.lintText(text, kernelOptions); }, /** * Lint files and fix them * Note: fixFiles respect ignore file * @param fileOrGlobs An array of file path and directory names, or glob. * @returns {Promise<TextlintFixResult[]>} The results for all files that were linted and fixed. */ async fixFiles(fileOrGlobs) { const patterns = (0, old_find_util_1.pathsToGlobPatterns)(fileOrGlobs, { extensions: options.descriptor.availableExtensions }); const targetFiles = (0, old_find_util_1.findFiles)(patterns, { ignoreFilePath: options.ignoreFilePath }); const { availableFiles, unAvailableFiles } = (0, separate_by_availability_1.separateByAvailability)(targetFiles, { extensions: options.descriptor.availableExtensions }); debug("Available extensions: %j", options.descriptor.availableExtensions); debug("Process files: %j", availableFiles); debug("No Process files that are un-support extensions: %j", unAvailableFiles); return executeFileBackerManger.process(availableFiles, async (filePath) => { const absoluteFilePath = path_1.default.resolve(process.cwd(), filePath); const fileContent = await promises_1.default.readFile(filePath, "utf-8"); const kernelOptions = { ext: path_1.default.extname(filePath), filePath: absoluteFilePath, ...baseOptions }; return kernel.fixText(fileContent, kernelOptions); }); }, /** * Lint text and fix it * Note: fixText does not respect ignore file * You can detect the file path is ignored or not by `scanFilePath()` * @param text * @param filePath */ async fixText(text, filePath) { const kernelOptions = { ext: path_1.default.extname(filePath), filePath, ...baseOptions }; return kernel.fixText(text, kernelOptions); }, /** * Scan file path and return scan result * If you want to know the file is ignored by ignore file, use this function * Return { status "ok" | "ignored" | "error" } object: * - ok: found file and allow to lint/fix * - ignored: found file, and it is ignored by ignore file * - error: not found file * @param filePath * @returns {Promise<ScanFilePathResult>} */ async scanFilePath(filePath) { return (0, find_util_1.scanFilePath)(filePath, { cwd, ignoreFilePath: options.ignoreFilePath }); } }; }; exports.createLinter = createLinter; //# sourceMappingURL=createLinter.js.map