|
1 | 1 | 'use strict'
|
2 | 2 |
|
| 3 | +const promisify = require('promisify-es6') |
| 4 | +const _get = require('lodash.get') |
| 5 | +const _has = require('lodash.has') |
| 6 | +const _set = require('lodash.set') |
| 7 | + |
3 | 8 | module.exports = function config (self) {
|
4 | 9 | return {
|
5 |
| - // cli only feature built with show and replace |
6 |
| - // edit: (callback) => {}, |
7 |
| - replace: (config, callback) => { |
| 10 | + get: promisify((key, callback) => { |
| 11 | + if (typeof key === 'function') { |
| 12 | + callback = key |
| 13 | + key = undefined |
| 14 | + } |
| 15 | + |
| 16 | + if (!key) { |
| 17 | + return self._repo.config.get(callback) |
| 18 | + } |
| 19 | + |
| 20 | + if (typeof key !== 'string') { |
| 21 | + return callback(new Error('Invalid key type')) |
| 22 | + } |
| 23 | + |
| 24 | + self._repo.config.get((err, config) => { |
| 25 | + if (err) { |
| 26 | + return callback(err) |
| 27 | + } |
| 28 | + if (_has(config, key)) { |
| 29 | + const value = _get(config, key, undefined) |
| 30 | + callback(null, value) |
| 31 | + } else { |
| 32 | + callback(new Error('Key does not exist in config')) |
| 33 | + } |
| 34 | + }) |
| 35 | + }), |
| 36 | + set: promisify((key, value, callback) => { |
| 37 | + if (!key || typeof key !== 'string') { |
| 38 | + return callback(new Error('Invalid key type')) |
| 39 | + } |
| 40 | + |
| 41 | + if (value === undefined || Buffer.isBuffer(value)) { |
| 42 | + return callback(new Error('Invalid value type')) |
| 43 | + } |
| 44 | + |
| 45 | + self._repo.config.get((err, config) => { |
| 46 | + if (err) { |
| 47 | + return callback(err) |
| 48 | + } |
| 49 | + _set(config, key, value) |
| 50 | + self.config.replace(config, callback) |
| 51 | + }) |
| 52 | + }), |
| 53 | + replace: promisify((config, callback) => { |
8 | 54 | self._repo.config.set(config, callback)
|
9 |
| - }, |
10 |
| - show: (callback) => { |
11 |
| - self._repo.config.get(callback) |
12 |
| - } |
| 55 | + }) |
13 | 56 | }
|
14 | 57 | }
|
0 commit comments