-
Notifications
You must be signed in to change notification settings - Fork 1
/
secret.js
53 lines (44 loc) · 1.32 KB
/
secret.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
// unexplained hogehoge :)
var fs = require('fs');
var secret = JSON.parse(fs.readFileSync('secret.json'));
var Reflect = require('harmony-reflect');
var makeProxy = function (object, parent, proxy, key) {
object.__parent__ = parent;
object.__proxy__ = proxy;
object.__key__ = key;
return new Proxy(object, {
get: function (target, name) {
if (typeof target[name] === 'undefined') {
return undefined;
} else if (typeof target[name] === 'object') {
return makeProxy(target[name], target, this, name);
} else {
return target[name];
}
},
set: function (target, name, value) {
target[name] = value;
if (target.__parent__ === null) {
var sanitize = function (object) {
var newObject = {};
for (var key in object) if (object.hasOwnProperty(key)) {
if (key === '__parent__' || key === '__proxy__' || key === '__key__') {
// skip
} else if (typeof object[key] === 'object') {
newObject[key] = sanitize(object[key]);
} else {
newObject[key] = object[key];
}
}
return newObject;
}
var data = sanitize(target);
fs.writeFile('secret.json', JSON.stringify(data, null, 2));
} else {
target.__proxy__.set(target.__parent__, target.__key__, target);
}
}
});
};
module.exports = makeProxy(secret, null, null, null);