-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
90 lines (71 loc) · 2.08 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
85
86
87
88
89
90
'use strict';
const SilentError = require('silent-error');
const FEATURES = require('./features');
const getConfigPath = require('./utils').getConfigPath;
module.exports = {
name: '@ember/optional-features',
includedCommands() {
return require('./commands');
},
init() {
this._super && this._super.init.apply(this, arguments);
this._features = this._validateFeatures(this._loadFeatures());
},
_loadFeatures() {
let features = {};
let configPath = getConfigPath(this.project);
try {
Object.assign(features, this.project.require(configPath));
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
throw err;
}
}
if (process.env.EMBER_OPTIONAL_FEATURES) {
Object.assign(features, JSON.parse(process.env.EMBER_OPTIONAL_FEATURES));
}
return features;
},
_validateFeatures(features) {
let validated = {};
let keys = Object.keys(features);
keys.forEach((key) => {
if (FEATURES[key] === undefined) {
throw new SilentError(
`Unknown feature "${key}" found in config/optional-features.json`
);
} else if (features[key] !== null && typeof features[key] !== 'boolean') {
throw new SilentError(
`Unsupported value "${String(
features[key]
)}" for "${key}" found in config/optional-features.json`
);
}
});
Object.keys(FEATURES).forEach((key) => {
if (typeof features[key] === 'boolean') {
validated[key] = features[key];
}
});
return validated;
},
isFeatureEnabled(name) {
let value = this._features[name];
return value !== undefined ? value : FEATURES[name].default;
},
isFeatureExplicitlySet(name) {
return this._features[name] !== undefined;
},
config() {
let EmberENV = {};
let features = this._features;
Object.keys(FEATURES).forEach((key) => {
let value = features[key];
if (value !== undefined) {
let KEY = `_${key.toUpperCase().replace(/-/g, '_')}`;
EmberENV[KEY] = value;
}
});
return { EmberENV };
},
};