Repository: ember-cli/ember-try
Pull request #585 (2020-10-28 23:02:23)

Use `ember-addon.configPath` from `package.json` if defined.
This makes `ember-try` work much closer to how other ecosystem tools work (`@ember/optional-features`, `ember-cli`'s lookup of `config/environment.js` and `config/targets.js`, `ember-cli-code-coverage`).

This is not a breaking change, as it still falls back to using the older hardcoded `config/ember-try.js` path.

Fixes https://github.com/ember-cli/ember-try/issues/305



Patch:
diff --git a/index.js b/index.js
index 0679a2f..66d7df2 100644
--- a/index.js
+++ b/index.js
@@ -3,7 +3,7 @@
 module.exports = {
   name: 'ember-try',
 
-  includedCommands: function() {
+  includedCommands() {
     return require('./lib/commands');
   }
 };
diff --git a/lib/commands/config.js b/lib/commands/config.js
index 6c9eed9..50cbd95 100644
--- a/lib/commands/config.js
+++ b/lib/commands/config.js
@@ -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) {
diff --git a/lib/commands/try-each.js b/lib/commands/try-each.js
index e314928..7b9e645 100644
--- a/lib/commands/try-each.js
+++ b/lib/commands/try-each.js
@@ -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'),
diff --git a/lib/commands/try-ember.js b/lib/commands/try-ember.js
index 8ded64c..fd5ae7c 100644
--- a/lib/commands/try-ember.js
+++ b/lib/commands/try-ember.js
@@ -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'),
diff --git a/lib/utils/config.js b/lib/utils/config.js
index bd6eac3..f3e9042 100644
--- a/lib/utils/config.js
+++ b/lib/utils/config.js
@@ -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) {


Test patch:
diff --git a/test/utils/config-test.js b/test/utils/config-test.js
index 456633c..577c5e0 100644
--- a/test/utils/config-test.js
+++ b/test/utils/config-test.js
@@ -21,7 +21,7 @@ describe('utils/config', () => {
   beforeEach(() => {
     tmpdir = tmp.in(tmproot);
     process.chdir(tmpdir);
-    project = { root: tmpdir };
+    project = { root: tmpdir, pkg: {} };
   });
 
   afterEach(() => {
@@ -29,11 +29,11 @@ describe('utils/config', () => {
     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', () => {
@@ -116,7 +116,7 @@ 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);
@@ -124,6 +124,32 @@ describe('utils/config', () => {
     });
   });
 
+  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" }] };');
 
@@ -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) => {