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

226 lines (174 loc) 5.14 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.getPath = getPath; exports.ScopeJson = 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 = _interopRequireDefault(require("path")); _path = function () { return data; }; return data; } function _utils() { const data = require("../utils"); _utils = function () { return data; }; return data; } function _constants() { const data = require("../constants"); _constants = function () { return data; }; return data; } function _bitId() { const data = _interopRequireDefault(require("../bit-id/bit-id")); _bitId = function () { return data; }; return data; } function _generalError() { const data = _interopRequireDefault(require("../error/general-error")); _generalError = function () { return data; }; return data; } function _exceptions() { const data = require("./exceptions"); _exceptions = function () { return data; }; return data; } // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! function getPath(scopePath) { return _path().default.join(scopePath, _constants().SCOPE_JSON); } class ScopeJson { // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! constructor({ name, remotes, resolverPath, hooksPath, license, groupName, version }) { (0, _defineProperty2().default)(this, "_name", void 0); (0, _defineProperty2().default)(this, "version", void 0); (0, _defineProperty2().default)(this, "resolverPath", void 0); (0, _defineProperty2().default)(this, "hooksPath", void 0); (0, _defineProperty2().default)(this, "license", void 0); (0, _defineProperty2().default)(this, "remotes", void 0); (0, _defineProperty2().default)(this, "groupName", void 0); this.name = name; this.version = version; this.resolverPath = resolverPath; this.hooksPath = hooksPath; this.license = license; this.remotes = remotes || {}; this.groupName = groupName || ''; } // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! set name(suggestedName) { this._name = _bitId().default.getValidScopeName(suggestedName); // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! return this; } // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! get name() { return this._name; } toPlainObject() { return (0, _utils().cleanObject)({ name: this.name, remotes: this.remotes, resolverPath: this.resolverPath, license: this.license, groupName: this.groupName, version: this.version }); } toJson(readable = true) { if (!readable) return JSON.stringify(this.toPlainObject()); return JSON.stringify(this.toPlainObject(), null, 4); } set(key, val) { // eslint-disable-next-line no-prototype-builtins if (!this.hasOwnProperty(key)) throw new (_generalError().default)(`unknown key ${key}`); this[key] = val; return this; } get(key) { // eslint-disable-next-line no-prototype-builtins if (!this.hasOwnProperty(key)) throw new (_generalError().default)(`unknown key ${key}`); return this[key]; } del(key) { // eslint-disable-next-line no-prototype-builtins if (!this.hasOwnProperty(key)) throw new (_generalError().default)(`unknown key ${key}`); return this[key]; } addRemote(remote) { this.remotes[remote.name] = remote.host; return this; } rmRemote(name) { if (!this.remotes[name]) return false; delete this.remotes[name]; return true; } write(path) { var _this = this; return (0, _bluebird().coroutine)(function* () { return (0, _utils().writeFile)(_path().default.join(path, _constants().SCOPE_JSON), _this.toJson()); })(); } static loadFromJson(json) { return new ScopeJson(JSON.parse(json)); } static loadFromFile(scopeJsonPath) { return (0, _bluebird().coroutine)(function* () { let rawScopeJson; try { rawScopeJson = yield _fsExtra().default.readFile(scopeJsonPath); } catch (err) { if (err.code === 'ENOENT') throw new (_exceptions().ScopeJsonNotFound)(scopeJsonPath); throw err; } return ScopeJson.loadFromJson(rawScopeJson.toString()); })(); } getPopulatedLicense() { // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! if (!this.get('license') || !_fsExtra().default.existsSync(this.get('license'))) return Promise.resolve(); return _fsExtra().default.readFile(this.get('license')).then(license => license.toString()); } } exports.ScopeJson = ScopeJson;