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
115 lines (83 loc) • 2.24 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function _bluebird() {
const data = require("bluebird");
_bluebird = 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 _constants() {
const data = require("../constants");
_constants = function () {
return data;
};
return data;
}
function _utils() {
const data = require("../utils");
_utils = function () {
return data;
};
return data;
}
function getPath() {
return path().join(_constants().GLOBAL_CONFIG, _constants().GLOBAL_CONFIG_FILE);
}
class Config extends Map {
toPlainObject() {
return (0, _utils().mapToObject)(this);
}
toJson() {
return JSON.stringify(this.toPlainObject());
}
write() {
return _fsExtra().default.outputFile(getPath(), this.toJson());
}
writeSync() {
return _fsExtra().default.outputFileSync(getPath(), this.toJson());
}
static loadSync() {
const configPath = getPath();
if (!_fsExtra().default.existsSync(configPath)) {
const config = new Config([]);
config.writeSync();
return config;
}
const contents = _fsExtra().default.readFileSync(configPath);
return new Config(Object.entries(JSON.parse(contents.toString())));
}
static load() {
return (0, _bluebird().coroutine)(function* () {
const configPath = getPath();
const exists = yield _fsExtra().default.pathExists(configPath);
if (!exists) {
const config = new Config([]);
yield config.write();
return config;
}
const contents = yield _fsExtra().default.readFile(configPath);
return new Config(Object.entries(JSON.parse(contents.toString())));
})();
}
}
exports.default = Config;