UNPKG

user-conf

Version:

Manage configs in the user home dir

233 lines (195 loc) 7.03 kB
'use strict'; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); // to be promisified var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _pify = require('pify'); var _pify2 = _interopRequireDefault(_pify); var _lodash = require('lodash.merge'); var _lodash2 = _interopRequireDefault(_lodash); var _lodash3 = require('lodash.get'); var _lodash4 = _interopRequireDefault(_lodash3); var _lodash5 = require('lodash.set'); var _lodash6 = _interopRequireDefault(_lodash5); var _lodash7 = require('lodash.isplainobject'); var _lodash8 = _interopRequireDefault(_lodash7); var _fs2 = require('fs'); var _fs3 = _interopRequireDefault(_fs2); var _rimraf2 = require('rimraf'); var _rimraf3 = _interopRequireDefault(_rimraf2); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var USER_CONF = '__user-conf__'; var fs = (0, _pify2.default)(_fs3.default); var rimraf = (0, _pify2.default)(_rimraf3.default); var UserConfig = function () { function UserConfig(name) { var base = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; _classCallCheck(this, UserConfig); if (!name || typeof name !== 'string') { throw new Error('Expecting name to be non-empty string'); } else if (!(0, _lodash8.default)(base)) { throw new Error('Expecting base to be plain object'); } this._confFile = _path2.default.resolve(require('os').homedir(), name + '.json'); if (fs.existsSync(this._confFile)) { var conf = void 0; try { conf = require(this._confFile); } catch (err) { throw new Error('Could not load existing config at ' + this._confFile); } if (!conf[USER_CONF]) { throw new Error('File at ' + this._confFile + ' is not user-conf file'); } } else { base[USER_CONF] = true; fs.closeSync(fs.openSync(this._confFile, 'w')); var stringy = JSON.stringify(base, null, '\t'); fs.writeFileSync(this._confFile, stringy); } } /// Private _createClass(UserConfig, [{ key: '_writeJSON', value: function _writeJSON(obj) { if (!(0, _lodash8.default)(obj)) { throw new Error('Expecting obj to be object'); } obj[USER_CONF] = true; var stringy = JSON.stringify(obj, null, '\t'); return fs.writeFile(this._confFile, stringy); } }, { key: '_writeJSONSync', value: function _writeJSONSync(obj) { if (!(0, _lodash8.default)(obj)) { throw new Error('Expecting obj to be object'); } obj[USER_CONF] = true; var stringy = JSON.stringify(obj, null, '\t'); fs.writeFileSync(this._confFile, stringy); } }, { key: '_getJSON', value: function _getJSON() { return fs.readFile(this._confFile).then(function (result) { var json = JSON.parse(String(result)); delete json[USER_CONF]; return json; }); } }, { key: '_getJSONSync', value: function _getJSONSync() { var stringy = String(fs.readFileSync(this._confFile)); var json = JSON.parse(stringy); delete json[USER_CONF]; return json; } /// Public }, { key: 'clear', value: function clear() { return this._writeJSON({}); } }, { key: 'clearSync', value: function clearSync() { this._writeJSONSync({}); } }, { key: 'get', value: function get(key) { if (key && typeof key !== 'string') { throw new Error('Expecting key to be string'); } return this._getJSON().then(function (json) { return key ? (0, _lodash4.default)(json, key) : json; }); } }, { key: 'getSync', value: function getSync(key) { if (key && typeof key !== 'string') { throw new Error('Expecting key to be string'); } var json = this._getJSONSync(); return key ? (0, _lodash4.default)(json, key) : json; } }, { key: 'set', value: function set(key, val) { var json = void 0; if (typeof val === 'undefined') { if (!(0, _lodash8.default)(key)) { throw new Error('set expects object if no key given'); } json = key; } else if (typeof key !== 'string') { throw new Error('Expecting key to be string'); } else { json = this._getJSONSync(); (0, _lodash6.default)(json, key, val); } return this._writeJSON(json); } }, { key: 'setSync', value: function setSync(key, val) { var json = void 0; if (typeof val === 'undefined') { if (!(0, _lodash8.default)(key)) { throw new Error('setSync expects object if no key given'); } json = key; } else if (typeof key !== 'string') { throw new Error('Expecting key to be string'); } else { json = this._getJSONSync(); (0, _lodash6.default)(json, key, val); } this._writeJSONSync(json); } }, { key: 'update', value: function update(diff) { var _this = this; if (!(0, _lodash8.default)(diff)) { throw new Error('Expecting diff to be object'); } return this.get().then(function (options) { return _this.set((0, _lodash2.default)({}, options, diff)); }).then(function () { return _this.get(); }); } }, { key: 'updateSync', value: function updateSync(diff) { if (!(0, _lodash8.default)(diff)) { throw new Error('Expecting diff to be object'); } var options = this.getSync(); var updatedOptions = (0, _lodash2.default)({}, options, diff); this.setSync(updatedOptions); return this.getSync(); } }, { key: 'destroy', value: function destroy() { return rimraf(this._confFile); } }]); return UserConfig; }(); module.exports = { UserConfig: UserConfig, init: function init() { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return new (Function.prototype.bind.apply(UserConfig, [null].concat(_toConsumableArray(args))))(); } };