Dino
Dino

Reputation: 352

How to access meteor package name inside package?

I am creating a package and want to use the name of the package dynamically within the package code (i.e. for logging in my /log.js file).

How do I access the variable that is defined in package.js?

Package.describe({
    name: "author:packagename"
})

Since I can not be sure if package names are changing do to separation etc. , I don't want to hard code them.

Thanks

Upvotes: 3

Views: 352

Answers (1)

Tarang
Tarang

Reputation: 75945

It's a bit difficult to do this as the packaging system doesn't give access to this. You can give this a shot though.

Create a publicly available global object using api.exports, e.g api.exports("some_global", ["server", "client"]);

The global in your package (can be anything else).

some_global = {}

Then you can run this to find the package name (make sure it has access to some_global

getPackageName = function() {
    for(var packageName in Package) {
        if(Package[packageName] && Package[packageName].some_global === some_global) return packageName
    } 
}

Meteor.startup(function() {
    console.log(getPackageName()) //=> Should give the package name
});

It's a bit messy. Meteor removes most of the stuff mean't to help with this when its in compiled form with the exception of the global Package object to remove unnecessary code.

I gave this a quick test with _ (used for the underscore package):

getPackageName = function() {
    for(var packageName in Package) {
        if(Package[packageName] && Package[packageName]._ === _) return packageName 
    }
}

console.log(getPackageName) // => "underscore"

Hope it helps!

Upvotes: 2

Related Questions