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

239 lines (177 loc) 5.67 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.Packer = void 0; function _bluebird() { const data = require("bluebird"); _bluebird = function () { return data; }; return data; } function _defineProperty2() { const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); _defineProperty2 = function () { return data; }; return data; } function _path() { const data = _interopRequireDefault(require("path")); _path = function () { return data; }; return data; } function _execa() { const data = _interopRequireDefault(require("execa")); _execa = function () { return data; }; return data; } function _fsExtra() { const data = _interopRequireDefault(require("fs-extra")); _fsExtra = function () { return data; }; return data; } function _scope() { const data = _interopRequireDefault(require("../../scope/scope")); _scope = function () { return data; }; return data; } function _scopeNotFound() { const data = require("./exceptions/scope-not-found"); _scopeNotFound = function () { return data; }; return data; } function _generalError() { const data = _interopRequireDefault(require("../../error/general-error")); _generalError = function () { return data; }; return data; } function _environment() { const data = _interopRequireDefault(require("../../environment")); _environment = function () { return data; }; return data; } function _component() { const data = require("../component"); _component = function () { return data; }; return data; } // @ts-ignore (for some reason the tsc -w not found this) class Packer { constructor(isolator, scope) { this.isolator = isolator; this.scope = scope; (0, _defineProperty2().default)(this, "options", void 0); } packComponent(componentId, scopePath, options) { var _this = this; return (0, _bluebird().coroutine)(function* () { _this.options = options; // By default do not load scope from cache when packing const loadScopeFromCache = options && options.loadScopeFromCache !== undefined ? !!options.loadScopeFromCache : false; const scope = scopePath ? yield _scope().default.load(scopePath, loadScopeFromCache) : _this.scope; if (!scope) { throw new (_scopeNotFound().ScopeNotFound)(scopePath); } if (_this.options.useCapsule) { return _this.packUsingCapsule(componentId, scope); } return _this.packLegacy(componentId, scope); })(); } packLegacy(componentId, scope) { var _this2 = this; return (0, _bluebird().coroutine)(function* () { const isolatedEnvironment = new (_environment().default)(scope, undefined); yield isolatedEnvironment.create(); const isolatePath = isolatedEnvironment.path; const isolateOpts = { writeBitDependencies: true, createNpmLinkFiles: true, installPackages: false, noPackageJson: false, excludeRegistryPrefix: !_this2.options.prefix, saveDependenciesAsComponents: false }; yield isolatedEnvironment.isolateComponent(componentId, isolateOpts); const packResult = yield _this2.runNpmPack(isolatePath); if (!_this2.options.keep) { yield isolatedEnvironment.destroy(); } return packResult; })(); } packUsingCapsule(componentId, scope) { var _this3 = this; return (0, _bluebird().coroutine)(function* () { const bitId = yield scope.getParsedId(componentId); if (!bitId.hasScope()) { throw new (_generalError().default)(`unable to find "${componentId}" in the scope, make sure the component is tagged first`); } const network = yield _this3.isolator.createNetworkFromScope([componentId], scope); const capsule = network.capsules.getCapsuleIgnoreVersion(new (_component().ComponentID)(bitId)); if (!capsule) throw new Error(`capsule not found for ${componentId}`); return _this3.runNpmPack(capsule.wrkDir); })(); } runNpmPack(pathToPack) { var _this4 = this; return (0, _bluebird().coroutine)(function* () { const packDir = _this4.options.outDir || pathToPack; return npmPack(pathToPack, packDir, _this4.options.override); })(); } } exports.Packer = Packer; function readPackageJson(dir) { const pkgJson = _fsExtra().default.readJsonSync(_path().default.join(dir, 'package.json')); return pkgJson; } function npmPack(_x, _x2) { return _npmPack.apply(this, arguments); } function _npmPack() { _npmPack = (0, _bluebird().coroutine)(function* (cwd, outputPath, override = false) { const result = yield (0, _execa().default)('npm', ['pack'], { cwd }); const stdout = result.stdout; const tgzName = stdout.trim(); const tgzOriginPath = _path().default.join(cwd, tgzName); const pkgJson = readPackageJson(cwd); const tarPath = _path().default.join(outputPath, tgzName); const response = { pkgJson, tarPath }; if (tgzOriginPath !== tarPath && _fsExtra().default.pathExistsSync(tarPath)) { if (override) { _fsExtra().default.removeSync(tarPath); } else { throw new (_generalError().default)(`directory "${outputPath}" already exists, use --override flag to override`); } } if (tgzOriginPath !== tarPath) { yield _fsExtra().default.move(tgzOriginPath, tarPath); } return response; }); return _npmPack.apply(this, arguments); }