UNPKG

bit-bin

Version:

<a href="https://opensource.org/licenses/Apache-2.0"><img alt="apache" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"></a> <a href="https://github.com/teambit/bit/blob/master/CONTRIBUTING.md"><img alt="prs" src="https://img.shields.io/b

110 lines (77 loc) 2.53 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; function path() { const data = _interopRequireWildcard(require("path")); path = function () { return data; }; return data; } function _glob() { const data = _interopRequireDefault(require("glob")); _glob = function () { return data; }; return data; } function _bitId() { const data = require("../bit-id"); _bitId = function () { return data; }; return data; } function _constants() { const data = require("../constants"); _constants = function () { return data; }; return data; } function _exceptions() { const data = require("../scope/exceptions"); _exceptions = function () { return data; }; return data; } function _logger() { const data = _interopRequireDefault(require("../logger/logger")); _logger = function () { return data; }; return data; } function getLatestVersion(bitId, componentsDir) { // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! if (bitId.hasVersion()) return bitId.version; const regexRemoveLatestVersion = new RegExp(`${_constants().LATEST_BIT_VERSION}$`); const relativePathWithoutVersion = bitId.toFullPath().replace(regexRemoveLatestVersion, ''); const pathWithoutVersion = path().join(componentsDir, relativePathWithoutVersion); const versionsDirs = _glob().default.sync('*', { cwd: pathWithoutVersion }); if (!versionsDirs || !versionsDirs.length) { throw new (_exceptions().ComponentNotFound)(bitId.toString()); } // @ts-ignore return Math.max(versionsDirs); } function componentResolver(componentId, mainFilePath, projectRoot = process.cwd()) { const bitId = _bitId().BitId.parse(componentId, true); // used for envs. components, all have a scope const componentsDir = path().join(projectRoot, _constants().BITS_DIRNAME); const version = getLatestVersion(bitId, componentsDir); const bitIdWithLatestVersion = bitId.changeVersion(version.toString()); const componentPath = path().join(componentsDir, bitIdWithLatestVersion.toFullPath()); _logger().default.debug(`resolving component, path: ${componentPath}`); if (mainFilePath) { return path().join(componentPath, mainFilePath); } return componentPath; } var _default = componentResolver; exports.default = _default;