Skip to content

Commit

Permalink
Merge pull request #585 from ember-cli/use-custom-config-path-if-defined
Browse files Browse the repository at this point in the history
Use `ember-addon.configPath` from `package.json` if defined.
  • Loading branch information
rwjblue committed Oct 29, 2020
2 parents 37f062e + f48c6ad commit cd0a440
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 16 deletions.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -3,7 +3,7 @@
module.exports = {
name: 'ember-try',

includedCommands: function() {
includedCommands() {
return require('./lib/commands');
}
};
2 changes: 1 addition & 1 deletion lib/commands/config.js
Expand Up @@ -7,7 +7,7 @@ module.exports = {
description: 'Displays the config that will be used',
works: 'insideProject',
availableOptions: [
{ name: 'config-path', type: String, default: 'config/ember-try.js' },
{ name: 'config-path', type: String },
],

async run(commandOptions) {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/try-each.js
Expand Up @@ -8,7 +8,7 @@ module.exports = {

availableOptions: [
{ name: 'skip-cleanup', type: Boolean, default: false },
{ name: 'config-path', type: String, default: 'config/ember-try.js' },
{ name: 'config-path', type: String },
],

_getConfig: require('../utils/config'),
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/try-ember.js
Expand Up @@ -13,7 +13,7 @@ module.exports = {

availableOptions: [
{ name: 'skip-cleanup', type: Boolean, default: false },
{ name: 'config-path', type: String, default: 'config/ember-try.js' },
{ name: 'config-path', type: String },
],

_getConfig: require('../utils/config'),
Expand Down
29 changes: 24 additions & 5 deletions lib/utils/config.js
Expand Up @@ -7,17 +7,36 @@ const debug = require('debug')('ember-try:utils:config');

const IMPLICIT_BOWER_VERSION = '^1.8.2';

function getConfigPath(project) {
let possibleConfigPath;
if (project.pkg && project.pkg['ember-addon'] && project.pkg['ember-addon']['configPath']) {
let configDir = project.pkg['ember-addon']['configPath'];

possibleConfigPath = path.join(configDir, 'ember-try.js');
}

if (fs.existsSync(possibleConfigPath)) {
debug(`using config from package.json ember-addon.configPath: ${possibleConfigPath}`);

return possibleConfigPath;
}

debug('using config from config/ember-try.js');

return path.join('config', 'ember-try.js');
}

async function getBaseConfig(options) {
let relativePath = options.configPath || path.join('config', 'ember-try.js');
let configFile = path.join(options.project.root, relativePath);
let relativeConfigPath = options.configPath || getConfigPath(options.project);
let configPath = path.join(options.project.root, relativeConfigPath);
let data;

if (fs.existsSync(configFile)) {
let configData = await require(configFile);
if (fs.existsSync(configPath)) {
let configData = await require(configPath);

data = typeof configData === 'function' ? await configData(options.project) : configData;
} else {
debug('Config file does not exist %s', configFile);
debug('Config file does not exist %s', configPath);
}

if (data && data.scenarios && !data.useVersionCompatibility && !options.versionCompatibility) {
Expand Down
40 changes: 33 additions & 7 deletions test/utils/config-test.js
Expand Up @@ -21,19 +21,19 @@ describe('utils/config', () => {
beforeEach(() => {
tmpdir = tmp.in(tmproot);
process.chdir(tmpdir);
project = { root: tmpdir };
project = { root: tmpdir, pkg: {} };
});

afterEach(() => {
process.chdir(root);
return remove(tmproot);
});

function generateConfigFile(contents, _filename) {
let filename = _filename || 'ember-try.js';
fs.mkdirsSync('config');
function generateConfigFile(contents, filename = 'config/ember-try.js') {
let directory = path.dirname(filename);
fs.mkdirsSync(directory);

fs.writeFileSync(`config/${filename}`, contents, { encoding: 'utf8' });
fs.writeFileSync(filename, contents, { encoding: 'utf8' });
}

describe('addImplicitBowerToScenarios', () => {
Expand Down Expand Up @@ -116,14 +116,40 @@ describe('utils/config', () => {
});

it('uses specified options.configFile if present', () => {
generateConfigFile('module.exports = { scenarios: [ { qux: "baz" }] };', 'non-default.js');
generateConfigFile('module.exports = { scenarios: [ { qux: "baz" }] };', 'config/non-default.js');

return getConfig({ project, configPath: 'config/non-default.js' }).then((config) => {
expect(config.scenarios).to.have.lengthOf(1);
expect(config.scenarios[0].qux).to.equal('baz');
});
});

it('uses projects configured configPath if present', async () => {
generateConfigFile('module.exports = { scenarios: [ { foo: "bar" }] };', 'other-path/ember-try.js');

project.pkg['ember-addon'] = {
configPath: 'other-path',
};

let config = await getConfig({ project })

expect(config.scenarios).to.have.lengthOf(1);
expect(config.scenarios[0].foo).to.equal('bar');
});

it('falls back to config/ember-try.js if projects configured configPath is not present', async () => {
generateConfigFile('module.exports = { scenarios: [ { foo: "bar" }] };');

project.pkg['ember-addon'] = {
configPath: 'other-path',
};

let config = await getConfig({ project })

expect(config.scenarios).to.have.lengthOf(1);
expect(config.scenarios[0].foo).to.equal('bar');
});

it('uses projects config/ember-try.js if present', () => {
generateConfigFile('module.exports = { scenarios: [ { foo: "bar" }] };');

Expand Down Expand Up @@ -198,7 +224,7 @@ describe('utils/config', () => {
});

it('uses specified options.configFile over project config/ember-try.js', () => {
generateConfigFile('module.exports = { scenarios: [ { qux: "baz" }] };', 'non-default.js');
generateConfigFile('module.exports = { scenarios: [ { qux: "baz" }] };', 'config/non-default.js');
generateConfigFile('module.exports = { scenarios: [ { foo: "bar" }] };'); // Should not be used

return getConfig({ project, configPath: 'config/non-default.js' }).then((config) => {
Expand Down

0 comments on commit cd0a440

Please sign in to comment.