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

178 lines (132 loc) 4.57 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = 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 _fsExtra() { const data = _interopRequireDefault(require("fs-extra")); _fsExtra = function () { return data; }; return data; } function path() { const data = _interopRequireWildcard(require("path")); path = function () { return data; }; return data; } function _sources() { const data = require("../consumer/component/sources"); _sources = function () { return data; }; return data; } function _constants() { const data = require("../constants"); _constants = function () { return data; }; return data; } function _logger() { const data = _interopRequireDefault(require("../logger/logger")); _logger = function () { return data; }; return data; } function _validationError() { const data = _interopRequireDefault(require("../error/validation-error")); _validationError = function () { return data; }; return data; } class LinkFile extends _sources().AbstractVinyl { constructor(...args) { super(...args); (0, _defineProperty2().default)(this, "override", false); (0, _defineProperty2().default)(this, "ignorePreviousSymlink", false); (0, _defineProperty2().default)(this, "writeAutoGeneratedMessage", true); (0, _defineProperty2().default)(this, "srcPath", void 0); (0, _defineProperty2().default)(this, "componentId", void 0); } // needed for logging purposes write() { var _this = this; return (0, _bluebird().coroutine)(function* () { const stat = yield _this._getStatIfFileExists(); if (stat) { if (!_this.ignorePreviousSymlink && stat.isSymbolicLink()) { // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! throw new (_validationError().default)(`fatal: trying to write a link file into a symlink file at "${_this.path}"`); } if (!_this.override) { // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! const fileContent = _fsExtra().default.readFileSync(_this.path).toString(); // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! if (!fileContent.includes(_constants().AUTO_GENERATED_STAMP)) return _this.path; } } // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! const data = _this.writeAutoGeneratedMessage ? _constants().AUTO_GENERATED_MSG + _this.contents : _this.contents; // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! _logger().default.debug(`link-file.write, path ${_this.path}`); try { // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! yield _fsExtra().default.outputFile(_this.path, data); } catch (err) { if (err.code === 'EISDIR') { // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! _logger().default.debug(`deleting a directory ${_this.path} in order to write a link file with the same name`); // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! yield _fsExtra().default.remove(_this.path); // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! yield _fsExtra().default.outputFile(_this.path, data); } else { throw err; } } // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! return _this.path; })(); } static load({ filePath, base, content, override = false, ignorePreviousSymlink = false, writeAutoGeneratedMessage = true, srcPath, componentId }) { // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! const linkFile = new LinkFile({ base: base || path().dirname(filePath), path: filePath, contents: Buffer.from(content) }); linkFile.override = override; linkFile.writeAutoGeneratedMessage = writeAutoGeneratedMessage; linkFile.srcPath = srcPath; linkFile.componentId = componentId; linkFile.ignorePreviousSymlink = ignorePreviousSymlink; return linkFile; } } exports.default = LinkFile;