Accessing non-main modules of bundled packages


#1

Not sure if this is possible, but if it is, could someone let me know how to do it?

For example, if I want to access the package-manager module from settings-view, how would I go about doing this?

What I’ve tried:

I can do something like sv = atom.packages.getActivePackage('settings-view'), and then sv.mainModule, which will give me the main module, but this is not exactly what I want.

I could also do something like sv.mainModulePath and try to get at the surrounding modules via altering that filepath, but this is a bit hackish. In fact it doesn’t even work in my case, as this is returned:

/Applications/Atom.app/Contents/Resources/app.asar/node_modules/settings-view/lib/main.js

Note the app.asar, which is not a real filepath, as I understand it.

Is there a right way to do this?


#2

This should not be a problem. Atom (or rather the underlying electron) integrates asar virtual file paths into require.

settingsview = atom.packages.getLoadedPackage('settings-view')
PackageManager = require(path.normalize(path.join settingsview.mainModulePath, '..', 'package-manager.js'))
pm = new PackageManager
pm.loadInstalled (err, data) -> console.log data # Works as expected

Doesn’t look nice but it works.


#3

Beware, this kind of trick is really like a double-edge sword. If a class/module isn’t exposed publicly then there’s a high probability that it’ll change without you noticing, leading to nasty bugs (I know what I’m talking about, see https://github.com/atom-minimap/minimap/issues/357).


#4

@deprint
Thank you! I didn’t know anything about asar until I encountered this.

@abe
Good point. Is there a best practice for incorporating others’ code like this? I am hesitant to copy and paste, as it will look like I wrote the file in the commit history.
It looks like package-manager.coffee is under MIT or something similar, so maybe it’s no big deal.