UNPKG

1.98 kBJavaScriptView Raw
1'use strict';
2
3const SilentError = require('silent-error');
4
5const FEATURES = require('./features');
6const getConfigPath = require('./utils').getConfigPath;
7
8module.exports = {
9 name: '@ember/optional-features',
10
11 includedCommands() {
12 return require('./commands');
13 },
14
15 init() {
16 this._super && this._super.init.apply(this, arguments);
17 this._features = this._validateFeatures(this._loadFeatures());
18 },
19
20 _loadFeatures() {
21 let features = {};
22
23 let configPath = getConfigPath(this.project);
24
25 try {
26 Object.assign(features, this.project.require(configPath));
27 } catch(err) {
28 if (err.code !== 'MODULE_NOT_FOUND') {
29 throw err;
30 }
31 }
32
33 if (process.env.EMBER_OPTIONAL_FEATURES) {
34 Object.assign(features, JSON.parse(process.env.EMBER_OPTIONAL_FEATURES));
35 }
36
37 return features;
38 },
39
40 _validateFeatures(features) {
41 let validated = {};
42 let keys = Object.keys(features);
43 keys.forEach(key => {
44 if (FEATURES[key] === undefined) {
45 throw new SilentError(`Unknown feature "${key}" found in config/optional-features.json`);
46 } else if (features[key] !== null && typeof features[key] !== 'boolean') {
47 throw new SilentError(`Unsupported value "${String(features[key])}" for "${key}" found in config/optional-features.json`);
48 }
49 });
50
51 Object.keys(FEATURES).forEach(key => {
52 if (typeof features[key] === 'boolean') {
53 validated[key] = features[key];
54 }
55 });
56
57 return validated;
58 },
59
60 isFeatureEnabled(name) {
61 let value = this._features[name];
62 return value !== undefined ? value : FEATURES[name].default;
63 },
64
65 config() {
66 let EmberENV = {};
67 let features = this._features;
68
69 Object.keys(FEATURES).forEach(key => {
70 let value = features[key];
71
72 if (value !== undefined) {
73 let KEY = `_${key.toUpperCase().replace(/-/g, '_')}`;
74 EmberENV[KEY] = value;
75 }
76
77 });
78
79 return { EmberENV };
80 }
81};