UNPKG

workspace-tools

Version:

A collection of utilities that are useful in a git-controlled monorepo managed by one of these tools:

38 lines 1.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.getPackagesByFiles = void 0; const micromatch_1 = __importDefault(require("micromatch")); const path_1 = __importDefault(require("path")); const getWorkspaces_1 = require("./getWorkspaces"); function getPackagesByFiles(cwdOrOptions, files, ignoreGlobs, returnAllPackagesOnNoMatch) { let root; if (typeof cwdOrOptions === "string") { root = cwdOrOptions; files = files; } else { ({ root, files, ignoreGlobs, returnAllPackagesOnNoMatch } = cwdOrOptions); } const workspaces = (0, getWorkspaces_1.getWorkspaces)(root); const ignoreSet = new Set(ignoreGlobs?.length ? (0, micromatch_1.default)(files, ignoreGlobs) : []); const filteredFiles = files.filter((change) => !ignoreSet.has(change)); const packages = new Set(); for (const file of filteredFiles) { const candidates = workspaces.filter((pkg) => file.startsWith(path_1.default.relative(root, pkg.path).replace(/\\/g, "/"))); if (candidates.length) { const found = candidates.reduce((found, item) => { return found.path.length > item.path.length ? found : item; }, candidates[0]); packages.add(found.name); } else if (returnAllPackagesOnNoMatch) { return workspaces.map((pkg) => pkg.name); } } return [...packages]; } exports.getPackagesByFiles = getPackagesByFiles; //# sourceMappingURL=getPackagesByFiles.js.map