UNPKG

2.13 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(
46 `Unknown feature "${key}" found in config/optional-features.json`
47 );
48 } else if (features[key] !== null && typeof features[key] !== 'boolean') {
49 throw new SilentError(
50 `Unsupported value "${String(
51 features[key]
52 )}" for "${key}" found in config/optional-features.json`
53 );
54 }
55 });
56
57 Object.keys(FEATURES).forEach((key) => {
58 if (typeof features[key] === 'boolean') {
59 validated[key] = features[key];
60 }
61 });
62
63 return validated;
64 },
65
66 isFeatureEnabled(name) {
67 let value = this._features[name];
68 return value !== undefined ? value : FEATURES[name].default;
69 },
70
71 isFeatureExplicitlySet(name) {
72 return this._features[name] !== undefined;
73 },
74
75 config() {
76 let EmberENV = {};
77 let features = this._features;
78
79 Object.keys(FEATURES).forEach((key) => {
80 let value = features[key];
81
82 if (value !== undefined) {
83 let KEY = `_${key.toUpperCase().replace(/-/g, '_')}`;
84 EmberENV[KEY] = value;
85 }
86 });
87
88 return { EmberENV };
89 },
90};