UNPKG

diffjam

Version:
80 lines (79 loc) 3.67 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.gitIgnoreToGlob = exports.GitIgnore = void 0; /* Manually parse .gitignore files into glob patterns in case git ls-tree fails or we are running the vscode extension. */ var micromatch_1 = __importDefault(require("micromatch")); var fs_1 = __importDefault(require("fs")); var lodash_1 = require("lodash"); var GitIgnore = /** @class */ (function () { function GitIgnore(gitIgnoreFileName) { if (gitIgnoreFileName === void 0) { gitIgnoreFileName = '.gitignore'; } var _this = this; this.gitIgnoreFileName = gitIgnoreFileName; this.ready = new Promise(function (resolve) { fs_1.default.readFile(gitIgnoreFileName, { encoding: "utf8" }, function (err, fileContents) { if (err) return resolve(undefined); // This is not an error, just means there is no .gitignore file var patterns = gitIgnoreToGlob(fileContents); var _a = (0, lodash_1.partition)(patterns, function (pattern) { return pattern.startsWith("!"); }), positive = _a[0], include = _a[1]; _this.patterns = { positive: positive, include: include }; resolve(); }); }); } GitIgnore.prototype.isIgnored = function (file) { if (!this.patterns) return false; return !micromatch_1.default.all(file, this.patterns.positive) || micromatch_1.default.any(file, this.patterns.include); }; return GitIgnore; }()); exports.GitIgnore = GitIgnore; // Copied with minor modifications from https://github.com/EE/gitignore-to-glob // which is licensed under the MIT license. function gitIgnoreToGlob(fileContents) { return fileContents.split('\n') // Filter out empty lines and comments. .filter(function (pattern) { return !!pattern && pattern[0] !== '#'; }) // '!' in .gitignore and glob mean opposite things so we need to swap it. // Return pairt [ignoreFlag, pattern], we'll concatenate it later. .map(function (pattern) { return pattern[0] === '!' ? ['', pattern.substring(1)] : ['!', pattern]; }) // Filter out hidden files/directories (i.e. starting with a dot). .filter(function (patternPair) { var pattern = patternPair[1]; return (pattern.indexOf('/.') === -1 && pattern.indexOf('.') !== 0); }) // Patterns not starting with '/' are in fact "starting" with '**/'. Since that would // catch a lot of files, restrict it to directories we check. // Patterns starting with '/' are relative to the project directory and glob would // treat them as relative to the OS root directory so strip the slash then. .map(function (patternPair) { var pattern = patternPair[1]; if (pattern[0] !== '/') { return [ patternPair[0], "**/".concat(pattern), ]; } return [patternPair[0], pattern.substring(1)]; }) // We don't know whether a pattern points to a directory or a file and we need files. // Therefore, include both `pattern` and `pattern/**` for every pattern in the array. .reduce(function (result, patternPair) { var pattern = patternPair.join(''); result.push(pattern); result.push("".concat(pattern, "/**")); return result; }, []); } exports.gitIgnoreToGlob = gitIgnoreToGlob;