From 746e348ba3de110bdc8a0091abb17e9b8208d753 Mon Sep 17 00:00:00 2001 From: Henry Majoros Date: Wed, 24 Feb 2021 17:00:56 -0800 Subject: [PATCH 1/4] prefer addon.moduleName as telemetry if one exists, otherwise fallback to package name --- lib/utils/get-module-path-for.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/utils/get-module-path-for.js b/lib/utils/get-module-path-for.js index dc14fa1..5c629c7 100644 --- a/lib/utils/get-module-path-for.js +++ b/lib/utils/get-module-path-for.js @@ -18,12 +18,32 @@ for (let packagePath of packagePaths) { let packageDir = path.dirname(path.resolve(cwd, packagePath)); if (pkg.keywords && pkg.keywords.includes('ember-addon')) { - ADDON_PATHS[packageDir] = pkg.name; + // build addon instance + ADDON_PATHS[packageDir] = getAddonPackageName(packagePath); } else if (isEmberCliProject(pkg)) { APP_PATHS[packageDir] = pkg.name; } } +/** + * takes a package path and returns the runtime name of the addon. + * + * @param {String} packagePath the path on disk (from current working directory) + * @returns {String} The runtime name of the addon + */ +function getAddonPackageName(packagePath) { + const indexPath = path.resolve(path.dirname(packagePath), 'index.js'); + + if (fs.existsSync(indexPath)) { + const { name: packageName, moduleName } = require(indexPath); + // this is bad, fix later + return moduleName && typeof moduleName === 'function' ? moduleName() : packageName; + } + + const { name: packageName } = require(packagePath); + return packageName; +} + function isEmberCliProject(pkg) { return ( pkg && From b361a14aa2ac74f2307bd20233c864ef154c31b4 Mon Sep 17 00:00:00 2001 From: Hank Majoros Date: Fri, 26 Mar 2021 12:48:33 -0400 Subject: [PATCH 2/4] Add Rob's suggestions in lib/utils/get-module-path-for.js Co-authored-by: Robert Jackson --- lib/utils/get-module-path-for.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/utils/get-module-path-for.js b/lib/utils/get-module-path-for.js index 5c629c7..5d9561a 100644 --- a/lib/utils/get-module-path-for.js +++ b/lib/utils/get-module-path-for.js @@ -32,16 +32,22 @@ for (let packagePath of packagePaths) { * @returns {String} The runtime name of the addon */ function getAddonPackageName(packagePath) { - const indexPath = path.resolve(path.dirname(packagePath), 'index.js'); - - if (fs.existsSync(indexPath)) { - const { name: packageName, moduleName } = require(indexPath); - // this is bad, fix later - return moduleName && typeof moduleName === 'function' ? moduleName() : packageName; + const pkg = require(packagePath); + const entryPoint = pkg['ember-addon'] && pkg['ember-addon'].main + ? pkg['ember-addon'].main + : 'index.js'; + + let moduleName = pkg.name; + try { + let entryModule = require(path.join(packagePath, entryPoint)); + if (typeof entryModule.moduleName === 'function') { + moduleName = entryModule.moduleName(); + } + } catch { + // do nothing, this falls back to using package name } - - const { name: packageName } = require(packagePath); - return packageName; + + return moduleName; } function isEmberCliProject(pkg) { From fa4979fd771526074979a052fa1151b57f08276f Mon Sep 17 00:00:00 2001 From: Henry Majoros Date: Fri, 26 Mar 2021 12:57:21 -0400 Subject: [PATCH 3/4] bump eslint ecmaversion to allow optional catch binding --- .eslintrc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintrc.js b/.eslintrc.js index 760ce7b..aec49d1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,7 +1,7 @@ module.exports = { root: true, parserOptions: { - ecmaVersion: 2017 + ecmaVersion: 2019 }, env: { node: true, From adefcb783d0a2a74890972ef0c429f4d46d5c1dd Mon Sep 17 00:00:00 2001 From: Henry Majoros Date: Fri, 26 Mar 2021 13:05:42 -0400 Subject: [PATCH 4/4] run prettier --- lib/utils/get-module-path-for.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/utils/get-module-path-for.js b/lib/utils/get-module-path-for.js index 5d9561a..149e720 100644 --- a/lib/utils/get-module-path-for.js +++ b/lib/utils/get-module-path-for.js @@ -33,9 +33,8 @@ for (let packagePath of packagePaths) { */ function getAddonPackageName(packagePath) { const pkg = require(packagePath); - const entryPoint = pkg['ember-addon'] && pkg['ember-addon'].main - ? pkg['ember-addon'].main - : 'index.js'; + const entryPoint = + pkg['ember-addon'] && pkg['ember-addon'].main ? pkg['ember-addon'].main : 'index.js'; let moduleName = pkg.name; try { @@ -46,7 +45,7 @@ function getAddonPackageName(packagePath) { } catch { // do nothing, this falls back to using package name } - + return moduleName; }