Skip to content

Commit

Permalink
Refactor to put each feature in its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
chancancode committed Jan 24, 2018
1 parent 41707e6 commit 2851e78
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 25 deletions.
2 changes: 1 addition & 1 deletion commands/index.js
Expand Up @@ -32,7 +32,7 @@ const SHARED = {
let configJSON = JSON.parse(fs.readFileSync(configPath, { encoding: 'UTF-8' }));
let config = {};

FEATURES.NAMES.forEach(feature => {
Object.keys(FEATURES).forEach(feature => {
if (feature === name) {
config[feature] = value;
} else if(configJSON[feature] !== undefined) {
Expand Down
21 changes: 12 additions & 9 deletions features.js
@@ -1,13 +1,16 @@
'use strict';

const DEFAULTS = {
'application-template-wrapper': true,
'template-only-component-wrapper': true
};
const glob = require('glob');
const path = require('path');

const NAMES = Object.keys(DEFAULTS).sort();
const FEATURES_PATH = path.resolve(__dirname, './features');
const FEATURES = {};

module.exports = {
DEFAULTS,
NAMES
};
glob.sync('*.js', { cwd: FEATURES_PATH }).sort().forEach(filename => {
let key = filename.slice(0, -3);
let value = Object.assign({}, require(`./features/${key}`));

FEATURES[key] = Object.freeze(value);
});

module.exports = Object.freeze(FEATURES);
5 changes: 5 additions & 0 deletions features/application-template-wrapper.js
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
default: true
};
5 changes: 5 additions & 0 deletions features/template-only-glimmer-components.js
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
default: false
};
11 changes: 6 additions & 5 deletions index.js
Expand Up @@ -39,18 +39,18 @@ module.exports = {
let keys = Object.keys(features);

keys.forEach(key => {
if (FEATURES.NAMES.indexOf(key) === -1) {
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`);
}
});

FEATURES.NAMES.forEach(key => {
Object.keys(FEATURES).forEach(key => {
if (typeof features[key] === 'boolean') {
validated[key] = features[key];
} else {
validated[key] = FEATURES.DEFAULTS[key];
validated[key] = FEATURES[key].default;
}
});

Expand All @@ -65,10 +65,11 @@ module.exports = {
let EmberENV = {};
let features = this._features;

FEATURES.NAMES.forEach(key => {
Object.keys(FEATURES).forEach(key => {
let defaultValue = FEATURES[key].default
let value = features[key];

if (value !== FEATURES.DEFAULTS[key]) {
if (value !== defaultValue) {
let KEY = `_${key.toUpperCase().replace(/-/g, '_')}`;
EmberENV[KEY] = value;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -22,6 +22,7 @@
"node": "^4.5 || 6.* || >= 7.*"
},
"dependencies": {
"glob": "^7.1.2",
"mkdirp": "^0.5.1",
"silent-error": "^1.1.0"
},
Expand Down
4 changes: 2 additions & 2 deletions tests/commands-test.js
Expand Up @@ -89,7 +89,7 @@ QUnit.module('commands', hooks => {
config: {
'optional-features.json': strip(`
{
"template-only-component-wrapper": false
"template-only-glimmer-components": true
}
`)
}
Expand All @@ -101,7 +101,7 @@ QUnit.module('commands', hooks => {
'optional-features.json': strip(`
{
"application-template-wrapper": ${testCase.expected},
"template-only-component-wrapper": false
"template-only-glimmer-components": true
}
`)
}, 'it should have rewritten the config file with the appropiate flags');
Expand Down
16 changes: 8 additions & 8 deletions tests/optional-features-test.js
Expand Up @@ -61,13 +61,13 @@ QUnit.module('@ember/optional-features', hooks => {
QUnit.test('it generates the correct config when features are passed', assert => {
let addon = buildAddon({
'application-template-wrapper': false,
'template-only-component-wrapper': false
'template-only-glimmer-components': true
});

let expected = {
EmberENV: {
"_APPLICATION_TEMPLATE_WRAPPER": false,
"_TEMPLATE_ONLY_COMPONENT_WRAPPER": false
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": true
}
};

Expand All @@ -77,12 +77,12 @@ QUnit.module('@ember/optional-features', hooks => {
QUnit.test('it removes features that matches their default value', assert => {
let addon = buildAddon({
'application-template-wrapper': true,
'template-only-component-wrapper': false
'template-only-glimmer-components': true
});

let expected = {
EmberENV: {
"_TEMPLATE_ONLY_COMPONENT_WRAPPER": false
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": true
}
};

Expand All @@ -92,7 +92,7 @@ QUnit.module('@ember/optional-features', hooks => {
QUnit.test('it allows `null` to mean using the default value', assert => {
let addon = buildAddon({
'application-template-wrapper': null,
'template-only-component-wrapper': null
'template-only-glimmer-components': null
});

let expected = {
Expand All @@ -108,18 +108,18 @@ QUnit.module('@ember/optional-features', hooks => {
});

assert.strictEqual(addon.isEnabled('application-template-wrapper'), false, 'Expecting suppied value');
assert.strictEqual(addon.isEnabled('template-only-component-wrapper'), true, 'Expecting default value');
assert.strictEqual(addon.isEnabled('template-only-glimmer-components'), false, 'Expecting default value');
});

QUnit.test('it allows the config to be a overridden with an ENV variable', assert => {
process.env.EMBER_OPTIONAL_FEATURES = `{ "application-template-wrapper": false }`;

let addon = buildAddon({
'application-template-wrapper': true,
'template-only-component-wrapper': false
'template-only-glimmer-components': true
});

assert.strictEqual(addon.isEnabled('application-template-wrapper'), false, 'Expecting value from ENV var');
assert.strictEqual(addon.isEnabled('template-only-component-wrapper'), false, 'Expecting value from JSON');
assert.strictEqual(addon.isEnabled('template-only-glimmer-components'), true, 'Expecting value from JSON');
});
});

0 comments on commit 2851e78

Please sign in to comment.