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
52 lines (40 loc) • 1.74 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = componentIdToPackageName;
function _npmRegistryName() {
const data = _interopRequireDefault(require("./npm-registry-name"));
_npmRegistryName = function () {
return data;
};
return data;
}
function _constants() {
const data = require("../../constants");
_constants = function () {
return data;
};
return data;
}
/**
* convert a component name to a valid npm package name
* e.g. BitId { scope: util, name: is-string } => @bit/util.is-string
*/
function componentIdToPackageName(id, bindingPrefix, defaultScope, // if an id doesn't have a scope, use defaultScope if exists
withPrefix = true) {
const allSlashes = new RegExp('/', 'g');
const name = id.name.replace(allSlashes, _constants().NODE_PATH_COMPONENT_SEPARATOR);
const scope = id.scope || defaultScope;
const partsToJoin = scope ? [scope, name] : [name];
let nameWithoutPrefix = partsToJoin.join(_constants().NODE_PATH_COMPONENT_SEPARATOR);
if (!withPrefix) return nameWithoutPrefix;
const registryPrefix = bindingPrefix || (0, _npmRegistryName().default)(); // Make sure we don't have the prefix also as part of the scope name
// since prefixes are now taken from the owner name, and the scope name has the owner name as well.
const registryPrefixWithDotWithoutAt = `${registryPrefix}.`.replace('@', '');
if (nameWithoutPrefix.startsWith(registryPrefixWithDotWithoutAt) && registryPrefix !== '@bit') {
nameWithoutPrefix = nameWithoutPrefix.replace(registryPrefixWithDotWithoutAt, '');
}
return `${registryPrefix}/${nameWithoutPrefix}`;
}
;