Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer addon.moduleName as telemetry if one exists, otherwise fallback to packageName #38

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

hmajoros
Copy link

fixes #37.

if the codemod telemetry helpers are being run from inside of an ember-addon, prefer to use the addon's moduleName if it exists, otherwise fallback to the package name from package.json.

This is important as many packages now use npm's scoped package feature, which allows you to create packages like @company/package-name. In ember-land, these packages need to have a different module name (in this case, just package-name, as the @ syntax breaks handlebars invocation of templates from this package (ex: <@Company/Package-Name /> )

* @returns {String} The runtime name of the addon
*/
function getAddonPackageName(packagePath) {
const indexPath = path.resolve(path.dirname(packagePath), 'index.js');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't assume packagePath + index.js is the right addon entry point. We have to respect ember-addon.main in package.json.

Comment on lines 39 to 40
// this is bad, fix later
return moduleName && typeof moduleName === 'function' ? moduleName() : packageName;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably do this in a try/catch block. In most cases that I've seen, the moduleName function is simple (e.g. it returns a string directly, without logic) but in some cases it is reasonable to access other context off of the addon instance (e.g. this.name). If an addon's moduleName implementation does this it will error and break telemetry gathering.

lib/utils/get-module-path-for.js Outdated Show resolved Hide resolved
@@ -18,12 +18,37 @@ 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);
Copy link

@tehhowch tehhowch Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be packageDir and not packagePath?

(If it is packagePath, this function dies attempting to require('package.json') on my team's addon, but if it is packageDir, then the native-class codemod is able to transform classes. Our setup is a multi-app monorepo that also contains a shared ember addon with reused components and services.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support transforming addons where package.name != addon.moduleName
3 participants