Browse Source

Merge branch 'pass-custom-options' of CoBox/cobox-config into development

kyphae 2 years ago
parent
commit
4fcc4598b0
2 changed files with 14 additions and 10 deletions
  1. 8
    10
      index.js
  2. 6
    0
      test/index.test.js

+ 8
- 10
index.js View File

@@ -15,12 +15,8 @@ module.exports.IDENTITY_SUBKEY_ID = IDENTITY_SUBKEY_ID
15 15
 const KeyHandler = require('./lib/key-handler')
16 16
 const MapHandler = require('./lib/map-handler')
17 17
 
18
-const defaultOptions = () => ({
19
-  mount: '/cobox'
20
-})
21
-
22
-const defaultConfig = () => ({
23
-  options: defaultOptions(),
18
+const defaultConfig = (options = {}) => ({
19
+  options,
24 20
   groups: { byKey: {}, byName: {} },
25 21
   replicators: { byKey: {}, byName: {} },
26 22
   identities: { byKey: {}, byName: {} }
@@ -33,13 +29,15 @@ class CoBoxConfig {
33 29
   constructor (storage, opts = {}) {
34 30
     this.root = storage || path.join(os.homedir(), '.cobox')
35 31
     this.storage = path.join(this.root, CONFIG_FILE)
32
+    opts.mount = opts.mount || '/cobox'
33
+    this._opts = opts
36 34
 
37 35
     mkdirp.sync(path.join(this.root, 'logs'))
38 36
 
39
-    var config = Object.assign(defaultConfig(), opts.seeds || {})
37
+    var config = Object.assign(defaultConfig(this._opts))
40 38
 
41 39
     if (!fs.existsSync(this.storage)) {
42
-      fs.writeFileSync(this.storage, yaml.safeDump(defaultConfig(), { sortKeys: true }))
40
+      fs.writeFileSync(this.storage, yaml.safeDump(defaultConfig(this._opts), { sortKeys: true }))
43 41
       this._groups = config.groups
44 42
       this._replicators = config.replicators
45 43
       this._identities = config.identities
@@ -67,7 +65,7 @@ class CoBoxConfig {
67 65
 
68 66
   save () {
69 67
     try {
70
-      var config = defaultConfig()
68
+      var config = defaultConfig(this._opts)
71 69
       config.groups = this._groups
72 70
       config.replicators = this._replicators
73 71
       config.identities = this._identities
@@ -87,7 +85,7 @@ class CoBoxConfig {
87 85
       this._groups = config.groups
88 86
       this._identities = config.identities
89 87
       this._replicators = config.replicators
90
-      this._options = config.options || defaultOptions()
88
+      this._options = config.options || this.defaultOptions
91 89
       return true
92 90
     } catch (err) {
93 91
       console.error(err)

+ 6
- 0
test/index.test.js View File

@@ -83,6 +83,12 @@ describe('options', (context) => {
83 83
     assert.same(config.options.get('mount'), './mnt', 'saved options')
84 84
     next()
85 85
   })
86
+
87
+  context('custom', (assert, next) => {
88
+    var config = Config(storage, { mount: '/tmp' })
89
+    assert.same(config.options.get('mount'), '/tmp', 'mount defaults to /tmp')
90
+    next()
91
+  })
86 92
 })
87 93
 
88 94
 describe('set', (context) => {