Now do I properly get the path name to my plugin?

Greetings,

What is the right way to get the path name to my plugin?

I am doing it as follows:
Code:
//  Copyright (c) 2017 Joy Diamond.  MIT License.
alert(new RegExp(/ at file:\/\/\/(.*):\d+:\d+$/).exec(new Error().stack)[1]);

/*: @plugindesc Opal: Optimium Plugin Automatic Loader */
... It works ...

Spoiler: Picture of alert box properly showing the path name
upload_2017-11-25_20-56-25.png

... But I am pretty unhappy with the ugliness of:
  • new RegExp(/ at file:\/\/\/(.*):\d+:\d+$/).exec(new Error().stack)[1]
Any better suggestions?​

Thanks,

Joy Diamond

P.S.: This is only meant for local development, not for release (it is supposed to load my plugin's as I modify them while I develop them), so using file:/// in the regular expression is fine.

Also I searched the internet and didn't find anything better than the above. The above was from
How might I get the script filename from within that script?
​
you also have some tricks with node for get file and local.

PHP:
var path = require('path');
      gameDir = path.dirname(process.mainModule.filename);
i very love how you do it in regex !.
however the regex is a bit heavier, but I find the code very clean.
this it a exampel only, but need to more test in deep to target what you need.
Greetings Jonforum,
Code:
var path = require('path');
      gameDir = path.dirname(process.mainModule.filename);
This returns me the HTML page (i.e.: index.html) not my plugin name (i.e.: js/plugins/Opal.js)
  • Very useful also -- I want this also.. So thanks 😎
Here is my modified code using your suggestion:
Code:
//  Copyright (c) 2017 Joy Diamond.  MIT License.
alert(
      'HTML       is ' + process.mainModule.filename + '\n'
    + 'JavaScript is ' + (new RegExp(/ at file:\/\/\/(.*):\d+:\d+$/).exec(new Error().stack)[1])
);

/*: @plugindesc Opal: Optimium Plugin Automatic Loader */
And the results of running this new version:

Spoiler: Alert box showing path to index.html & js/plugins/Opal.js
upload_2017-11-25_21-32-22.png

I guess I could use (from your suggestion):
  • gameDir + 'js/plugins/Opal.js'
  • (But I am trying to avoid hardcoding 'Opal.js' into my code ... I know I am probably overly finicky 😕)
  • I will probably do it the way you suggested, I detest creating Error objects & using RegExp on them .... just seems ... wrong ...
Thanks,

Joy Diamond.
​
thank you for sharring your snippet.
yes the code i give you in this context, allow you to get for example the current Dir for the game project from rmmv.
i use it to detect custom directory and some file extention for animation.
the full context can be found here.

check at line 254; you have more context and code from node here
https://www.dropbox.com/s/bpnscupkhcomxgk/rmmvPixiSpriteAnimeCore.js?dl=0

I do not know by heart the code under node, however I think that with several try & error , you could find a way.
However waring, it does not work under android. (intel XDK test)

if you find a great way, thanks you share it.
Greetings Jonforum,

the full context can be found here.

check at line 254; you have more context and code from node here
https://www.dropbox.com/s/bpnscupkhcomxgk/rmmvPixiSpriteAnimeCore.js?dl=0
Thanks, I read the code very helpful 🙂

Of even bigger interest to me was line 212:

Code:
 if(Utils.isNwjs()&&Utils.isOptionValid('test')){var _debugWindow=require('nw.gui').Window.get().showDevTools();console.log('_debugWindow: ',_debugWindow);_debugWindow.moveTo(0,0);_debugWindow.height=800;_debugWindow.width=1100;window.focus();}; // delete me, show debug console
I've really wanted to do that, I am so tired of constantly hitting F8 (or F12). Thanks! 😎​

Joy Diamond

P.S.: Thanks for putting your code under the MIT license. Much appreciated 🙂
I was about to lay down for the night and decided to scan the forum real quick, I noticed this post and just had to chime in.
Are you attempting to get the parameters via plugin filename? If so, I'd recommend instead of the filename pull a special string from the description in the plugins.json array. Ivara from the forums here showed us this when MV was first released, I will never forget lol.

PHP:
//  Copyright (c) 2017 Joy Diamond.  MIT License.

/*: @plugindesc Opal: Optimium Plugin Automatic Loader <opal> */

const parameters = window.$plugins.filter((p) => p.description.contains('<Opal>'))[0].parameters

// Or put it in the PluginManager as a function
  PluginManager.getPluginParameters = function (plugin) {
    return window.$plugins.filter((p) => p.description.contains('<' + plugin + '>'))[0].parameters
  }
// And retrieve them by calling
const parameters = PluginManager.getPluginParameters('Opal')

If that is not what you're trying to do and you just want a simple way to get the direct path the plugin is located then the way shown by jonforum is what I would do.

Original thread: https://forums.rpgmakerweb.com/threads/now-do-i-properly-get-the-path-name-to-my-plugin.87728/