This repository has been archived by the owner on Dec 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (70 loc) · 2.24 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var yaml = require("js-yaml"),
fs = require("fs"),
path = require("path");
module.exports = function (configFilePath) {
var api = Object.create(null),
currentConfigFilePath = configFilePath,
defaultConfigFilePath = path.join(
process.cwd(),
"config",
(process.NODE_ENV || "development") + ".yml");
;
function contains (featureUsers, user) {
var userName = (user.name || user);
if (userName === undefined) {
return false;
} else {
switch (typeof featureUsers) {
case "string":
return featureUsers === userName;
case "object":
return featureUsers.indexOf(userName) > -1;
default:
return false;
}
}
}
api.reload = function (newConfigPath, callback) {
var doc, configPath;
switch (typeof newConfigPath) {
case "string":
configPath = newConfigPath;
break;
case "function":
callback = newConfigPath;
case "undefined":
default:
configPath = currentConfigFilePath || defaultConfigFilePath;
}
doc = yaml.safeLoad(fs.readFileSync(configPath, "utf8"));
currentConfigFilePath = configPath;
features = doc["features"];
if (callback) { callback(); }
};
api.isEnabled = function (feature) {
var status = features[feature],
enabled = false;
if (!status) {
return false;
}
if (status.users) {
enabled = contains(status.users, this.user);
} else if (status.groups) {
enabled = contains(status.groups, this.group);
} else {
enabled = (status !== undefined) && (status !== "off");
}
return enabled;
};
api.variant = function (feature) {
var data = features[feature];
if (data instanceof Array) {
var randomIndex = Math.floor(Math.random() * data.length);
return data[randomIndex];
} else {
return undefined;
}
};
api.reload(configFilePath);
return api;
}