UNPKG

1.81 kBJavaScriptView Raw
1'use strict'
2
3var CONFIG_FILE = '.remix.config'
4const EventEmitter = require('events')
5
6function Config (storage) {
7 this.items = {}
8 this.unpersistedItems = {}
9 this.events = new EventEmitter()
10
11 // load on instantiation
12 try {
13 var config = storage.get(CONFIG_FILE)
14 if (config) {
15 this.items = JSON.parse(config)
16 }
17 } catch (exception) {
18 }
19
20 this.exists = function (key) {
21 return this.items[key] !== undefined
22 }
23
24 this.get = function (key) {
25 this.ensureStorageUpdated(key)
26 return this.items[key]
27 }
28
29 this.set = function (key, content) {
30 this.items[key] = content
31 try {
32 storage.set(CONFIG_FILE, JSON.stringify(this.items))
33 this.events.emit(key + '_changed', content)
34 } catch (exception) {
35 }
36 }
37
38 this.ensureStorageUpdated = function (key) {
39 if (key === 'currentFile') {
40 if (this.items[key] && this.items[key] !== '' &&
41 this.items[key].indexOf('config/') !== 0 &&
42 this.items[key].indexOf('browser/') !== 0 &&
43 this.items[key].indexOf('localhost/') !== 0 &&
44 this.items[key].indexOf('swarm/') !== 0 &&
45 this.items[key].indexOf('gist/') !== 0 &&
46 this.items[key].indexOf('github/') !== 0 &&
47 this.items[key].indexOf('ipfs/') !== 0 &&
48 this.items[key].indexOf('http/') !== 0 &&
49 this.items[key].indexOf('https/') !== 0) {
50 this.items[key] = 'browser/' + this.items[key]
51 }
52 }
53 }
54
55 this.getUnpersistedProperty = function (key) {
56 return this.unpersistedItems[key]
57 }
58
59 // TODO: this only used for *one* property "doNotShowTransactionConfirmationAgain"
60 // and can be removed once it's refactored away in txRunner
61 this.setUnpersistedProperty = function (key, value) {
62 this.unpersistedItems[key] = value
63 }
64}
65
66module.exports = Config