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

156 lines (116 loc) 3.73 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 _defineProperty2() { const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); _defineProperty2 = function () { return data; }; return data; } function _ramda() { const data = _interopRequireDefault(require("ramda")); _ramda = function () { return data; }; return data; } function path() { const data = _interopRequireWildcard(require("path")); path = function () { return data; }; return data; } function _fsExtra() { const data = _interopRequireDefault(require("fs-extra")); _fsExtra = function () { return data; }; return data; } function _lodash() { const data = _interopRequireDefault(require("lodash.set")); _lodash = function () { return data; }; return data; } class BitJsonHelper { constructor(scopes) { (0, _defineProperty2().default)(this, "scopes", void 0); this.scopes = scopes; } read(bitJsonDir = this.scopes.localPath) { const bitJsonPath = path().join(bitJsonDir, 'bit.json'); return _fsExtra().default.existsSync(bitJsonPath) ? _fsExtra().default.readJSONSync(bitJsonPath) : {}; } write(bitJson, bitJsonDir = this.scopes.localPath) { const bitJsonPath = path().join(bitJsonDir, 'bit.json'); return _fsExtra().default.writeJSONSync(bitJsonPath, bitJson, { spaces: 2 }); } addKeyVal(key, val, bitJsonDir = this.scopes.localPath) { const bitJson = this.read(bitJsonDir); bitJson[key] = val; this.write(bitJson, bitJsonDir); } addOverrides(overrides) { const bitJson = this.read(); bitJson.overrides = overrides; this.write(bitJson); } addDefaultScope(scope = this.scopes.remote) { this.addKeyVal('defaultScope', scope); } getEnvByType(bitJson, envType) { const basePath = ['env', envType]; const env = _ramda().default.path(basePath, bitJson); const envName = Object.keys(env)[0]; return env[envName]; } addToRawConfigOfEnv(bitJsonPath = this.scopes.localPath, key, val, envType) { this._addKeyValToEnvProp(bitJsonPath, 'rawConfig', key, val, envType); } manageWorkspaces(withWorkspaces = true) { const bitJson = this.read(); bitJson.packageManager = 'yarn'; bitJson.manageWorkspaces = withWorkspaces; bitJson.useWorkspaces = withWorkspaces; this.write(bitJson); } setComponentsDir(content) { const bitJson = this.read(); bitJson.componentsDefaultDirectory = content; this.write(bitJson); } corrupt(bitJsonPath = path().join(this.scopes.localPath, 'bit.json')) { _fsExtra().default.writeFileSync(bitJsonPath, '"corrupted'); } modifyField(key, value) { const bitJson = this.read(); bitJson[key] = value; this.write(bitJson); } _getEnvNameByType(bitJson, envType) { // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! const env = bitJson.env[envType]; const envName = typeof env === 'string' ? env : Object.keys(env)[0]; return envName; } _addKeyValToEnvProp(bitJsonDir = this.scopes.localPath, propName, key, val, envType) { const bitJson = this.read(bitJsonDir); const envName = this._getEnvNameByType(bitJson, envType); const propPath = ['env', envType, envName, propName]; const prop = _ramda().default.pathOr({}, propPath, bitJson); prop[key] = val; (0, _lodash().default)(bitJson, propPath, prop); this.write(bitJson, bitJsonDir); } } exports.default = BitJsonHelper;